r - ggplot 填充 SpatialPolygonsDataFrame
问题描述
我正在尝试绘制美国县 SPDF 并对出现在另一个向量中的县进行着色(通过 GEOID 匹配)。这是我目前拥有的代码,但没有一个县是阴影的。
我已经从这个链接下载了美国县界的 ShapeFile 。
library(ggplot2)
library(raster)
US_counties <- shapefile("~/us_county_shp_files/tl_2017_us_county.shp")
counties_in_radius <- c("12011", "12015", "12021", "12027", "12043", "12049", "12051", "12055", "12061")
base_plot <- ggplot(data = US_counties) +
coord_fixed(ratio = 1.3) +
geom_polygon(data=US_counties, aes(x = long, y = lat, group = group), size = 0.1, colour = "gray",
fill= (if(US_counties$GEOID %in% counties_in_radius) "blue" else NA))
base_plot
如果我只做 GEOID,我会得到错误对象 'GEOID' not found,而当我做 US_counties$GEOID 时,我会得到“条件长度 > 1,并且只会使用第一个元素”。
如何正确填写counties_in_radius 中的县?
解决方案
推荐阅读
- android - 访问文件创建抛出错误打开失败:Android 11 中的 EACCES(权限被拒绝)
- javascript - 为什么这段代码不会显示hello world?它说 text.content 为 null
- marklogic - cts.estimate 显示错误的文档计数
- python - 使用 AWS CDK for python 创建 AWS 网络负载均衡器时无法传递 ec2-instance 详细信息
- html - 每个索引上的 SCSS for 循环 x 次
- vector - 沿向上向量的最高点
- twitter - 如何通过 Twitter API 计算推文数量
- reactjs - 反应警告消息“将箭头函数分配给变量”
- r - 如何通过点图绘制二进制数据?
- javascript - 如何使用正则表达式从状态为“通知”或“无库存”的字符串中删除项目