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

R语言绘制地图时如何将五大湖设置为与海洋相同的颜色

解决方法

不需要更换现有底图,核心思路是叠加单独的五大湖矢量图层,覆盖原有陆地填充色即可实现和海洋同色的效果。
你当前使用的国家、州级矢量数据默认将五大湖归入陆地区域,因此会和陆地一样填充为灰色。我们可以调用rnaturalearth自带的全球湖泊数据集,提取五大湖范围后叠加到地图最上层,将其填充色设置为和海洋背景一致的aliceblue即可。

完整修改后代码

library(cowplot)
library(googleway)
library(ggplot2)
library(ggrepel)
library(ggspatial)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)

world <- ne_countries(scale = "medium", returnclass = "sf")
usa <- st_as_sf(maps::map("state", fill=TRUE, plot =FALSE),
                crs = 4269)
# 新增:读取全球湖泊数据并筛选五大湖
lakes <- ne_download(type = "lakes", scale = "medium", returnclass = "sf")
great_lakes <- lakes[lakes$name %in% c("Lake Superior", "Lake Michigan", "Lake Huron", "Lake Erie", "Lake Ontario"), ]

ggplot(data = world) +
  geom_sf(color = "black", fill = "gray") +
  geom_sf(data = usa, color = "black", fill = "gray") +
  # 新增:叠加五大湖图层,填充色与海洋背景一致
  geom_sf(data = great_lakes, fill = "aliceblue", color = "black") +
  xlab("Longitude") + ylab("Latitude") +
  coord_sf(xlim = c(-123, -69), ylim = c(25, 49), expand = TRUE) +
  annotation_scale(location = "br", width_hint = 0.5, text_cex = 1) +
  annotation_north_arrow(location = "br", which_north = "true", 
                         pad_x = unit(0.15, "in"), pad_y = unit(0.3, "in"),
                         style = north_arrow_fancy_orienteering) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black"),
        axis.text.y = element_text(size = 14, color = "black"),
        panel.grid.major = element_line(color = gray(0.5), linetype = "dashed", size = 0.5),
        panel.background = element_rect(fill = "aliceblue"))

注意事项

  • 如果运行时提示缺少湖泊数据集,可先执行以下命令安装依赖包:
    install.packages("rnaturalearthhires", repos = "https://ropensci.r-universe.dev")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 22:45:03