尝试删除所有边距,以便绘图区域包含整个图形 [英] trying to remove all margins so that plot region comprises the entire graphic

查看:33
本文介绍了尝试删除所有边距,以便绘图区域包含整个图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除 R 中绘图的所有边距和图形区域",以便绘图区域包含整个图形设备.我认为下面的代码可以做到,但我的情节周围仍然有一个边框(左/下更宽,上/右更薄).谢谢

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

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

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

推荐答案

一个问题是根本没有得到 plt 的作用.从 ?par 我们有:

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.

所以如果你执行 par(plt=c(1, 1, 1, 1)),你的绘图区域的大小为零,所以这似乎不是要走的路.这是因为图形区域包含绘图区域.

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:

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

This assumes the default for 0 outer margin (oma). Is this what you were looking for?

我们可以看到,只是调整了图边距,如上,我们还改变了plt参数作为副作用:

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.

当然,仍然有一些内部填充,以确保轴的范围略大于 xy 中的数据范围坐标.但是你可以用 xaxsyaxs 来控制这个——见 ?par

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

更新: 由于 OP 已经展示了他们试图在没有边距的情况下生产的那种数字,我可以提供一个可重复的示例:

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)

用于比较:

并仅显示完整的绘图区域:

and showing just the full plotting region:

这篇关于尝试删除所有边距,以便绘图区域包含整个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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