【发布时间】:2023-01-30 21:03:57
【问题描述】:
我需要在 R 中制作一张与这张地图类似的地图,但在乌拉圭绘制了一个矩形: enter image description here
我正在尝试使用 ggplot,但地球没有边框,我找不到指示坐标的方法来绘制我感兴趣的矩形。这是我在 web 之后获得的:
任何帮助表示赞赏!谢谢
【问题讨论】:
标签: r ggplot2 maps map-projections
我需要在 R 中制作一张与这张地图类似的地图,但在乌拉圭绘制了一个矩形: enter image description here
我正在尝试使用 ggplot,但地球没有边框,我找不到指示坐标的方法来绘制我感兴趣的矩形。这是我在 web 之后获得的:
任何帮助表示赞赏!谢谢
【问题讨论】:
标签: r ggplot2 maps map-projections
您可以使用 ggplot2 库在 R 中绘制圆形地图投影,并在特定区域周围绘制一个矩形。这是一个基本示例:
library(ggplot2)
# Load data
data(world)
# Create a round projection
p <- ggplot() +
geom_sf(data = world, fill = "gray80") +
coord_sf(crs = "+proj=longlat +datum=WGS84") +
theme_void()
# Define the rectangle coordinates
rect_coords <- data.frame(
xmin = c(-120, -120),
xmax = c(-60, -60),
ymin = c(30, 50),
ymax = c(40, 70)
)
# Add rectangle to plot
p + geom_rect(
data = rect_coords,
aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
fill = "red",
alpha = 0.5
)
【讨论】: