Search code examples
rggplot2fill

How do can I color in specific counties in R?


I would like to create a map of California with Fresno, San Joaquin and Monterrey county colored in using R. I have created a map of California with counties however I am unable to color in the specific counties. Below is my R code

install.packages(c("maps", "mapdata"))

library(tidyverse)
library(mapdata)

library(maps)
library(stringr)
 library(viridis)
    
 usa <- map_data("usa")

 dim(usa)

 head(usa)


 tail(usa)

 usa <- map_data("usa") # we already did this, but we can do it again
 ggplot() + 
 geom_polygon(data = usa, aes(x = long, y = lat, group = group)) + 
 coord_quickmap()


 states <- map_data("state")
 dim(states)

 tail(states)

 ggplot(data = states) + 
 geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") + 
 coord_quickmap() +
 guides(fill = FALSE)  # do this to leave off the color legend

ca_df <- states %>%
  filter(region == "california")

head(ca_df)

counties <- map_data("county") ca_county <- counties %>% filter(region == "california")

head(ca_county)

ca_base <- ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) + 
 coord_quickmap() + 
  geom_polygon(color = "black", fill = "gray")
 ca_base + theme_void()

ca_base + theme_void() + 
 geom_polygon(data = ca_county, fill = NA , color = "white") + 
 geom_polygon(color = "black", fill = NA)   + 
 labs(title = "California Counties",
   subtitle = "This is a map of the counties in California with Fresno, San Joaquin, and           
    Monterrey Counties highlighted."

I tried changing the "fill" option howeves this changed the color of the whole region. ca_county$subregion containts the county information.


Solution

  • You were almost there! First you need to set up a coloring variable in your ca_county dataframe, and then use that variable as the fill parameter in the geom_polygon function.

    # define your three target counties
    target_counties = c("fresno", "san joaquin", "monterey")  # note that 'monterey' is the correct spelling
    
    # add a new column to identify whether each county is a target county
    ca_county$highlight = if_else(ca_county$subregion %in% target_counties, "highlight", "other")
    
    # create the base plot
    ca_base = ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) + 
      coord_quickmap() + 
      geom_polygon(color = "black", fill = "gray")
    
    # add the counties, coloring by the highlight variable
    ca_base + theme_void() + 
      geom_polygon(data = ca_county, aes(fill = highlight), color = "white") +
      scale_fill_manual(values = c("other" = "gray", "highlight" = "red")) +
      geom_polygon(color = "black", fill = NA)   + 
      labs(title = "California Counties",
           subtitle = "This is a map of the counties in California with Fresno, San Joaquin, and Monterey Counties highlighted.") + 
      guides(fill = FALSE)
    

    This script creates a new variable highlight that has the value "highlight" for your three target counties and "other" for all other counties. This variable is then used in the aes argument of geom_polygon to fill the counties with different colors. The scale_fill_manual function is used to set the colors, with your target counties in red and all other counties in gray.

    P.S. I took the liberty to remove the coloring scheme, describing it in the subtitle. If you prefer otherwise, remove guides(fill = FALSE).