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