[R] Legend placement in barplots -- use horiz = TRUE

Martin Maechler maechler at stat.math.ethz.ch
Tue Jan 8 09:08:14 CET 2002


>>>>> "Uwe" == Uwe Ligges <ligges at statistik.uni-dortmund.de> writes:

    Uwe> david.whiting at ncl.ac.uk wrote:
    >> 
    >> Hi,
    >> 
    >> I am trying to use R to create some reasonably automatic output for
    >> routine monitoring. Some of these outputs are simple bar charts. I need to
    >> create something similar to an excel bar chart (yuk!, I know...:) which
    >> places the legend at the bottom of the chart.
    >> 
    >> barplot() place the legend over the chart and can potentially cover some
    >> of the detail (especially if the bars are numerous and thin). legend()
    >> allows me to position the legend but, as the example below shows, still
    >> only within the chart area. How can I place the legend below or to the
    >> right of a bar plot? Do I need to use plot.new() and build it up myself?
    >> 
    >> Example:
    >> --------
    >> barplot(1:3, legend.text = c("cats", "dogs", "cows"))
    >> legend(1,0.5, c("cats", "dogs", "cows"))
    >> legend(1,0.1, c("cats", "dogs", "cows"))

    Uwe> The solution is to play around with the magic of par():

    Uwe> par("mar")	# borders, see ?par
    Uwe>  # [1] 5.1 4.1 4.1 2.1
    Uwe>  # Now increase size of the bottom-border:
    Uwe> par(mar=c(10.1, 4.1, 4.1, 2.1))
    Uwe>  # And set xpd=TRUE, so all plotting is clipped to the figure 
    Uwe>  # (not the plot) region:
    Uwe> par(xpd=TRUE)
    Uwe>  # Your barplot:
    Uwe> bp <- barplot(1:3) 
    Uwe>  # Text for the legend:
    Uwe> legend.text <- c("cats", "dogs", "cows")
    Uwe>  # And plot the legend below the existing plot:
    Uwe> legend(1,-0.5, legend.text, fill=heat.colors(length(legend.text)))
    Uwe>  # Or much nicer, center the legend:
    Uwe> legend(mean(range(bp)), -0.5, legend.text, xjust = 0.5,
    Uwe>        fill=heat.colors(length(legend.text)))

  (I've corrected two typos)

Note that it becomes quite a bit nicer to use the (relatively new)
argument `` horiz = TRUE '' with legend() -- you also need much
less margin space : 


## Example again :

 ## increase size of the bottom-border:
 par(mar= c(6, 4, 4, 2) + .1)
 ## Set xpd=TRUE, so all plotting is clipped to the figure 
 ## (not the plot) region:
 par(xpd=TRUE)
 ## Your barplot:
 bp <- barplot(1:3) 
 ## Text for the legend:
 legend.text <- c("cats", "dogs", "cows")
 ## And plot the legend below the existing plot -- centering it
 legend(mean(range(bp)), -0.3, legend.text, xjust = 0.5,
	fill=heat.colors(length(legend.text)), horiz = TRUE)


Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list