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


as title says: how can plot legend outside plotting area when using base graphics?

i thought fiddling around layout , produce empty plot contain legend, interested in way using base graph facilities , e.g., par(mar = ) space on right of plot legend.


here 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 said, legend outside plotting area (e.g., right of graph/plot.

maybe need par(xpd=true) enable things drawn outside plot region. if main plot bty='l' you'll have space on right legend. clipped plot region, par(xpd=true) , bit of adjustment can legend far right can go:

 set.seed(1) # same random numbers  par(xpd=false) # default   plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='l')  # legend gets clipped:  legend(2.8,0,c("group a", "group b"), pch = c(1,2), lty = c(1,2))   # turn off clipping:  par(xpd=true)  legend(2.8,-1,c("group a", "group b"), pch = c(1,2), lty = c(1,2)) 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -