【问题标题】:Remove PNG plot margins删除 PNG 绘图边距
【发布时间】:2015-11-22 23:22:23
【问题描述】:

我试图摆脱 SpatialPolygons 图的顶部和底部边距。 我尝试将边距设置为 c(0,0,0,0) 但这只会改变左右边距。

在 RStudio 中绘图时,上下边距为 0,但左右不是。

library(sp)

coords <- cbind(c(631145, 631757, 631928, 631664, 631579, 631281),
                c(6967640, 6967566, 6968027, 6967985, 6968141, 6968009))
poly <- Polygons(list(Polygon(coords)),"coords")
poly.sp <- SpatialPolygons(list(poly))

par(mar = rep(0, 4), xaxs='i', yaxs='i')
plot(poly.sp, bg="yellow")

png('poly.png')
par(mar = rep(0, 4), xaxs='i', yaxs='i')
plot(poly.sp, bg="yellow")
dev.off()

【问题讨论】:

    标签: r plot png sp


    【解决方案1】:

    我通过计算我要绘制的多边形的纵横比然后设置绘图的宽度和高度来解决这个问题。

    这可能不是最优雅的解决方案,但确实可以。

    library(sp)
    
    coords <- cbind(c(631145, 631757, 631928, 631664, 631579, 631281),
                    c(6967640, 6967566, 6968027, 6967985, 6968141, 6968009))
    poly <- Polygons(list(Polygon(coords)),"coords")
    poly.sp <- SpatialPolygons(list(poly))
    
    width <- poly.sp@bbox[3] - poly.sp@bbox[1]
    height <- poly.sp@bbox[4] - poly.sp@bbox[2]
    aspect <- height / width
    
    png('poly.png', width = 10, height = 10*aspect, units = 'in', res = 300)
    par(mar = rep(0, 4), xaxs='i', yaxs='i')
    plot(poly.sp, bg="yellow")
    dev.off()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2021-04-21
      • 2013-06-18
      相关资源
      最近更新 更多