Divide Legend of ggplot2 Plot in R Last Updated : 29 Jun, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to divide the legend of ggplot2 plot in the R programming language. To divide the legend of the ggplot2 plot, the user needs to install and import the gridExtra and cowplot packages in the R console. gridExrta package: Provides a number of user-level functions to work with "grid" graphics, notably to arrange multiple grid-based plots on a page, and draw tables. cowplot package: The cowplot package is a simple add-on to ggplot. It provides various features that help with creating publication-quality figures, such as a set of themes, functions to align plots and arrange them into complex compound figures, and functions that make it easy to annotate plots and or mix plots with images. Let us first create a plot with all the legends before dividing them, so that the difference is more apparent. Data in use: Example: R library(ggplot2) library(gridExtra) library(cowplot) gfg_data <- data.frame(x = 1:10, y = 10:1, group = LETTERS[1:10]) ggp_plot<- ggplot(gfg_data, aes(x,y,color = group)) + geom_bar(stat="identity") +scale_color_manual(values = 1:10) + labs(color = "Legend-1") ggp_plot Output: To divide the legends, extract a smaller sample of the data from the dataframe and apply the required functions with proper parameters to generate the desired plot. Example: R # legends for two library(ggplot2) library(gridExtra) library(cowplot) gfg_data <- data.frame(x = 1:10, y = 10:1, group = LETTERS[1:10]) gfg_split_1 <- gfg_data[gfg_data$group %in% c("A", "B"), ] gfg_split_1 ggp_split_plot_1 <- ggplot(gfg_split_1, aes(x,y,color = group)) + geom_bar(stat="identity")+scale_color_manual(values = 1:2) + labs(color = "Legend-1") ggp_split_plot_1 Output: Example: R # legends for three library(ggplot2) library(gridExtra) library(cowplot) gfg_data <- data.frame(x = 1:10, y = 10:1, group = LETTERS[1:10]) gfg_split_2 <- gfg_data[gfg_data$group %in% c("C", "D","E"), ] gfg_split_2 ggp_split_plot_2 <- ggplot(gfg_split_2, aes(x,y,color = group)) + geom_bar(stat="identity")+ scale_color_manual(values = 1:3) +labs(color = "Legend-1") ggp_split_plot_2 Output: Example: R # legends for rest of the data library(ggplot2) library(gridExtra) library(cowplot) gfg_data <- data.frame(x = 1:10, y = 10:1, group = LETTERS[1:10]) gfg_split_3 <- gfg_data[! gfg_data$group %in% c("A","B","C", "D","E"), ] gfg_split_3 ggp_split_plot_3 <- ggplot(gfg_split_3, aes(x,y,color = group)) + geom_bar(stat="identity")+scale_color_manual(values = 1:5) + labs(color = "Legend-1") ggp_split_plot_3 Output: Comment More infoAdvertise with us Next Article Divide Legend of ggplot2 Plot in R G geetansh044 Follow Improve Article Tags : R Language R-ggplot Similar Reads Create Legend in ggplot2 Plot in R In this article, we will discuss how to create a legend in ggplot using R programming language. To draw a legend within ggplot the parameter col is used, it basically adds colors to the plot and these colors are used to differentiate between different plots. To depict what each color represents a le 2 min read Draw ggplot2 Legend without Plot in R A legend in the graph describes each part of the plot individually and is used to show statistical data in graphical form. In this article, we will see how to draw only the legend without a plot in ggplot2. First, let us see how to draw a graph with a legend so that the difference is apparent. For 3 min read Set Area Margins of ggplot2 Plot in R In this article, we will discuss how to set area margins of the ggplot2 plot in the R programming language. To do this call the theme() function and use the plot.margin argument of this function with the required data to this argument as per the requirement of the user. theme() function is a powerf 1 min read Modify axis, legend, and plot labels using ggplot2 in R In this article, we are going to see how to modify the axis labels, legend, and plot labels using ggplot2 bar plot in R programming language. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters :  stat : Set the stat parameter to 5 min read Set Axis Breaks of ggplot2 Plot in R In this article, we are going to see how to set axis break of ggplot2 plot in R Programming Language. To add axis breaks in ggplot2 plots in R, we use scale_x_break() and scale_y_break() functions. These functions take a vector as a parameter that has breakpoints.  If we need multiple breakpoints w 2 min read Like