2016-11-21 89 views
0

我有一个带有邮政编码的数据集,我用choroplethrZip绘制数据。我正在查看州和县一级的数据。但是,邮政编码并不一定与州和郡线相符。我试图使用reference_map = TRUE,但它没有县线,并开始看起来有点忙。有没有办法将默认参考地图更改为具有州和县行而没有地图细节其余部分的地图?换句话说,我不想要街道和地形。R:州和县概述choroplethrZip

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

#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

回答

0

我用这个博客,发现了如何使这项工作: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函数中添加它,它工作得很好。当我用我的数据(而不是通用人口数据)进行测试时,它也起作用。