0

I have a series of 11 graphs with a common legend. Therefore, I am putting the 11 graphs in a matrix layout so they can all be seen at once and want to put the legend in the 12th space.

How can I create a legend as its own plot in the matrix?

jpeg("Yearly live carbon.jpg", width = 2268, height = 1620, units = "px", pointsize = 28, quality = 85, bg = "white",type = "windows")           
layout(matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), 3,4, byrow=TRUE))

# Have a loop here to generate the graphics

dev.off()

The matrix layout will be filled with graphs for plots 1 thru 11, and I am trying to put a common legend in plot number 12. How can I set the legend as it's own plot in this?

thanks.

MacK
  • 2,132
  • 21
  • 29
kingmidaz
  • 53
  • 6
  • possible duplicate of [Plot a legend outside of the plotting area in base graphics?](http://stackoverflow.com/questions/3932038/plot-a-legend-outside-of-the-plotting-area-in-base-graphics) – agstudy Jul 11 '14 at 17:11
  • @agstudy I don't think this is a duplicate of that question. This is about using `layout`, where the final plot is entirely a legend, a plotting area issue would only come up if the margins are set too small. – Gregor Thomas Jul 11 '14 at 20:43
  • @agstudy but, duplicate of this question: https://stackoverflow.com/questions/10389967/common-legend-for-multiple-plots-in-r (although each user understood a different bit of what was needed) – user3386170 Feb 19 '18 at 19:29

1 Answers1

1

Just initialize a blank plot, something like

plot(0, 0, type = "n", ann = F, axes = F)

Then make a legend as normal

legend(0, 0, legend = c("a", "b"), pch = c(1, 5))
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294