gpt4 book ai didi

r - 在纵横比为 1 的 R 中设置绘图限制

转载 作者:行者123 更新时间:2023-12-04 03:38:38 25 4
gpt4 key购买 nike

我正在尝试在极地立体投影中绘制北极 map 。为此,我使用 rGDAL 包将一个已经投影的 shapefile 导入到 R 中:

ds <- readOGR(dsn = path, layer = "10m_coastline_ps")

这将创建一个 SpatialLinesDataFrame。现在,当我使用 plot() 函数绘制此 SpatialLinesDataFrame 时,我无法使用 xlimylim 设置绘图限制 参数:

plot(ds, xlim=c(-1700000,1700000), ylim=c(-4000000,800000), axes=T)

plot() 函数自动将纵横比设置为 1 并强制限制轴(如本文所述:igraph axes xlim ylim plot incorrectly)。这会产生以下情节:

enter image description here

我尝试在绘制 SpatialLinesDataFrame 之前初始化绘图范围,如下所示:

mapExtentPr <- rbind(c(-1700000,-4000000), c(1700000,800000))
plot(mapExtentPr, pch=NA)
plot(ds, xlim=c(-1700000,1700000), ylim=c(-4000000,800000), axes=T, asp = 1, add=TRUE)

这设置了正确的限制但不保持纵横比。我也尝试过使用没有效果的 clip() 函数。

有没有办法在保持纵横比为 1 的同时精确设置绘图限制(不使用 ggplotspplot)?

注意:我已经检查过这个答案:Is it possible to change the ylim and xlim when the plot has already been drawn?

最佳答案

我最终使用了这个:

xmin <- -1700000
xmax <- 1700000
ymin <- -4000000
ymax <- 100000

asratio = (ymax-ymin)/(xmax-xmin)

png("greenland_map.png",width=500,height=ceiling(500*asratio))

#plot basemap
plot(ne_10m_coastline_ps,xlim=c(xmin,xmax), ylim=c(ymin,ymax),
axes=TRUE,asp=1,xaxs="i",yaxs="i")

par(usr=c(xmin,xmax,ymin,ymax))

dev.off()

map with exact axis limits

关于r - 在纵横比为 1 的 R 中设置绘图限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15640547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com