【问题标题】:Add extra spacing between a subset of plots在图的子集之间添加额外的间距
【发布时间】:2010-09-03 19:03:55
【问题描述】:

我正在尝试以 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)

代码输出如下:



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



【问题讨论】:

    标签: r graphics plot spacing


    【解决方案1】:

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

    1 2
    3 4
    5 6
    7 8
    

    然后为第三个和第四个放置空地块。或者只是坚持六个并致电par 在底部添加额外的间距。

    【讨论】:

    • 谢谢 - layout() 对我来说是新的。像这样的东西似乎做我想要的: a=layout(matrix(c(1,2,0,0,3,4,5,6),4,2,byrow=TRUE), widths=c(1, 1), 高度=c(4,1,4,4))
    • 是的,布局非常棒,而且非常有用。这些天孩子们只谈论 ggplot2 所以它往往会被遗忘:) 很好地捕捉到 0 零以省略情节,而且你的宽度和高度完全正确。
    【解决方案2】:

    我能想到三种方法:

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

    您可以使用

    检索当前边距
    currmar <- par()$mar
    

    您可以设置新的边距

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

    for 数字是底部、左侧、顶部和右侧边距(请参阅?par

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

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

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

    【讨论】:

      【解决方案3】:

      我认为使用mar 是我会这样做的方式。但是,看起来,您希望所有图都相同。因此,您需要在顶部和底部的每个地块上从 mar 取出相同的数量。
      在您的情况下,可以使用以下数字:
      1. 行:par(mar=c(7,4,4,2))
      2.行:par(mar=c(5,4,6,2))
      3.行:par(mar=c(7,4,4,2))

      这样所有地块都占据相同的高度。修改第一个和第三个数字,使它们对于每个绘图都相同以完成此操作。 但是,需要注意的是:底行的图下方有一些额外的空白。

      【讨论】:

      • 不错。我已经使用 layout() 编写了我的解决方案,但这也可以。谢谢。
      【解决方案4】:

      关于情节没有什么可添加的,但我在布局方面遇到了几次困难,所以这是可视化布局的方法(来自here) - 使用 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))
      

      结果:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-01
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        • 2013-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多