Shared legend for a cowplot grid in R - GeeksforGeeks

24 Oct, 2021
In this article, we will discuss how to create a cowplot grid with the shared legend in the R Programming language.
To do this first we will make a basic cowplot grid with two plots having their own legends separately. To do so using the cowplot grid package in the R Language we use the plot_grid() function and pass all the plots that we need in the grid as parameters.
Syntax: plot_grid(plot1,plot2,label=<label-vector>, ncol, nrow)
Example:
A basic cowplot grid with two plots having separate legends.
R
# Create sample data                           sample_data <- data.frame(  name = c("Geek1","Geek2","Geek3",           "Geek4","Geeek5") ,  value=c(31,12,15,28,45))  # Load ggplot2 and cowplotlibrary("ggplot2") library("cowplot")  # Create both plot and store in variableplot1<-ggplot(sample_data, aes(x=name, y=value, fill=name)) +        geom_bar(stat = "identity")  plot2<-ggplot(sample_data, aes(x="", y=value, fill=name))+        geom_bar(width = 1, stat = "identity") +        coord_polar("y", start=0)  # plot_grid() function adds both plots in a cowplot gridplot_grid(plot1, plot2)
Output:
To create a cowplot grid with shared legend there is no built-in method but the functionality can be achieved by following these steps:
Step 1: Create plots to be put in the grid without legend using:
plot + theme(legend.position = "none")
Step 2: Now combine both plots using the plot_grid() function and store that in a variable:
combined_plot<-plot_grid(plot1, plot2, ...... ncol)
Step 3: Now we extract the legend from one of the above plots to put it in the combined plot using:
legend <- get_legend( plot1 )
Step 4: Finally combine the combined plot with the derived legend using the plot_grid function to get the desired look for the shared legend plot.
plot_grid(combined_plot, legend, ncol)
Example:
Here, is an implementation of the above approach to get a cowplot grid with shared legend.
R
# Create sample data                           sample_data <- data.frame(  name = c("Geek1","Geek2","Geek3",           "Geek4","Geeek5") ,  value=c(31,12,15,28,45))  # Load ggplot2 and cowplotlibrary("ggplot2") library("cowplot")  # Create both plot without legend and store in variableplot1<-ggplot(sample_data, aes(x=name, y=value, fill=name)) +        geom_bar(stat = "identity")+        theme(legend.position = "none")  plot2<-ggplot(sample_data, aes(x="", y=value, fill=name))+        geom_bar(width = 1, stat = "identity") +        coord_polar("y", start=0)+        theme(legend.position = "none")  # combine both plot using plot_grid()combined_plot<-plot_grid(plot1, plot2,ncol=2)  # extract legend from plot1legend <- get_legend(  plot1 +    guides(color = guide_legend(nrow = 1)) +    theme(legend.position = "bottom"))  # Combine combined plot and legend using plot_grid()plot_grid(combined_plot, legend,ncol=1,rel_heights = c(1, .1))
Output:
Picked
R-Charts
R-Graphs
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to Change Axis Scales in R Plots?
Replace Specific Characters in String in R
R - if statement
How to filter R dataframe by multiple conditions?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R