Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. ## Increase HORIZONTAL space inside legend
  2. ggplot_padded <- function(x, w = 1){ # w=1 adds 1cm to horizontal space
  3. require(gtable)
  4. if (!gtable::is.gtable(x))
  5. x <- ggplotGrob(x)
  6. get_legend <- function(x){
  7. leg <- which(sapply(x$grobs, function(x) x$name) == "guide-box")
  8. x$grobs[[leg]]
  9. }
  10. g <- get_legend(x)
  11. # CASE-SPECIFIC: WITH 2 LEGEND ITEMS, POSITION TO BE ADJUSTED is 6
  12. # TWEAK THIS ON A CASE-BY-CASE... e.g. ADD LINES BELOW AS NEEDED
  13. # SIMILARLY FOR heights...
  14. g$grobs[[1]]$widths[6] <- g$grobs[[1]]$widths[6] + unit(w,"cm")
  15. x$grobs[x$layout$name == "guide-box"][[1]] <- g
  16. require(grid)
  17. grid.draw(x)
  18. }
  19. pdf("plot.pdf", width = 8, height = 5)
  20. ggplot_padded(p)
  21. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement