Skip to content

Instantly share code, notes, and snippets.

@kohske
Created April 2, 2015 12:15
Show Gist options
  • Save kohske/9abf67bd26f69ed851fd to your computer and use it in GitHub Desktop.
Save kohske/9abf67bd26f69ed851fd to your computer and use it in GitHub Desktop.
separate plot of facet_wrap in ggplot2
library(gtable)
d=data.frame(g=c(1,1,1,2,2,3),x=c(1,2,3,1,2,3),y=1:6);
p = ggplot(d,aes(x,y))+geom_bar(stat="identity")+facet_wrap(~g)
gt= ggplot_gtable(ggplot_build(g))
graphics.off()
for (p in c(4, 7, 10)) {
pdf(paste0("plot-", p, ".pdf"))
grid.draw(gt[,c(1:3, p, 12)])
dev.off()
}
@kohske
Copy link
Author

kohske commented Apr 2, 2015

more generally you can inspect the column of panel by:

subset(gt$layout, grepl("^panel", name))$l

@kozo2
Copy link

kozo2 commented Apr 2, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment