【问题标题】:How can I color in one zipcode in a map, RStudio?如何在地图中的一个邮政编码中着色,RStudio?
【发布时间】:2019-12-31 19:05:50
【问题描述】:

我使用这个 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()

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

【问题讨论】:

    标签: r ggplot2 colors mapping zipcode


    【解决方案1】:

    一种方法是添加一个列,为每个邮政编码指定一个填充键,然后使用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()
    

    【讨论】:

      【解决方案2】:

      如果你把scale_fill_vidris_c()改成scale_fill_gray()然后加上geom_sf(data=filter(dallas_streets, zip=='&lt;zipcode&gt;'), 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()
      

      【讨论】:

        猜你喜欢
        • 2019-02-20
        • 2012-10-23
        • 1970-01-01
        • 2023-01-11
        • 2020-04-19
        • 1970-01-01
        • 2021-11-14
        • 2012-01-25
        • 1970-01-01
        相关资源
        最近更新 更多