用par(mfrow)编译的数字面板的通用主标题

ECI*_*CII 47 plot r graph title par

我有4个绘图的汇编par(mfrow=c(2,2)).我想为上面的2个图绘制一个共同的标题,并在下面的2个面板中绘制一个共同的标题,这些标题位于2个左右图之间.

这可能吗?

A5C*_*2T1 47

应该可行,但你需要使用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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

mtext代表"保证金文字".side = 3说把它放在"顶部"边缘.line = -21说要将展示位置抵消21行.outer = TRUE说使用外边缘区域是可以的.

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

  • @ECII,没问题.但总的来说,我认为你会使用`layout`来解决这个问题,正如@ Didzis的回答所示. (2认同)

Did*_*rts 32

您可以使用该功能layout()并设置两个列中出现的两个绘图区域(请参阅重复的数字1和3 matrix()).然后我用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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 你在这里得到我的投票.我认为`layout`绝对是可行的方法,但也希望显示`mtext`选项. (5认同)

Ufo*_*fos 13

使用title(...)与上述相同的参数可以使用相同但粗体显示:

title("My 'Title' in a strange place", line = -21, outer = TRUE)
Run Code Online (Sandbox Code Playgroud)


jba*_*ums 7

这是另一种方法,使用这篇文章中line2user函数.

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

centered_titles

这里,标题位于比图的上边缘高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))
}
Run Code Online (Sandbox Code Playgroud)