删除geo_join

时间:2015-12-09 23:14:28

标签: r leaflet tigris

我试图在明尼苏达州,爱荷华州和内布拉斯加州的县之上绘制盈利能力。使用leaflettigris,我已经能够绘制所有县,无论我是否有数据。这让我有几个县的颜色,其余的标记为NA。我有没有办法从我的geo_join数据中删除所有NA,以便它不会使用未使用过的Wisconsin区域?我尝试过使用fortify,但我无法弄清楚当我将TIGER边界线与我的县FIPS文件合并以便删除它时,我将如何确定我所看到的县界。

以下是我的leaflet目前的情况:enter image description here

获取地图的代码是:

library(tigris)
library(leaflet)

pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="white")
IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))


leaflet() %>%
      addProviderTiles("MapQuestOpen.OSM") %>%
      addLegend(pal = pal, 
                values = IA_merged$Construction.Cost, 
                position = "bottomright", 
                title = "County Projects",
                labFormat=labelFormat(prefix="$")) %>%
      addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="red",
                 radius = 96560) %>%
      addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="blue",
                 radius = 193121) %>%
      addPolygons(data = IA_counties, 
                  fillColor = ~pal(IA_merged$Construction.Cost), 
                  layerId=1,
                  fillOpacity = .25, 
                  weight = 0.05, 
                  popup = popupIA)%>%
      addPolygons(data=MN_counties,
                  fillColor=~pal(MN_merged$Construction.Cost),
                  fillOpacity=0.25,
                  weight=0.05, 
                  popup = popupMN) %>%
      addPolygons(data=NE_counties,
                  fillColor=~pal(NE_merged$Construction.Cost),
                  fillOpacity=0.25,
                  weight=0.05, 
                  popup = popupNE) 

我为不包含可重复的数据而道歉,但如果需要,请询问。我希望这更像是一个简单的na.color=公式解决方案。地图看起来好了&#34;截至目前,但是我喜欢它,如果不必让fillOpacity这么轻,那么NA县就不会脱颖而出。

感谢您提供的所有帮助,如果您有任何疑问,请与我们联系!

2 个答案:

答案 0 :(得分:2)

我是tigris包的创建者。非常感谢您使用它!在GitHub上的tigris的开发版本(https://github.com/walkerke/tigris)中,我为geo_join添加了一个选项以适应内部联接,这将从结果空间数据框中完全删除不匹配的数据(如果这是你要找的东西)。如果需要,还可以将公共合并列名称作为命名参数提供给新的by参数。例如:

IA_merged <- geo_join(IA_counties, county, by = "GEOID", how = "inner")

应该有效。我还在测试,但我可能会在1月份将此更新提交给CRAN。

答案 1 :(得分:0)

所以,令人尴尬的是,这个问题的答案就像我希望的那样简单。我调整了以下na.color代码,它完全符合我的要求。

pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="transparent")

enter image description here