26

I am using R and Latex together to draw some plots and am trying to make a common legend for all of them.

I have six separate plots on the same page. I made each plot separately in R and then displayed them on the same page using \includegraphics in Latex.

Each graph has the same legend information, so rather than having a legend in each plot I would like to have one horizontal legend on display at the bottom of page. Unfortunately, I can't figure out how to make a legend without a plot. Once I have a separate image for the legend, I will know how to include it at the bottom of the page using Latex.

The code I am trying to use to make the legend is

plot(1, type = "n", axes=FALSE, xlab="", ylab="")
plot_colors <- c("blue","black", "green", "orange", "pink")

legend(.6,1.3,legend = c("Fabricated Metal", "Iron and Steel", "Paper", 
"Beverages", "Tobacco"), 
       col=plot_colors, lwd=5, cex=.5, horiz = TRUE)

But, the font is too small and the side of the legend box is cut off.

joran
  • 169,992
  • 32
  • 429
  • 468
user1288578
  • 411
  • 1
  • 6
  • 15
  • If all the plots use base R functions, you may have an easier time putting them all in one plot in R using `layout`. – joran Apr 30 '12 at 21:12

3 Answers3

36

A simple example of what I was talking about:

m <- matrix(c(1,2,3,4,5,6,7,7,7),nrow = 3,ncol = 3,byrow = TRUE)

layout(mat = m,heights = c(0.4,0.4,0.2))

for (i in 1:6){
    par(mar = c(2,2,1,1))
    plot(runif(5),runif(5),xlab = "",ylab = "")
}


plot(1, type = "n", axes=FALSE, xlab="", ylab="")
plot_colors <- c("blue","black", "green", "orange", "pink")
legend(x = "top",inset = 0,
        legend = c("Fabricated Metal", "Iron and Steel", "Paper","Beverages", "Tobacco"), 
        col=plot_colors, lwd=5, cex=.5, horiz = TRUE)

enter image description here

joran
  • 169,992
  • 32
  • 429
  • 468
9

Modifying the code using only par to align the legend on the bottom.

   n <- 6
   par(oma = c(4,1,1,1), mfrow = c(2, 3), mar = c(2, 2, 1, 1))
   for (i in 1:n){
      plot(runif(5),runif(5),xlab = '',ylab = '')
   }
   par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
   plot(0, 0, type = 'l', bty = 'n', xaxt = 'n', yaxt = 'n')
   legend('bottom',legend = c("Fabricated Metal", "Iron and Steel", "Paper", "Beverages", "Tobacco"), col = c("blue","black", "green", "orange", "pink"), lwd = 5, xpd = TRUE, horiz = TRUE, cex = 1, seg.len=1, bty = 'n')
   # xpd = TRUE makes the legend plot to the figure

enter image description here

Ying
  • 143
  • 1
  • 6
5

Try this,

plot_colors <- c("blue","black", "green", "orange", "pink")
text <- c("Fabricated Metal", "Iron and Steel", "Paper", 
"Beverages", "Tobacco")
plot.new()
par(xpd=TRUE)
legend("center",legend = text, text.width = max(sapply(text, strwidth)),
       col=plot_colors, lwd=5, cex=1, horiz = TRUE)
par(xpd=FALSE)
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • The above code creates a legend that is too large - You can only see the center of it and the sides are cut off. Even when I exported the file and reopened it I did not have the full legend. – user1288578 May 01 '12 at 00:05
  • that would depend on your font size and device width. You may want to have a look at the legends that `lattice` can produce, they may be a bit more clever in spacing the different keys. (you would need to use `gridBase` to add the legend in base plots). – baptiste May 01 '12 at 00:17