Add Common Legend to Combined ggplot2 Plots in R - GeeksforGeeks

28 Nov, 2021
In this article, we will discuss how to create a combined plot with a shared legend in R Language using the ggplot2 package.
To join multiple ggplot2 plots, we use the grid.arrange() function of the gridExtra package in the R Language. The grid.arrange() function converts the frame into a grid of the desired number of rows and columns. Then we put different plots in different parts of the grid to create the desired look for multiple plots all in the same frame.
Syntax:
grid.arrange( plot1, plot2,......, ncol/nrow)
where,
ncol: Determines the number of columns in the grid.nrow: Determines the number of rows in the grid.
ncol: Determines the number of columns in the grid.
nrow: Determines the number of rows in the grid.
Example:
Here, is a combined plot made using the grid.arrange() function of the ggplot2 package.
R
# Load ggplot2 and gridExtralibrary("ggplot2") library("gridExtra")  # Create sample dataset.seed(5642)                             sample_data <- data.frame(  name = c("Geek1","Geek2","Geek3",           "Geek4","Geeek5") ,  value=c(31,12,15,28,45))    # Create both plot and store in variableplot1<-ggplot(sample_data,              aes(x = name, y = value, col=name)) +              geom_point(size=4)plot2<-ggplot(sample_data,              aes(x = name, y = value, fill=name)) +              geom_col()  # combine both plots using grid.arrange()grid.arrange(plot1, plot2, ncol = 2)
Output:
To create a combined plot with shared legend we follow the following steps:
Step 1: Create a combined plot using the grid.arrange() function without any legend.
R
# To remove legendplot+ theme( legend.position = "none" )   # arrange side by side in a gridgrid.arrange( plot1 , plot2 , ncol=2)
Step 2: Create a function that extracts legend out of the ggplot2 plot and returns it as a ggplot element.
R
get_only_legend <- function(plot) {    # get tabular interpretation of plotplot_table <- ggplot_gtable(ggplot_build(plot))     #  Mark only legend in plotlegend_plot <- which(sapply(plot_table$grobs, function(x) x$name) == "guide-box")                               # extract legendlegend <- plot_table$grobs[[legend_plot]]                              # return legendreturn(legend) }
Step3: Combine legend obtained from the above function using the grid.arrange() function.
R
grid.arrange(combined_plot, legend, nrow = 2, heights = c(10, 1))
And the resulting plot will be a combined plot with a shared legend.
Example:
Here, is a combined plot made using the grid.arrange() function with shared legend.
R
# Create sample dataset.seed(5642)                             sample_data <- data.frame(  name = c("Geek1","Geek2","Geek3",           "Geek4","Geeek5") ,  value=c(31,12,15,28,45))  # Load ggplot2 and gridExtralibrary("ggplot2") library("gridExtra")  # Create both plot and store in variable without legendplot1<-ggplot(sample_data,              aes(x = name, y = value, col=name)) +              geom_point(size=4)+              theme(legend.position = "none")plot2<-ggplot(sample_data,              aes(x = name, y = value, fill=name)) +              geom_col()+              theme(legend.position = "none")  # combine both plots using grid.arrange()combined_plot <- grid.arrange(plot1, plot2, ncol = 2)  # plot1 with legendplot1_legend <- ggplot(sample_data,              aes(x = name, y = value, col=name)) +              geom_point(size=4)+              theme(legend.position = "bottom")  # function to extract legend from plotget_only_legend <- function(plot) {  plot_table <- ggplot_gtable(ggplot_build(plot))  legend_plot <- which(sapply(plot_table$grobs, function(x) x$name) == "guide-box")  legend <- plot_table$grobs[[legend_plot]]  return(legend)}                            # extract legend from plot1 using above functionlegend <- get_only_legend(plot1_legend)        # final combined plot with shared legendgrid.arrange(combined_plot, legend, nrow = 2, heights = c(10, 1))
Output:
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to import an Excel File into R ?
Time Series Analysis in R