我如何在地图中的一个邮政编码中着色,RStudio?

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

我使用这个 shapefile 制作了达拉斯的邮政编码地图:https://gis.dallascityhall.com/shapefileDownload.aspx(街道文件)

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()

我希望能够将地图设置为灰度,并且只有一个邮政编码是彩色的,如果您能提供帮助,请告诉我。谢谢!

一种方法是只添加一个列,为每个邮政编码指定一个键,然后使用 scale_fill_manual() 在 ggplot 中 link 它。在这里,邮政编码 75241 为红色,其他邮政编码为浅灰色。

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()

如果将 scale_fill_vidris_c() 更改为 scale_fill_gray(),然后添加 geom_sf(data=filter(dallas_streets, zip=='<zipcode>'), fill='#ff0000')(我随意选择红色 (#ff0000) 作为颜色)会怎么样?组合代码如下所示:

ggplot(dallas_streets) + ggtitle("Zip Code Map of Dallas") +
    geom_sf(aes(fill = as.numeric(POSTAL_L))) + 
    scale_fill_gray()+
    geom_sf(data=filter(dallas_streets, zip=='<zipcode>'), fill='#ff0000')
    geom_sf_text(aes(label = POSTAL_L)) + 
    theme_minimal()