You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整阿拉斯加、夏威夷河流图层位置以匹配plot_usmap

解决plot_usmap中阿拉斯加/夏威夷河流位置不匹配问题

问题核心

plot_usmap默认会将阿拉斯加(AK)和夏威夷(HI)偏移至美国本土南部展示,但你的河流shapefile中这两地的河流保留了真实地理位置,导致图层位置错位。由于河流数据无州标识,直接用shift_geometry无法生效,我们可以通过空间筛选区分区域+手动偏移解决。


步骤1:拆分河流数据为本土、阿拉斯加、夏威夷三部分

先在WGS84(EPSG:4326)坐标系下筛选不同区域的河流,这个坐标系下更容易用空间范围或州边界区分:

library(usmap)
library(sf)
library(dplyr)

# 1. 确保河流数据转换为WGS84坐标系(如果原始不是的话)
rivers_wgs84 <- st_transform(rivers, 4326)

# 2. 获取usmap自带的WGS84格式州边界,用于精准筛选
us_states_wgs84 <- us_map("states") %>% 
  st_as_sf(coords = c("x", "y"), crs = "ESRI:102003") %>% 
  st_cast("POLYGON") %>% 
  group_by(abbr) %>% 
  st_union() %>% 
  st_sf() %>% 
  ungroup() %>% 
  st_transform(4326)

# 3. 筛选各区域河流
ak_state <- us_states_wgs84 %>% filter(abbr == "AK")
rivers_ak <- st_intersection(rivers_wgs84, ak_state)

hi_state <- us_states_wgs84 %>% filter(abbr == "HI")
rivers_hi <- st_intersection(rivers_wgs84, hi_state)

rivers_contiguous <- rivers_wgs84 %>% 
  st_difference(ak_state) %>% 
  st_difference(hi_state)

步骤2:对阿拉斯加/夏威夷河流应用偏移

使用usmap::shift_geometry对AK和HI的河流做与州地图一致的偏移,再统一转换到plot_usmap使用的ESRI:102003投影:

# 转换本土河流到目标投影
rivers_contiguous_proj <- st_transform(rivers_contiguous, "ESRI:102003")

# 对AK、HI河流应用偏移后转换投影
rivers_ak_proj <- st_transform(rivers_ak, "ESRI:102003") %>% 
  shift_geometry(regions = "AK")

rivers_hi_proj <- st_transform(rivers_hi, "ESRI:102003") %>% 
  shift_geometry(regions = "HI")

步骤3:合并图层绘制地图

将处理后的河流图层添加到你的原始填充地图上:

# 原始填充地图
p <- plot_usmap(data = state_difference, values = "my_variable") +
  theme(legend.position = "bottom") +
  scale_fill_stepsn(breaks = as.vector(quantile(state_difference$median_premium, probs = seq(0, 1, 1/5))), colors = heat.colors(5)) +
  labs(title = "Graph title")

# 添加河流图层
p +
  geom_sf(data = rivers_contiguous_proj, color = "#4A6FE3", size = 0.3) +
  geom_sf(data = rivers_ak_proj, color = "#4A6FE3", size = 0.3) +
  geom_sf(data = rivers_hi_proj, color = "#4A6FE3", size = 0.3)

补充说明

  • 如果不需要精准筛选,也可以用经纬度范围替代州边界筛选(适合数据量较大的情况):
    # 阿拉斯加大致经纬度范围
    ak_bbox <- st_bbox(c(xmin = -180, xmax = -130, ymin = 50, ymax = 72), crs = 4326) %>% st_as_sfc()
    rivers_ak <- st_intersection(rivers_wgs84, ak_bbox)
    
    # 夏威夷大致经纬度范围
    hi_bbox <- st_bbox(c(xmin = -162, xmax = -154, ymin = 18, ymax = 22), crs = 4326) %>% st_as_sfc()
    rivers_hi <- st_intersection(rivers_wgs84, hi_bbox)
    
  • 调整geom_sf的color和size参数可以优化河流的显示效果。

内容的提问来源于stack exchange,提问作者leviemb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:37:02