20

I would like to know how to provide a common title and legend for combined plots in R. I have four plots which I have combined into one. Each plot has its own Title. I want to specify a common Title on the top center and common legend on the top left corner of the combined plot. I generated the combined plot using par(). I have provided my plot belowCombined plot

user3897
  • 551
  • 2
  • 5
  • 14

1 Answers1

22

You can use the oma parameter to increase the outer margins, then add the main title with mtext, and try to position the legend by hand.

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)
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78