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

tag*_*ius 13 plot r

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

par(oma=c(0, 0, 0, 0))
par(mar=c(0, 0, 0, 0))
par(plt=c(0, 1, 0, 1))
Run Code Online (Sandbox Code Playgroud)

以为我会添加一张图片来展示我的进步.xaxs和yaxs几乎从顶部和右边移除了所有边界 - 左边和底部仍然有一个边框.

R图

我脚本的相关部分如下.

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()
Run Code Online (Sandbox Code Playgroud)

Rei*_*son 15

一个问题基本上没有得到什么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.
Run Code Online (Sandbox Code Playgroud)

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

这个情节似乎覆盖了整个地区,没有任何边缘:

op <- par(mar = rep(0, 4))
plot(1:10)
par(op)
Run Code Online (Sandbox Code Playgroud)

它覆盖得很好,你看不到轴或盒子:

覆盖整个地区

假定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
Run Code Online (Sandbox Code Playgroud)

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

当然,仍然有一些内部填充确保轴的范围略大于xy坐标中的数据范围.但你可以控制这个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)
Run Code Online (Sandbox Code Playgroud)

这给出了比较:

分别比较默认和无边距

并显示完整的绘图区域:

完整的地块覆盖

  • 这看起来是正确的,设置par(mar = c(0,0,0,0),xaxs ='i',yaxs ='i'),就像在上面的脚本中完全摆脱了顶部和右侧.现在我只需要摆脱左/下 (2认同)
  • 就像绘制空间POlygonsDataFrame一样。解决的方法是在`plot.window(...)`中添加`xaxs =“ i”,yaxs =“ i”`。 (2认同)