使用ggplot2和ggmap在R中制作邮政编码等值线

gar*_*son 17 r ggplot2 ggmap choropleth choroplethr

我试图弄清楚非常简单的数据,这是一种痛苦的问题.我在美国东部有以下邮政编码.这是由数据组成,但你明白了.

Zip    Freq
11101    10
10014    15
11238   400
Run Code Online (Sandbox Code Playgroud)

大约100行.Freq的值范围为0-1000,这些是我想用来确定每个邮政编码颜色的值.理想情况下,我也希望地图能够集中在美国东部而不是整个国家.

我想用这些数据和每个邮政编码制作一个等值线,但我无法弄清楚如何导入邮政编码shapefile.我已经尝试过这个教程,但是我在fortify()步骤中遇到了一个错误,我无法超越.我不确定该教程的方法是否是最好的方法.

ggplot2似乎来自州和县,但我无法弄清楚如何通过邮政编码进行映射.(最终我将通过人口普查区进行映射,但现在我只想学习如何使用shapefile来获取邮政编码和这个简单的数据集)

我为choroplethr找到的所有资源都使用了现已弃用的函数.我花了几个小时追逐我的尾巴,努力使用它,我非常沮丧,所以任何帮助将不胜感激.

Ari*_*Ari 11

感谢您的使用choroplethr,对不起,您的弃用zip_map导致了您的问题.我已将所有与ZIP相关的函数移到一个名为choroplethrZip的单独打包中.

choroplethr的旧版本将ZIP渲染为散点图,而不是等值线.将它们渲染为适当的等值线需要一个对于CRAN来说太大的地图(~60MB),这就是为什么它只能通过github获得.

我链接到上面的github页面有3个小插图.基本上,该功能zip_choropleth应该完全符合您的要求,并且像所有其他choroplethr功能一样工作.你想用它state_zoom来放大东海岸的状态:

# use the devtools package from CRAN to install choroplethrZip from github
install.packages("devtools")
library(devtools)
install_github('arilamstein/choroplethrZip@v1.3.0')
library(choroplethrZip)

data(df_pop_zip)

# ec = east coast
ec_states = c("maine", "new hampshire", "massachusetts", "rhode island", "connecticut", 
              "new york", "new jersey", "delaware", "maryland", 
              "virginia", "north carolina", "south carolina", "georgia", "florida",
              "pennsylvania", "district of columbia", "vermont", "west virginia")

zip_choropleth(df_pop_zip, 
               state_zoom = ec_states, 
               title      = "2012 ZCTA Population Estimates",
               legend     = "Population") + coord_map()    
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

生成的地图基本上是不可读的,因为拉链是如此之小,以至于您只能看到边框.如果要删除边框,请尝试以下操作:

choro = choroplethrZip::ZipChoropleth$new(df_pop_zip)
choro$prepare_map()

data(zip.regions)
choro$legend = "Population"
ec_zips = zip.regions[zip.regions$state.name %in% ec_states, "region"]
ec_df   = choro$choropleth.df[choro$choropleth.df$region %in% ec_zips, ]
ec_plot = choro$render_helper(ec_df, "", choro$theme_clean()) + 
              ggtitle("2012 ZCTA Population Estimates")

ec_plot + coord_map() 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在将来,我可能会添加一个选项,以便更容易渲染没有边框的地图.但是现在(版本1.3.0)这是我能看到的最简单的方法,而且基本上我在幕后制作国家邮政编码,它们本身是无国界的.

请注意,coord_map只需强制执行墨卡托投影.

  • 请注意,截至 2020 年 8 月,1.5.0 已发布,因此现在 `install_github('arilamstein/choroplethrZip@v1.5.0')` (2认同)