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

如何调整ggplot/sf绘制的中美地图位置与阿拉斯加尺寸

解决R语言绘制中美地图的位置与尺寸调整问题

一、调整两国位置、缩小间距并放大显示尺寸

默认地理坐标下中美相距较远,要拉近间距并放大显示,需手动修改地理要素的坐标:

  • 从空间数据中分离出中国、美国本土、阿拉斯加三个部分(假设数据里有country字段区分国家,region字段标记阿拉斯加)
  • 对美国本土和阿拉斯加做平移变换,将其挪到中国附近区域
  • 对所有地理要素做缩放变换,放大整体显示尺寸

二、缩小阿拉斯加的相对显示尺寸

阿拉斯加实际面积偏大导致图中占比失衡,需单独调整它的几何图形:

  • 提取阿拉斯加的空间数据,用st_scale()缩小其尺寸(比如缩至原大小的1/3)
  • 调整缩放后的阿拉斯加位置,放到美国本土的合适区域(比如右下角)

修改后的完整代码

library(sf)
library(ggplot2)

# 读取空间数据
two_triad_current <- st_read("two_triad_current")

# 1. 拆分各区域数据
china_data <- two_triad_current[two_triad_current$country == "China", ]
usa_main_data <- two_triad_current[two_triad_current$country == "USA" & two_triad_current$region != "Alaska", ]
alaska_data <- two_triad_current[two_triad_current$region == "Alaska", ]

# 2. 调整阿拉斯加:缩小尺寸+平移位置
# 缩小阿拉斯加至原尺寸1/3,再平移到美国本土附近(坐标按需调整)
alaska_scaled <- alaska_data %>%
  st_scale(factor = 1/3) %>%
  st_translate(xoff = -10, yoff = -20)

# 3. 调整美国本土位置:平移至中国附近
usa_main_translated <- usa_main_data %>%
  st_translate(xoff = 120, yoff = 20)

# 4. 放大所有区域显示尺寸(缩放因子按需调整)
china_scaled <- china_data %>% st_scale(factor = 1.2)
usa_main_scaled <- usa_main_translated %>% st_scale(factor = 1.2)
alaska_final <- alaska_scaled %>% st_scale(factor = 1.2)

# 5. 合并调整后的数据
adjusted_data <- rbind(china_scaled, usa_main_scaled, alaska_final)

# 6. 绘制地图
holistic_10_plot1 <- ggplot(adjusted_data) +
  geom_sf(aes(fill = holistic_10_province)) +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +
  geom_sf_text(aes(label = province_current), color = "grey", family = "sans", angle = 0) +
  theme_void() +
  labs(title = "The number of holistic groups(10 focal groups) 
 current states/provinces") +
  theme(legend.position = "bottom", 
        plot.title = element_text(hjust = 0.5), 
        title = element_text(size = 14, face = "bold", color = "black")) +
  guides(fill = guide_colorbar(title = "The number of the holistic groups", title.position = "top"))

# 输出绘图
print(holistic_10_plot1)

关键说明

  • 需根据自身数据的坐标系统(如WGS84)调整st_translate()里的xoff和yoff参数,直到两国位置间距合适
  • st_scale()的缩放因子可按需调整,数值越大图形显示越大
  • 如果数据没有country或region字段,可通过省份/州名称匹配来区分各区域

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 10:50:39