Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Common title and legend for combined plots

Tags:

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

like image 875
user3897 Avatar asked Jan 04 '12 22:01

user3897


People also ask

How do I add a title to a legend in R?

In case you need to add a title to the legend, in order to add some description of the elements of the legend, you can use the title argument. Note that you can customize the color of the text with the title. col argument and that you can make a horizontal adjustment of the title with the title.

Can you put legend outside of plot in R?

In order to draw our legend outside of the plotting area, we can use a combination of the “topright” argument and an additional specification of inset. The “topright” argument specifies that the legend should be in the upper right corner of the graph.


1 Answers

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)
like image 88
Vincent Zoonekynd Avatar answered Oct 04 '22 23:10

Vincent Zoonekynd