删除PNG绘图边距

时间:2015-08-28 10:17:29

标签: r plot png sp

我试图摆脱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()

plot

1 个答案:

答案 0 :(得分:3)

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

这可能不是最优雅的解决方案,但它可以胜任。

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()
相关问题