删除PNG绘图边距 [英] Remove PNG plot margins

查看:109
本文介绍了删除PNG绘图边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试摆脱SpatialPolygons图的顶部和底部边距. 我尝试将页边距设置为c(0,0,0,0),但这只会更改左侧和右侧页边距.

I am trying to get rid of the top and bottom margins of a SpatialPolygons plot. I have tried setting the margins to c(0,0,0,0) but this only changes the left and right margins.

在RStudio中进行绘制时,顶部和底部边距为0,而左侧和右侧则不为

When plotting in RStudio, the top and bottom margins are 0 but the left and right are not.

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

推荐答案

我解决了这个问题,方法是计算要绘制的多边形的纵横比,然后设置绘制的宽度和高度.

I solved the problem by calculating the aspect ratio of the polygon that I am trying to plot and then setting the plot width and height.

这可能不是最优雅的解决方案,但可以完成工作.

This might not be the most elegant solution but it does the job.

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

这篇关于删除PNG绘图边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆