【问题标题】:Setting plot limits in R with aspect ratio of 1在 R 中设置绘图限制,纵横比为 1
【发布时间】:2013-04-26 06:12:33
【问题描述】:

我正在尝试在极地立体投影中绘制北极地图。为此,我使用 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)。这会产生以下情节:

我尝试在绘制 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?

【问题讨论】:

  • pty = "s" 添加到第一个示例中的完整plot 调用或第二个示例中的plot(mapExtentPr, pch=NA) 调用是否有帮助?这里的要点是保持一个方形绘图区域,否则不可能强制 1asp = 1 并控制轴限制。即使绘图区域是方形的,您也无法完美地强制它。另一种选择是重新缩放绘图窗口,直到您大致获得所需的限制。
  • 感谢您的建议。不幸的是,添加pty = "s" 并不能完成这项工作,因为我需要一个非方形区域。重新缩放绘图窗口可以正常工作,但我正在寻找一个自动程序。我想我最终可能会使用 ggplotspplot...
  • 如果您想要 1 的纵横比 指定限制,则需要设置绘图的尺寸。这怎么可能?您可以在基础 R 中打开具有特定尺寸的新设备。如果 ggplotspplot 可以做到这一点,那么也许您可以在答案中解释如何?您可以在一天左右的时间内回答自己的问题。

标签: r map aspect-ratio clip axes


【解决方案1】:

我最终使用了这个:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-06
    • 2011-12-29
    • 2020-03-14
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多