【问题标题】:Common main title of a figure panel compiled with par(mfrow)用 par(mfrow) 编译的图形面板的常用主标题
【发布时间】:2013-01-17 14:18:25
【问题描述】:

我有一个与par(mfrow=c(2,2)) 一起绘制的 4 个图的汇编。我想为上面的 2 个图绘制一个通用标题,并为位于左右 2 个图之间的 2 个下面的面板绘制一个通用标题。

这可能吗?

【问题讨论】:

  • 这样做会掩盖“标题”,不是吗?为什么不希望它出现在所有情节的顶部?
  • 我的意思是 2 个上部地块的一个标题和 2 个下部地块的一个标题。我更正了我的 Q

标签: r graph plot title par


【解决方案1】:

应该可以工作,但您需要使用line 参数才能让它恰到好处:

par(mfrow = c(2, 2))
plot(iris$Petal.Length, iris$Petal.Width)
plot(iris$Sepal.Length, iris$Petal.Width)
plot(iris$Sepal.Width, iris$Petal.Width)
plot(iris$Sepal.Length, iris$Petal.Width)
mtext("My 'Title' in a strange place", side = 3, line = -21, outer = TRUE)

mtext 代表“边距文本”。 side = 3 说把它放在“顶部”边距。 line = -21 表示将位置偏移 21 行。 outer = TRUE 表示可以使用外边距区域。

要在顶部添加另一个“标题”,您可以使用 mtext("My 'Title' in a strange place", side = 3, line = -2, outer = TRUE) 来添加它

【讨论】:

  • 太棒了。谢谢你。我不知道mtext 可以使用负值。
  • @ECII,没问题。不过,总的来说,我认为您会使用layout 来解决此问题,如@Didzis 的回答所示。
【解决方案2】:

您可以使用函数layout() 并设置出现在两列中的两个绘图区域(参见matrix() 中重复的数字1 和3)。然后我使用plot.new()text() 设置标题。您可以使用边距和高度来获得更好的表现。

x<-1:10
par(mar=c(2.5,2.5,1,1))
layout(matrix(c(1,2,3,4,1,5,3,6),ncol=2),heights=c(1,3,1,3))
plot.new()
text(0.5,0.5,"First title",cex=2,font=2)
plot(x)
plot.new()
text(0.5,0.5,"Second title",cex=2,font=2)
hist(x)
boxplot(x)
barplot(x)

【讨论】:

  • 你在这里得到我的投票。我认为layout 绝对是要走的路,但也想显示mtext 选项。
【解决方案3】:

可以使用title(...) 和上面相同的参数来完成相同但粗体的操作:

title("My 'Title' in a strange place", line = -21, outer = TRUE)

【讨论】:

    【解决方案4】:

    这是另一种方法,使用来自this postline2user 函数。

    par(mfrow = c(2, 2))
    plot(runif(100))
    plot(runif(100))
    text(line2user(line=mean(par('mar')[c(2, 4)]), side=2), 
         line2user(line=2, side=3), 'First title', xpd=NA, cex=2, font=2)
    
    plot(runif(100))
    plot(runif(100))
    text(line2user(line=mean(par('mar')[c(2, 4)]), side=2), 
         line2user(line=2, side=3), 'Second title', xpd=NA, cex=2, font=2)
    

    这里,标题的位置比绘图的上边缘高 2 行,如 line2user(2, 3) 所示。我们通过将其相对于第 2 和第 4 个图的偏移量来居中,偏移量为左右边距组合宽度的一半,即mean(par('mar')[c(2, 4)])


    line2user 表示用户坐标系中轴的偏移量(行数),定义为:

    line2user <- function(line, side) {
      lh <- par('cin')[2] * par('cex') * par('lheight')
      x_off <- diff(grconvertX(0:1, 'inches', 'user'))
      y_off <- diff(grconvertY(0:1, 'inches', 'user'))
      switch(side,
             `1` = par('usr')[3] - line * y_off * lh,
             `2` = par('usr')[1] - line * x_off * lh,
             `3` = par('usr')[4] + line * y_off * lh,
             `4` = par('usr')[2] + line * x_off * lh,
             stop("side must be 1, 2, 3, or 4", call.=FALSE))
    }
    

    【讨论】:

    • 不错的@jbaums
    【解决方案5】:

    感谢@DidzisElferts 的回答,我刚刚找到了一个很棒的文档here

    试试下面的代码。我帮助了我很多了解发生了什么:

    def.par <- par(no.readonly = TRUE) # save default, for resetting...
    
    ## divide the device into two rows and two columns
    ## allocate figure 1 all of row 1
    ## allocate figure 2 the intersection of column 2 and row 2
    layout(matrix(c(1,1,0,2), 2, 2, byrow = TRUE))
    ## show the regions that have been allocated to each plot
    layout.show(2)
    
    ## divide device into two rows and two columns
    ## allocate figure 1 and figure 2 as above
    ## respect relations between widths and heights
    nf <- layout(matrix(c(1,1,0,2), 2, 2, byrow = TRUE), respect = TRUE)
    layout.show(nf)
    
    ## create single figure which is 5cm square
    nf <- layout(matrix(1), widths = lcm(5), heights = lcm(5))
    layout.show(nf)
    
    
    ##-- Create a scatterplot with marginal histograms -----
    
    x <- pmin(3, pmax(-3, stats::rnorm(50)))
    y <- pmin(3, pmax(-3, stats::rnorm(50)))
    xhist <- hist(x, breaks = seq(-3,3,0.5), plot = FALSE)
    yhist <- hist(y, breaks = seq(-3,3,0.5), plot = FALSE)
    top <- max(c(xhist$counts, yhist$counts))
    xrange <- c(-3, 3)
    yrange <- c(-3, 3)
    nf <- layout(matrix(c(2,0,1,3),2,2,byrow = TRUE), c(3,1), c(1,3), TRUE)
    layout.show(nf)
    
    par(mar = c(3,3,1,1))
    plot(x, y, xlim = xrange, ylim = yrange, xlab = "", ylab = "")
    par(mar = c(0,3,1,1))
    barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0)
    par(mar = c(3,0,1,1))
    barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE)
    
    par(def.par)  #- reset to default
    

    【讨论】:

      猜你喜欢
      • 2013-11-10
      • 2019-09-03
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多