Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Increase HORIZONTAL space inside legend
- ggplot_padded <- function(x, w = 1){ # w=1 adds 1cm to horizontal space
- require(gtable)
- if (!gtable::is.gtable(x))
- x <- ggplotGrob(x)
- get_legend <- function(x){
- leg <- which(sapply(x$grobs, function(x) x$name) == "guide-box")
- x$grobs[[leg]]
- }
- g <- get_legend(x)
- # CASE-SPECIFIC: WITH 2 LEGEND ITEMS, POSITION TO BE ADJUSTED is 6
- # TWEAK THIS ON A CASE-BY-CASE... e.g. ADD LINES BELOW AS NEEDED
- # SIMILARLY FOR heights...
- g$grobs[[1]]$widths[6] <- g$grobs[[1]]$widths[6] + unit(w,"cm")
- x$grobs[x$layout$name == "guide-box"][[1]] <- g
- require(grid)
- grid.draw(x)
- }
- pdf("plot.pdf", width = 8, height = 5)
- ggplot_padded(p)
- dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement