[R] Plotting legend outside of multiple panels

Patrick Lenon lenon at fstrf-wi.org
Tue May 4 15:22:04 CEST 2010


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.

-- 
Patrick Lenon
Database Engineer
Frontier Science and Technology Foundation

(608)441-2947



More information about the R-help mailing list