如何在R语言中提取sf格式的美国本土轮廓数据?
提取美国本土SF数据或仅绘制本土的方法
方法一:提前提取美国本土的SF子集
你可以通过坐标范围过滤或裁剪的方式,从giscoR获取的美国数据中分离出本土部分。这里用经纬度范围精准定位美国本土(西经125°至66°,北纬24°至49°):
library(sf) library(giscoR) # 获取美国全疆域数据 usa <- gisco_get_countries(country = "USA") # 定义美国本土的边界框 mainland_bbox <- st_bbox( c(xmin = -125, ymin = 24, xmax = -66, ymax = 49), crs = st_crs(usa) ) # 方法1:直接裁剪(快速去除域外部分) usa_mainland <- st_crop(usa, mainland_bbox) # 方法2:基于空间交集过滤(保留完整的本土多边形,避免边缘裁剪) mainland_poly <- st_as_sfc(mainland_bbox) usa_mainland <- usa[st_intersects(usa, mainland_poly, sparse = FALSE), ]
方法二:在ggplot中直接绘制美国本土
如果不想提前生成子集,也可以在geom_sf中直接设置过滤条件,同时配合coord_sf锁定显示范围:
library(ggplot2) # 复用之前定义的本土边界框/多边形 ggplot() + geom_sf( data = usa, subset = st_intersects(usa, mainland_poly, sparse = FALSE), fill = "#6baed6", color = "white" ) + coord_sf(xlim = c(-125, -66), ylim = c(24, 49)) + theme_minimal()
补充说明
- 如果需要更精准的筛选(比如排除波多黎各等海外领地),可以结合
usa数据的属性列(如NAME_ENGL或GID_1)进一步过滤,但坐标范围法已经能覆盖绝大多数场景。 st_crop适合快速生成可视化用的裁剪数据,st_intersects则保留完整的多边形几何,适合后续空间分析。
内容的提问来源于stack exchange,提问作者Miles N.
相关产品推荐
相关产品推荐

