Skip to content

Instantly share code, notes, and snippets.

@fawda123
Created September 17, 2013 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fawda123/6596631 to your computer and use it in GitHub Desktop.
Save fawda123/6596631 to your computer and use it in GitHub Desktop.
ggplot2_ex
#credit http://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots
require(ggplot2)
require(reshape)
require(gridExtra)
p.dat<-data.frame(step=row.names(grp.dat),grp.dat,stringsAsFactors=F)
p.dat<-melt(p.dat,id='step')
p.dat$step<-as.numeric(p.dat$step)
p<-ggplot(p.dat,aes(x=step,y=value))
p1<-p + geom_area(aes(fill=variable)) + theme(legend.position="bottom")
p2<-p + geom_area(aes(fill=variable),position='fill')
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)}
mylegend<-g_legend(p1)
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
p2 + theme(legend.position="none"),
nrow=1),
mylegend, nrow=2,heights=c(10, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment