尝试删除所有边距,以便绘图区域包含整个图形

发布于 2024-11-01 07:08:21 字数 1134 浏览 4 评论 0原文

我试图删除 R 中绘图的所有边距和“图形区域”,以便绘图区域包含整个图形设备。我认为下面的代码可以做到这一点,但我的图周围仍然有一个边框(左侧/底部较宽,顶部/右侧较细)。谢谢

par(oma=c(0, 0, 0, 0))
par(mar=c(0, 0, 0, 0))
par(plt=c(0, 1, 0, 1))

我想我会添加一张图片来显示我的进步。 xaxs 和 yaxs 几乎删除了顶部和右侧的所有边框 - 左侧和底部仍然有边框。

Rplot

我的脚本的相关部分如下。

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.

R plot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-11-08 07:08:21

一个问题是从根本上无法理解 plt 的作用。从 ?par 我们可以得到:

 ‘plt’ A vector of the form ‘c(x1, x2, y1, y2)’ giving the
      coordinates of the plot region as fractions of the current
      figure region.

因此,如果您执行 par(plt=c(1, 1, 1, 1)),那么您的绘图区域的大小为零,因此不会似乎不是要走的路。这是因为图形区域包含绘图区域。

该图似乎覆盖了整个区域,没有任何边距:

op <- par(mar = rep(0, 4))
plot(1:10)
par(op)

它覆盖得很好,您看不到轴或框:

“覆盖整个区域”

这假定默认值为 0 外边距 (oma)。这是您要找的吗?

我们可以看到,只需调整绘图边距,如上所述,我们还更改了 plt 参数作为副作用:

> par("plt")
[1] 0.1173174 0.9399106 0.1457273 0.8828467
> op <- par(mar = rep(0, 4))
> par("plt")
[1] 0 1 0 1
> par(op)
> par("plt")
[1] 0.1173174 0.9399106 0.1457273 0.8828467

表明简单地设置绘图边距足以获得包含整个绘图/图形区域设备。

当然,仍然有一些内部填充,以确保轴的范围略大于 x 和 y 坐标中的数据范围。但是您可以使用 xaxsyaxs 来控制它 --- 请参阅 ?par

更新: 正如OP所示他们试图在没有边距的情况下生成这种数字,我可以提供一个可重现的示例:

set.seed(1)
dat <- matrix(rnorm(100*100), ncol = 100, nrow = 100)

layout(matrix(1:2, ncol = 2))
image(dat)
op <- par(mar = rep(0, 4))
image(dat)
par(op)
layout(1)

它给出了比较:

comparison of default 和分别没有边距

和仅显示完整绘图区域:

覆盖完整绘图区域

One issue is fundamentally not getting what plt does. From ?par we have:

 ‘plt’ A vector of the form ‘c(x1, x2, y1, y2)’ giving the
      coordinates of the plot region as fractions of the current
      figure region.

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:

op <- par(mar = rep(0, 4))
plot(1:10)
par(op)

it covers it so well you can't see the axes or the box:

full region covered

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:

> par("plt")
[1] 0.1173174 0.9399106 0.1457273 0.8828467
> op <- par(mar = rep(0, 4))
> par("plt")
[1] 0 1 0 1
> par(op)
> par("plt")
[1] 0.1173174 0.9399106 0.1457273 0.8828467

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 and y coordinates. But you can control this with xaxs and yaxs --- 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:

set.seed(1)
dat <- matrix(rnorm(100*100), ncol = 100, nrow = 100)

layout(matrix(1:2, ncol = 2))
image(dat)
op <- par(mar = rep(0, 4))
image(dat)
par(op)
layout(1)

which gives for comparison:

comparison of default and no margins respectively

and showing just the full plotting region:

full plot region covered

若有似无的小暗淡 2024-11-08 07:08:21

尝试将剪辑区域参数“xpd”设置为 NA(剪辑到设备)。

par(xpd = NA)

Try setting the clip region parameter 'xpd' to NA ( clipped to device).

par(xpd = NA)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文