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

问题描述 投票:0回答:2

我使用以下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()

This is what I have right now

我希望能够将地图显示为灰度图,并且仅输入一个邮政编码,如果可以提供帮助,请告诉我。谢谢!

r ggplot2 colors mapping zipcode
2个回答
2
投票

一种方法是仅添加一列,该列为每个邮政编码指定所需的填充键,然后使用scale_fill_manual()将其链接到ggplot中。在这里,我将邮政编码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()

enter image description here


2
投票

如果将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()
© www.soinside.com 2019 - 2024. All rights reserved.