3

I have a ggplot object:

ggplot(plot1,aes(x = c, y = value, colour = variable, linetype = variable,size = variable)) + 
    geom_line() + 
    scale_x_continuous(breaks=seq(1,10,1)) +
    #scale_y_continuous(breaks=seq(0,1, 0.1))+
    scale_colour_manual(values=c("blue3","red3")) + 
    scale_linetype_manual(values = c(3,1)) + 
    scale_size_manual(values = c(0.6,0.3)) + 
    xlab("b") + 
    ylab("a") +
    theme_bw() +
    theme(axis.text=element_text(size=5),
          axis.title=element_text(size=5),
          axis.line = element_line(size=0.25),
          axis.ticks=element_line(size=0.25),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank(),
          legend.position="right" ,
          legend.direction="vertical", 
          legend.title=element_blank(),
          legend.text=element_text(size=8), 
          legend.background=element_blank(), 
          legend.key=element_blank())

I would like to plot the corresponding legend separately.

Is it possible to extract it somehow?

I tried using this function to save the legend a a grob, but it only returns and empty quartz window but no legend.

g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}

source: https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs

user1723765
  • 6,179
  • 18
  • 57
  • 85
upabove
  • 1,057
  • 3
  • 18
  • 29
  • 1
    Did you even try to [**search for an answer**](http://stackoverflow.com/search?q=[ggplot]+extract+legend)? – Henrik Feb 24 '14 at 13:30
  • yes I did, please see my edit – upabove Feb 24 '14 at 13:32
  • 1
    It would make it a lot easier for us when you also provide some data in a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jaap Feb 24 '14 at 18:51

1 Answers1

4

Using a simplyfied version of your plot code:

# base plot
p1 <- ggplot(plot1,aes(x=c, y=value, colour=variable, linetype=variable, size=variable)) + 
    geom_line()

# extract the different legends
leg1 <- p1 + guides(linetype=FALSE, size=FALSE)
leg2 <- p1 + guides(colour=FALSE, size=FALSE)
leg3 <- p1 + guides(colour=FALSE, linetype=FALSE)
legend.colour <- gtable_filter(ggplot_gtable(ggplot_build(leg1)), "guide-box") 
legend.linetype <- gtable_filter(ggplot_gtable(ggplot_build(leg2)), "guide-box") 
legend.size <- gtable_filter(ggplot_gtable(ggplot_build(leg3)), "guide-box") 

leg1Grob <- grobTree(legend.colour)
leg2Grob <- grobTree(legend.linetype)
leg3Grob <- grobTree(legend.size)

p2 <- p1 + guides(colour=FALSE, linetype=FALSE, size=FALSE)

# final plot were you can place the legends were you want them
finplot <- p2 +
  annotation_custom(grob = leg1Grob, xmin = ??, xmax = ??, ymin = ??, ymax = ??) +
  annotation_custom(grob = leg2Grob, xmin = ??, xmax = ??, ymin = ??, ymax = ??) +
  annotation_custom(grob = leg3Grob, xmin = ??, xmax = ??, ymin = ??, ymax = ??)

Replace the ?? with the coordinates in your plot were you want to put your legends.

When you want to plot the legends seperately, make a blank plot with geom_blank

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • The ``xmin`` and ``ymax`` etc. are in units of the data coordinates, and not the usual ggplot ``c(0,1)``. – PatrickT Jan 01 '15 at 07:52