Skip to content
Prev 219496 / 398500 Next

Plotting legend outside of multiple panels

Another solution I've used is to set up an additional layout space and 
put the legend in there with no graph.  You print a blank dummy graph 
and then add the legend to the "blank" layout panel like so:

if (floatLegend) {
      # We want to float the legend independently
      # so we have to add it here as the only visible component of a
      # dummy graph.

    legText <- yourLegendNames
     
        # create a blank graph -- automatically scales -1 to +1 on both axes
    op <- par(mar=plotMargins)
    tsFake <- barplot(0,0, axes=FALSE)
    legend(x=1, y=0,
           legend=legText,
        # set fill, angle, density to match your real graph scheme
           xjust=1,
           yjust=0.5)
    par(op)
  }

Hope that helps.