Search code examples
rggplot2colorsmappingzipcode

How can I color in one zipcode in a map, RStudio?


I made a zipcode map of dallas using this shapefile: https://gis.dallascityhall.com/shapefileDownload.aspx (Street Files)

dallas_streets %>% 
  sample_frac(1) %>% 
  group_by(POSTAL_L) %>% 
  summarize(geometry = st_convex_hull(st_union(geometry))) %>% 
  ggplot() + ggtitle("Zip Code Map of Dallas") +
  geom_sf(aes(fill = as.numeric(POSTAL_L))) + 
  geom_sf_text(aes(label = POSTAL_L)) + 
  scale_fill_viridis_c(option = "C") +
  theme_minimal()

This is what I have right now

I want to be able to have the map as greyscale and only one zipcode colored in, please let me know if you can help. Thanks!


Solution

  • One way is to just add a column that specifies a key for the fill you want for each zipcode and then link it in the ggplot using scale_fill_manual(). Here, I do red for zip code 75241 and lightgray for all others.

    dallas_streets2 <- dallas_streets %>% 
      sample_frac(1) %>% 
      group_by(POSTAL_L) %>% 
      summarize(geometry = st_convex_hull(st_union(geometry))) %>% 
      mutate(color = ifelse(POSTAL_L == "75241", "yes", "no"))
    
    dallas_streets2 %>% 
      ggplot() + ggtitle("Zip Code Map of Dallas") +
      geom_sf(aes(fill = color)) + 
      geom_sf_text(aes(label = POSTAL_L)) + 
      scale_fill_manual(values = c("red", "lightgray"), 
                        limits = c("yes", "no")) +
      theme_minimal()
    

    enter image description here