rplotlegend

Plot a legend outside of the plotting area in base graphics?


As the title says: How can I plot a legend outside the plotting area when using base graphics?

I thought about fiddling around with layout and produce an empty plot to only contain the legend, but I would be interested in a way using just the base graph facilities and e.g., par(mar = ) to get some space on the right of the plot for the legend.


Here an example:

plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
legend(1,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))

produces:

alt text

But as said, I would like the legend to be outside the plotting area (e.g., to the right of the graph/plot.


Solution

  • Maybe what you need is par(xpd=TRUE) to enable things to be drawn outside the plot region. So if you do the main plot with bty='L' you'll have some space on the right for a legend. Normally this would get clipped to the plot region, but do par(xpd=TRUE) and with a bit of adjustment you can get a legend as far right as it can go:

     set.seed(1) # just to get the same random numbers
     par(xpd=FALSE) # this is usually the default
    
     plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')
     # this legend gets clipped:
     legend(2.8,0,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
    
     # so turn off clipping:
     par(xpd=TRUE)
     legend(2.8,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))