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

如何在R中绘制圆形地图投影并为乌拉圭区域添加矩形框

在R中用ggplot绘制带乌拉圭矩形框的世界地图

1. 加载所需R包

先安装并加载绘图和地图相关的包:

install.packages(c("ggplot2", "maps", "ggspatial")) # 首次运行需安装
library(ggplot2)
library(maps)
library(ggspatial)

2. 绘制带边界的基础世界地图

使用map_data("world")获取标准世界地图数据,通过geom_polygon的color参数添加国家/海岸线边界,解决“地球没有边界”的问题:

world_map <- map_data("world")

ggplot() +
  geom_polygon(data = world_map, aes(x = long, y = lat, group = group), 
               fill = "lightblue", color = "black", size = 0.2) +
  coord_fixed(ratio = 1.3) # 维持地图比例,避免拉伸变形

3. 添加乌拉圭区域的矩形框

乌拉圭的大致经纬度范围为:西经53°-58°(对应R中经度值-58到-53)、南纬30°-35°(对应R中纬度值-35到-30)。用geom_rect绘制醒目的标记框:

ggplot() +
  geom_polygon(data = world_map, aes(x = long, y = lat, group = group), 
               fill = "lightblue", color = "black", size = 0.2) +
  geom_rect(aes(xmin = -58, xmax = -53, ymin = -35, ymax = -30), 
            fill = NA, color = "red", size = 1.2) + # 空心红框突出目标区域
  coord_fixed(ratio = 1.3) +
  labs(title = "世界地图 - 乌拉圭区域标记", x = "经度", y = "纬度") +
  theme_bw() # 切换为简洁的白底主题

可选:放大聚焦乌拉圭区域

如果需要更清晰地展示乌拉圭周边,调整坐标范围即可:

ggplot() +
  geom_polygon(data = world_map, aes(x = long, y = lat, group = group), 
               fill = "lightblue", color = "black", size = 0.2) +
  geom_rect(aes(xmin = -58, xmax = -53, ymin = -35, ymax = -30), 
            fill = NA, color = "red", size = 1.2) +
  coord_fixed(ratio = 1.3, xlim = c(-65, -45), ylim = c(-40, -25)) + # 限定显示的经纬度范围
  labs(title = "乌拉圭区域标记", x = "经度", y = "纬度") +
  theme_bw()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 23:37:45