使用参数设置png绘图设备的绘图边距 [英] Set plot margin of png plot device using par

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

问题描述

我创造了巴西的choropleth。将图保存为.png时,图的上部和下部会丢失(覆盖)。以下是保存该图的行。

I've created a choropleth of Brazil. When saving the plot in .png, the upper and the lower part of the plot are lost (covered). Here are the lines to save the plot.

plot.new()
par(omi=c(0,0,0,0), mgp=c(0,0,0),mar=c(0,0,0,0) , family = "D")
par(mfrow=c(1,1),cex=1,cex.lab = 0.75,cex.main=0.2,cex.axis=0.2)
png(filename = "map_cons_g.png", width = 6,height = 6, units = "in", res = 600)
plot(c(-75,-35),c(0,-30),type="n",axes=FALSE,xlab="",ylab="",asp=1.2)
plot(Brazil,col=cols[Brazil$Cons.g_ri],add=TRUE,border="black",lwd=0.5)
dev.off()

要保存图而又不丢失地图的上下部分,我必须更改坐标以在其上添加空白底部和顶部(即用c(5,-33)替换c(0,-30)):

For saving the plot without losing the upper and the lower part of the map, I must change the coordinates to add white space at the bottom and at the top (i.e. replace c(0,-30) by c(5,-33)):

plot.new()
par(omi=c(0,0,0,0), mgp=c(0,0,0),mar=c(0,0,0,0) , family = "D")
par(mfrow=c(1,1),cex=1,cex.lab = 0.75,cex.main=0.2,cex.axis=0.2)
png(filename = "map_cons_g.png", width = 6,height = 6, units = "in", res = 600)
plot(c(-75,-35),c(5,-33),type="n",axes=FALSE,xlab="",ylab="",asp=1.2)
plot(Brazil,col=cols[Brazil$Cons.g_ri],add=TRUE,border="black",lwd=0.5)
dev.off()

在某种意义上说,我可以看到完整的地图,但是这样,地图就不会使用图中的所有可用区域。保存绘图时,该图的上部和下部似乎有一定的余量。我从来没有遇到过其他类型的情节的问题。

This works in the sense that I can see the full map but the map then does not use all the available area in the figure. It seems that there are some margin in the upper and the lower part of the figure when saving the plot. I've never had that problem with other types of plot.

对不起,我没有足够的声誉来发布图像来向您展示地图的样子。

Sorry, I don't have enough "reputation" to post images to show you how the maps look like.

有什么解决方法的想法吗?

Any idea of how to fix this?

编辑:

下面的注释使我对问题进行了更多搜索,终于找到了解决方法。我很抱歉,因为我现在意识到我不了解问题的根源,因此没有尽我所能解释,

The comments below got me searching more into the problem and I finally found a fix. I apologize as I now realized that I did not understand the source of the problem and thus did not explain as best as I could have,

似乎png重置了外部情节的边缘。因此,即使我设置了omi = c(0,0,0,0),这些也不是png命令在保存绘图时使用的值。解决方案是在调用png后设置绘图参数,以便保存图形。

It seems that png resets the outer margin of the plot. Thus, even though I had set omi=c(0,0,0,0), those were not the value used by the png command in saving the plot. The solution was to set the plot parameters after calling png so save the figure.

plot.new()
png(filename = "map_cons_g.png", width = 6,height = 6, units = "in", res = 600)
par(omi=c(0,0,0,0), mgp=c(0,0,0),mar=c(0,0,0,0) , family = "D")
par(mfrow=c(1,1),cex=1,cex.lab = 0.75,cex.main=0.2,cex.axis=0.2)
plot(c(-75,-35),c(5,-33),type="n",axes=FALSE,xlab="",ylab="",asp=1.2)
plot(Brazil,col=cols[Brazil$Cons.g_ri],add=TRUE,border="black",lwd=0.5)
dev.off()


推荐答案

来自详细信息?par 中:


每个设备都有自己的一组图形参数。

Each device has its own set of graphical parameters.

因此,即使我将图的外部边距设置为 par omi = c(0,0,0,0)),则在保存图时,这些值被 png 中的参数覆盖。

Thus, even though I had set the outer margin of the plot in par (omi = c(0,0,0,0)), those value were overwritten by the parameters in png when saving the plot.

解决方案是在<$ c中设置保证金参数$ c> par 之后调用 png

plot.new()

# first open png device...
png(filename = "map_cons_g.png", width = 6,height = 6, units = "in", res = 600)

# ...then set par
par(omi = c(0,0,0,0), mgp = c(0,0,0), mar = c(0,0,0,0), family = "D")
par(mfrow = c(1, 1), cex = 1, cex.lab = 0.75, cex.main = 0.2, cex.axis = 0.2)

plot(c(-75, -35), c(5, -33),  type = "n", axes = FALSE, xlab = "", ylab = "", asp = 1.2)
plot(Brazil, col = cols[Brazil$Cons.g_ri], add = TRUE, border = "black", lwd = 0.5)
dev.off()

这篇关于使用参数设置png绘图设备的绘图边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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