R - 合并绘图的通用标题和图例

20
我想知道如何在R中为合并的图形提供共同的标题和图例。我已经将四个图形组合成一个图形,每个图形都有自己的标题。我希望在组合图的顶部中央指定共同的标题,在左上角指定共同的图例。我使用了par()函数生成了组合图。我已提供我的图形,如下所示 Combined plot
1个回答

22

您可以使用oma参数来增加外边距,接着使用mtext添加主标题, 并尝试手动定位图例。

op <- par(
  oma=c(0,0,3,0),# Room for the title and legend
  mfrow=c(2,2)
)
for(i in 1:4) {
  plot( cumsum(rnorm(100)), type="l", lwd=3,
  col=c("navy","orange")[ 1+i%%2 ], 
  las=1, ylab="Value",
  main=paste("Random data", i) )
}
par(op) # Leave the last plot
mtext("Main title", line=2, font=2, cex=1.2)
op <- par(usr=c(0,1,0,1), # Reset the coordinates
          xpd=NA)         # Allow plotting outside the plot region
legend(-.1,1.15, # Find suitable coordinates by trial and error
  c("one", "two"), lty=1, lwd=3, col=c("navy", "orange"), box.col=NA)

2
欢迎,Vincent!在我学习R的早期阶段,你的网页对我非常有用。很好地阐述了管理边距的方法。 - IRTFM
我该如何将“主标题”放到底部? - d.putto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接