在绘图子集之间添加额外间距

发布于 2024-09-17 10:59:36 字数 527 浏览 2 评论 0原文

我正在尝试以 3x2 布局将 6 个数字输出到一张图像中。我想在顶行和底两行之间放置额外的空间。使用 R 可以吗?我浏览了 par 和plot 的文档,似乎找不到合适的选项。

这是一些示例代码:

a = rnorm(100,100,10)
b = rnorm(100,100,10)

par(mfrow=c(3,2), oma=c(1,1,1,1), mar=c(2,2,2,2))
hist(a)
hist(b)
plot(a,b)
plot(a,b)
plot(a,b)
plot(a,b)

这是该代码输出的内容:


alt text


这是我希望它输出的内容(我修改了此内容)外部编辑器中的图像)。请注意顶行和底行之间的额外空间。


替代文本


I'm trying to output 6 figures into one image, in a 3x2 layout. I'd like to place extra space between the top row and the bottom two rows. Is this possible using R? I've looked through the documentation for par and plot and can't seem to find an appropriate option.

Here's some example code:

a = rnorm(100,100,10)
b = rnorm(100,100,10)

par(mfrow=c(3,2), oma=c(1,1,1,1), mar=c(2,2,2,2))
hist(a)
hist(b)
plot(a,b)
plot(a,b)
plot(a,b)
plot(a,b)

Here's what that code outputs:


alt text


Here's what I'd like it to output (I modified this image in an external editor). Note the extra space between the top row and bottom rows.


alt text


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

新人笑 2024-09-24 10:59:36

layout() 函数是你的朋友。例如,您可以定义一个绘图矩阵

1 2
3 4
5 6
7 8

,然后为第三个和第四个放置空图。或者只需坚持 6 并调用 par 在底部添加额外的间距。

The layout() function is your friend. You could for example define a plot matrix

1 2
3 4
5 6
7 8

and then put empty plots in for the third and fourth. Or just stick to six and call par to add extra spacing at the bottom.

情深已缘浅 2024-09-24 10:59:36

我可以想到三种方法:

1) 使用 mar 图形参数设置绘图边距

您可以使用 检索当前边距

currmar <- par()$mar

设置新边距

par("mar"=c(5, 4, 4, 2))

您可以使用 数字为下边距、左边距、上边距和右边距 (请参阅 ?par

您可以对每个绘图多次调用 par,因此您只能更改顶部绘图的底部边距。

2) 使用布局生成不均匀的布局网格(有关示例,请参见 ?layout

3) 将绘图保存为 .svg 或 .pdf,然后使用 Inkscape(或任何您喜欢的软件)移动绘图。

I can think of three ways:

1) Use the mar graphic parameter to set plot margin

You can retrieve current margins with

currmar <- par()$mar

You can set new margins with

par("mar"=c(5, 4, 4, 2))

with the for numbers being bottom, left, top and right margins (see ?par)

You can make multiple calls to par for each plot, so you can change the bottom margin only for the top plots.

2) Use layout to generate an uneven layout grid (see ?layout for examples)

3) Save the plot in .svg or .pdf and then use Inkscape (or whatever software you like) to move the plots.

泪眸﹌ 2024-09-24 10:59:36

我认为使用 mar 就是我的做法。然而,看起来,您希望所有的图都相同。因此,您需要在顶部和底部的每个地块上通过 mar 减去相同的量。
在您的情况下,可以使用以下数字:
1.行:par(mar=c(7,4,4,2))
2.行:par(mar=c(5,4,6,2))
3. row: par(mar=c(7,4,4,2))

这样所有的图都占据相同的高度。修改第一个和第三个数字,使每个图的第一个和第三个数字都相同,以实现此目的。
但是,需要注意的是:底行的图下方有一些额外的空白。

I think going with mar is the way I would do it. However, as it looks like, you want all the plots to be the same. Therefore, you need to have the same amount taken off by mar on every plot on top and bottom.
In your case one could use the following numbers:
1. row: par(mar=c(7,4,4,2))
2. row: par(mar=c(5,4,6,2))
3. row: par(mar=c(7,4,4,2))

This way all plots occupy the same height. Modifie the first and third number in such a way that they are the same for each plot to accomplish this.
However, on caveat: There is some extra white space below the plots in the bottom row.

栖迟 2024-09-24 10:59:36

关于绘图没有什么可添加的,但我在布局上挣扎了好几次,所以这里是可视化布局的方法(来自 此处) - 使用base-R:

# Margins area
par(oma=c(3,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1) # mar=c(b,l,t,r)

# Plot
plot(0:10, 0:10, xlab="X", ylab="Y") # type="n" hides the points

# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")

# Place text in the margins and label the margins, all in forestgreen  
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")  
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")  
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")  
box("figure", col="forestgreen")  

# Label the outer margin area and color it blue  
# Note the 'outer=TRUE' command moves us from the figure margins to the outer margins.  
mtext("Outer Margin Area", side=1, line=1, cex=2, col="blue", outer=TRUE)  
mtext("par(oma=c(b,l,t,r))", side=1, line=2, cex=1, col="blue", outer=TRUE)  
mtext("Line 0", side=1, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 1", side=1, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 2", side=1, line=2, adj=0.0, cex=1, col="blue", outer=TRUE)  
box("outer", col="blue")  

mtext("Line 0, side=2", side=2, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=2", side=2, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=3", side=3, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=3", side=3, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=4", side=4, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=4", side=4, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)

结果:
输入图片此处描述


有两个图:

par(mfrow = c(2, 1))
# First plot
# Margins area
par(oma=c(3,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1) # mar=c(b,l,t,r)

# Plot
plot(0:10, 0:10, xlab="X", ylab="Y") # type="n" hides the points

# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")

# Place text in the margins and label the margins, all in forestgreen  
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")  
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")  
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")  
box("figure", col="forestgreen")  

# Label the outer margin area and color it blue  
# Note the 'outer=TRUE' command moves us from the figure margins to the outer margins.  
mtext("Outer Margin Area", side=1, line=1, cex=2, col="blue", outer=TRUE)  
mtext("par(oma=c(b,l,t,r))", side=1, line=2, cex=1, col="blue", outer=TRUE)  
mtext("Line 0", side=1, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 1", side=1, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 2", side=1, line=2, adj=0.0, cex=1, col="blue", outer=TRUE)  
box("outer", col="blue")

mtext("Line 0, side=2", side=2, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=2", side=2, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=3", side=3, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=3", side=3, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=4", side=4, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=4", side=4, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)

# Second plot
plot(0:10, 0:10, xlab="X", ylab="Y", ) # type="n" hides the points

# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")

# Place text in the margins and label the margins, all in forestgreen  
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")  
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")  
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")  
box("figure", col="forestgreen")  
par(mfrow = c(1, 1))

结果:
输入图片此处描述

There is nothing to add concerning plots, but I stuggled several times with layout, so here is way to visualize the layout (from here) - using base-R:

# Margins area
par(oma=c(3,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1) # mar=c(b,l,t,r)

# Plot
plot(0:10, 0:10, xlab="X", ylab="Y") # type="n" hides the points

# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")

# Place text in the margins and label the margins, all in forestgreen  
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")  
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")  
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")  
box("figure", col="forestgreen")  

# Label the outer margin area and color it blue  
# Note the 'outer=TRUE' command moves us from the figure margins to the outer margins.  
mtext("Outer Margin Area", side=1, line=1, cex=2, col="blue", outer=TRUE)  
mtext("par(oma=c(b,l,t,r))", side=1, line=2, cex=1, col="blue", outer=TRUE)  
mtext("Line 0", side=1, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 1", side=1, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 2", side=1, line=2, adj=0.0, cex=1, col="blue", outer=TRUE)  
box("outer", col="blue")  

mtext("Line 0, side=2", side=2, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=2", side=2, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=3", side=3, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=3", side=3, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=4", side=4, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=4", side=4, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)

Result:
enter image description here


With two plots:

par(mfrow = c(2, 1))
# First plot
# Margins area
par(oma=c(3,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1) # mar=c(b,l,t,r)

# Plot
plot(0:10, 0:10, xlab="X", ylab="Y") # type="n" hides the points

# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")

# Place text in the margins and label the margins, all in forestgreen  
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")  
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")  
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")  
box("figure", col="forestgreen")  

# Label the outer margin area and color it blue  
# Note the 'outer=TRUE' command moves us from the figure margins to the outer margins.  
mtext("Outer Margin Area", side=1, line=1, cex=2, col="blue", outer=TRUE)  
mtext("par(oma=c(b,l,t,r))", side=1, line=2, cex=1, col="blue", outer=TRUE)  
mtext("Line 0", side=1, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 1", side=1, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)  
mtext("Line 2", side=1, line=2, adj=0.0, cex=1, col="blue", outer=TRUE)  
box("outer", col="blue")

mtext("Line 0, side=2", side=2, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=2", side=2, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=3", side=3, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=3", side=3, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=4", side=4, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=4", side=4, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)

# Second plot
plot(0:10, 0:10, xlab="X", ylab="Y", ) # type="n" hides the points

# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")

# Place text in the margins and label the margins, all in forestgreen  
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")  
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")  
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")  
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")  
box("figure", col="forestgreen")  
par(mfrow = c(1, 1))

Result:
enter image description here

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