尝试删除所有边距,以便绘图区域包含整个图形
我试图删除 R 中绘图的所有边距和“图形区域”,以便绘图区域包含整个图形设备。我认为下面的代码可以做到这一点,但我的图周围仍然有一个边框(左侧/底部较宽,顶部/右侧较细)。谢谢
par(oma=c(0, 0, 0, 0))
par(mar=c(0, 0, 0, 0))
par(plt=c(0, 1, 0, 1))
我想我会添加一张图片来显示我的进步。 xaxs 和 yaxs 几乎删除了顶部和右侧的所有边框 - 左侧和底部仍然有边框。
我的脚本的相关部分如下。
png("Test.png",
width = 256, height = 256,
units = "px", pointsize = 6.4,
bg = "black", res = NA)
par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
smoothScatter(lhb$px, lhb$pz, nrpoints=0, xlim=c(-3,3), ylim=c(0,5),
main="", xlab="", ylab="", axes=FALSE,
colramp=colorRampPalette(c("black", "#202020", "#736AFF", "cyan", "yellow", "#F87431", "#FF7F00", "red", "#7E2217"))
)
segments(.83, 1.597, .83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 1.597, -.83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 3.436, .83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 1.597, .83, 1.597, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
dev.off()
I am trying to remove all margins and the "figure region" of a plot in R, so that the plot region comprises the entire graphic device. I thought the code below would do it, but there is still a border around my plot (wider on left/bottom, thinner on top/right). Thanks
par(oma=c(0, 0, 0, 0))
par(mar=c(0, 0, 0, 0))
par(plt=c(0, 1, 0, 1))
Thought I would add a picture to show my progress. The xaxs and yaxs removed nearly all border from the top and right- there is still a border on the left and bottom.
The relevant portion of my script is below.
png("Test.png",
width = 256, height = 256,
units = "px", pointsize = 6.4,
bg = "black", res = NA)
par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
smoothScatter(lhb$px, lhb$pz, nrpoints=0, xlim=c(-3,3), ylim=c(0,5),
main="", xlab="", ylab="", axes=FALSE,
colramp=colorRampPalette(c("black", "#202020", "#736AFF", "cyan", "yellow", "#F87431", "#FF7F00", "red", "#7E2217"))
)
segments(.83, 1.597, .83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 1.597, -.83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 3.436, .83, 3.436, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
segments(-.83, 1.597, .83, 1.597, col = par("fg"), lty = par("lty"), lwd = par("lwd"))
dev.off()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个问题是从根本上无法理解
plt
的作用。从?par
我们可以得到:因此,如果您执行
par(plt=c(1, 1, 1, 1))
,那么您的绘图区域的大小为零,因此不会似乎不是要走的路。这是因为图形区域包含绘图区域。该图似乎覆盖了整个区域,没有任何边距:
它覆盖得很好,您看不到轴或框:
这假定默认值为 0 外边距 (
oma
)。这是您要找的吗?我们可以看到,只需调整绘图边距,如上所述,我们还更改了
plt
参数作为副作用:表明简单地设置绘图边距足以获得包含整个绘图/图形区域设备。
当然,仍然有一些内部填充,以确保轴的范围略大于 x 和 y 坐标中的数据范围。但是您可以使用
xaxs
和yaxs
来控制它 --- 请参阅?par
更新: 正如OP所示他们试图在没有边距的情况下生成这种数字,我可以提供一个可重现的示例:
它给出了比较:
和仅显示完整绘图区域:
One issue is fundamentally not getting what
plt
does. From?par
we have:So your plot region is of zero size if you do
par(plt=c(1, 1, 1, 1))
, so that doesn't seem to be the way to go. This is because the figure region contains the plot region.This plot seems to cover the entire region, without any margins:
it covers it so well you can't see the axes or the box:
This assumes the default for 0 outer margin (
oma
). Is this what you were looking for?We can see that just adjusting the plot margins, as above, we also change the
plt
parameter as a side effect:indicating that simply setting the plot margins is sufficient to get a plot/figure region encompassing the entire device.
Of course, there is still a bit of internal padding that insures the ranges of the axes are slightly large than the range of the data in both the
x
andy
coordinates. But you can control this withxaxs
andyaxs
--- see?par
Update: As the OP has shown the sort of figure they are trying to produce without margins, I can provide a reproducible example:
which gives for comparison:
and showing just the full plotting region:
尝试将剪辑区域参数“xpd”设置为 NA(剪辑到设备)。
par(xpd = NA)
Try setting the clip region parameter 'xpd' to NA ( clipped to device).
par(xpd = NA)