Skip to content
Prev 25215 / 398502 Next

trouble positioning legends on barplot written to a file

Hi

It is possible to draw the legend outside the plot, but it is
inconvenient because the location is specified in terms of the scales on
the x- and y-axes.  Positioning therefore becomes a matter of trial and
error.  In the example below, I create space for the legend using
par(oma) [again the size of the margin is a matter of guess work] and
make sure that the legend is not clipped out of existence by specifying
par(xpd=NA) ...

   # generate some dummy data that sums to 100 in each col
   data(VADeaths)
   temp <- function(x) {
     sample(seq(10, 30, 5))
   }
   dummy <- apply(VADeaths, 2, temp)
   # Plotting ...
   par(oma=c(0, 0, 5, 0), xpd=NA)
   colours <- 1:5
   barplot(dummy, col=colours)
   legend(par("usr")[1], 110,
          row.names(VADeaths), fill=colours,
          yjust=0, horiz=TRUE)

You might have other colours you would prefer to use :)

Hope that helps.

Paul
Tom Arnold wrote: