简体   繁体   English

使用 par 设置 png 绘图设备的绘图边距

[英]Set plot margin of png plot device using par

I've created a choropleth of Brazil.我已经创建了巴西的 choropleth。 When saving the plot in .png, the upper and the lower part of the plot are lost (covered).将绘图保存为 .png 时,绘图的上部和下部会丢失(被覆盖)。 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()

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 (ie replace c(0,-30) by c(5,-33)):为了在不丢失地图上部和下部的情况下保存绘图,我必须更改坐标以在底部和顶部添加空白(即用 c(5,-33) 替换 c(0,-30) ):

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?知道如何解决这个问题吗?

Edit:编辑:

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,我很抱歉,因为我现在意识到我不了解问题的根源,因此没有尽我所能解释,

It seems that png resets the outer margin of the plot.似乎 png 重置了情节的外边距。 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.因此,即使我设置了 omi=c(0,0,0,0),这些也不是 png 命令在保存绘图时使用的值。 The solution was to set the plot parameters after calling png so save the figure.解决方案是在调用 png 后设置绘图参数,以便保存图形。

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

From Details in ?par :?par详细信息

Each device has its own set of graphical parameters.每个设备都有自己的一组图形参数。

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.因此,即使我已将绘图的外边距设置为par ( omi = c(0,0,0,0) ),但在保存绘图时,这些值会被png的参数覆盖。

The solution was to set the margin parameters in par after calling png解决方案是调用pngpar设置边距参数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM