Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(ggplot2)
- library(gridExtra)
- dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
- p1 <- qplot(carat, price, data=dsamp, colour=clarity)
- p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
- g_legend<-function(p){
- tmp <- ggplotGrob(p)
- leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
- legend <- tmp$grobs[[leg]]
- return(legend)}
- legend <- g_legend(p1)
- lwidth <- sum(legend$width)
- ## using grid.arrange for convenience
- ## could also manually push viewports
- grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
- p2 + theme(legend.position="none"),
- main ="this is a title",
- left = "This is my global Y-axis title"), legend,
- widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
- p1 <- qplot(carat, price*0.001, data=dsamp, colour=clarity)
- p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
- g_legend<-function(p){
- tmp <- ggplotGrob(p)
- leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
- legend <- tmp$grobs[[leg]]
- return(legend)}
- legend <- g_legend(p1)
- lwidth <- sum(legend$width)
- ## using grid.arrange for convenience
- ## could also manually push viewports
- grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
- p2 + theme(legend.position="none"),
- main ="this is a title",
- left = "This is my global Y-axis title"), legend,
- widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement