R - Common title and legend for combined plots

后端 未结 1 890
长情又很酷
长情又很酷 2020-12-13 10:26

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

相关标签:
1条回答
  • 2020-12-13 10:49

    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)
    
    0 讨论(0)
提交回复
热议问题