Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. library(ggplot2)
  2. library(gridExtra)
  3. dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
  4.  
  5. p1 <- qplot(carat, price, data=dsamp, colour=clarity)
  6. p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
  7.  
  8. g_legend<-function(p){
  9. tmp <- ggplotGrob(p)
  10. leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  11. legend <- tmp$grobs[[leg]]
  12. return(legend)}
  13.  
  14. legend <- g_legend(p1)
  15. lwidth <- sum(legend$width)
  16.  
  17. ## using grid.arrange for convenience
  18. ## could also manually push viewports
  19. grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
  20. p2 + theme(legend.position="none"),
  21. main ="this is a title",
  22. left = "This is my global Y-axis title"), legend,
  23. widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
  24.  
  25. p1 <- qplot(carat, price*0.001, data=dsamp, colour=clarity)
  26. p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
  27.  
  28. g_legend<-function(p){
  29. tmp <- ggplotGrob(p)
  30. leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  31. legend <- tmp$grobs[[leg]]
  32. return(legend)}
  33.  
  34. legend <- g_legend(p1)
  35. lwidth <- sum(legend$width)
  36.  
  37. ## using grid.arrange for convenience
  38. ## could also manually push viewports
  39. grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
  40. p2 + theme(legend.position="none"),
  41. main ="this is a title",
  42. left = "This is my global Y-axis title"), legend,
  43. widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement