如何使用usmap绘制康涅狄格州旧郡区域(无需降级版本)
无需降级usmap即可绘制康涅狄格州旧郡区域
可以做到,不需要降级usmap版本,核心思路是在usmap绘制的基础地图上,叠加康涅狄格州旧郡的shapefile图层,具体实现方法如下:
方法一:手动导入旧郡shapefile
- 获取康涅狄格州2020年之前的郡级shapefile(可从美国人口普查局历史地理数据存档中获取,比如2019版TIGER/Line郡级文件)
- 用
sf包读取shapefile并转换为usmap兼容的坐标系 - 将你的旧郡统计数据与shapefile关联
- 先绘制usmap的基础地图(包含MA、CT、RI),再叠加旧郡的填充图层
代码示例:
library(usmap) library(ggplot2) library(sf) # 读取旧郡shapefile(替换为你的文件路径) ct_old_counties <- st_read("tl_2019_09_county.shp") # 转换为usmap使用的Albers Equal Area坐标系 ct_old_counties <- st_transform(ct_old_counties, crs = usmap_crs()) # 准备你的旧郡数据 CT_counties <- data.frame( county = c("Fairfield County, CT", "Hartford County, CT", "Litchfield County, CT", "Middlesex County, CT", "New Haven County, CT", "New London County, CT", "Tolland County, CT", "Windham County, CT"), fips = c("09001", "09003", "09005", "09007", "09009", "09011", "09013", "09015") ) plotdf <- data.frame(fips = CT_counties$fips, vals = rnorm(8)) # 合并shapefile与统计数据 ct_old_counties <- merge(ct_old_counties, plotdf, by.x = "GEOID", by.y = "fips") # 绘制基础地图 base_map <- plot_usmap(include = c("MA", "CT", "RI"), labels = FALSE) + theme(legend.position = "right") # 叠加旧郡填充图层 base_map + geom_sf(data = ct_old_counties, aes(fill = vals), inherit.aes = FALSE) + scale_fill_gradient(low = "#f0f9e8", high = "#0868ac") # 自定义配色
方法二:用tigris包直接获取旧版本郡数据
如果不想手动下载文件,可使用tigris包直接拉取2019年的康涅狄格郡数据:
library(usmap) library(ggplot2) library(tigris) library(sf) # 获取2019版康涅狄格郡数据 ct_old_counties <- counties(state = "CT", year = 2019) ct_old_counties <- st_transform(ct_old_counties, crs = usmap_crs()) # 合并数据并叠加图层 plotdf <- data.frame(fips = CT_counties$fips, vals = rnorm(8)) ct_old_counties <- merge(ct_old_counties, plotdf, by.x = "GEOID", by.y = "fips") plot_usmap(include = c("MA", "CT", "RI"), labels = FALSE) + geom_sf(data = ct_old_counties, aes(fill = vals), inherit.aes = FALSE) + scale_fill_viridis_c()
这种方式既保留了usmap绘制多州地图的便利性,又能完美适配你的旧郡维度统计数据。
内容的提问来源于stack exchange,提问作者Eff
相关产品推荐
相关产品推荐

