R:choroplethrZip 的州和县大纲

R: State and County Outlines for choroplethrZip

我有一个包含邮政编码的数据集,我使用 choroplethrZip 来绘制数据图表。我正在查看州和县级的数据。但是,邮政编码不一定对应于州和县的行。我试过使用 reference_map = TRUE,但它没有县界线,开始看起来有点忙。有没有办法将默认参考地图更改为具有州和县界线而没有其余地图详细信息的参考地图?换句话说,我不想要街道和地形。

这是我的代码,其中包含与我正在使用的数据类似的示例数据。您可以看到德克萨斯州边界的问题。

#zip.regions metadata file for choroplethrZip
data(zip.regions)
head(zip.regions)

#Test data file:A data.frame containing population estimates 
# for US Zip Code Tabulated Areas (ZCTAs) in 2012.
data(df_pop_zip) 

#Create a choropleth of US Zip Codes
zip_choropleth(df_pop_zip, 
               state_zoom="texas", 
               title="2012 Texas State ZCTA Population Estimates",
               legend="Population",
               reference_map = TRUE)

#Zoom County
dd_fips = c(48113, 48121)
zip_choropleth(df_pop_zip, 
               county_zoom=dd_fips,  
               title="2012 Denton & Dallas ZCTA Population Estimates",
               legend="Population",
               reference_map = TRUE)

TexasPlot

DentonDallasPlot

我使用了这个博客并了解了如何进行这项工作:http://www.arilamstein.com/blog/2015/07/02/exploring-the-demographics-of-ferguson-missouri/

这是我的最终代码:

library(choroplethrZip)
library(ggplot2)
library(choroplethr)


#Pull in zip.regions metadata file for choroplethrZip
data(zip.regions)
head(zip.regions)

#Test data file:A data.frame containing population estimates 
# for US Zip Code Tabulated Areas (ZCTAs) in 2012.
data(df_pop_zip)

# highlight a county
highlight_county = function(county_fips)
{
  library(choroplethrMaps)
  data(county.map, package="choroplethrMaps", envir=environment())
  df = county.map[county.map$region %in% county_fips, ]
  geom_polygon(data=df, aes(long, lat, group = group), color = "yellow", fill = NA, size = 1)
}

#Zoom County
dd_fips = c(48113, 48121)
zip_choropleth(df_pop_zip, 
               county_zoom=dd_fips,  
               title="2012 Denton & Dallas ZCTA Population Estimates",
               legend="Population",
               reference_map = TRUE) +
              highlight_county(dd_fips)

Rplot Counties

我只需要添加 highlight_county 功能,效果很好。当我用我的数据(而不是一般人口数据)进行测试时,它也能正常工作。