R/f_grid_arrange_shared_legend.R

Defines functions f_grid_arrange_shared_legend

## https://github.com/tidyverse/ggplot2/wiki/share-a-legend-between-two-ggplot2-graphs

#library(ggplot2)
#library(gridExtra)
#library(grid)

f_grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {
	plots <- list(...)
	position <- match.arg(position)
	g <- ggplotGrob(plots[[1]] + 
	theme(legend.position = position))$grobs
	legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
	lheight <- sum(legend$height)
	lwidth <- sum(legend$width)
	gl <- lapply(plots, function(x) x +
	theme(legend.position = "none"))
	gl <- c(gl, ncol = ncol, nrow = nrow)

	combined <- switch(position,
	                   "bottom" = arrangeGrob(do.call(arrangeGrob, gl), 
	                   legend,ncol = 1,
					heights = unit.c(unit(1, "npc") - lheight, lheight)),
					"right" = arrangeGrob(do.call(arrangeGrob, gl),
				  legend, ncol = 2,
					widths = unit.c(unit(1, "npc") - lwidth, lwidth)))

	grid.newpage()
	grid.draw(combined)

	# return gtable invisibly
	invisible(combined)
}



print("Function copied from: https://github.com/tidyverse/ggplot2/wiki/share-a-legend-between-two-ggplot2-graphs")
ManuelaRunge/ranalysis documentation built on Dec. 7, 2024, 5:01 a.m.