Open In App

Divide Legend of ggplot2 Plot in R

Last Updated : 29 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
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:



Similar Reads

How to move a ggplot2 legend with multiple rows to the bottom of a plot in R
In this article, we are going to see how to draw ggplot2 legend at the bottom and with two Rows in R Programming Language. First, we have to create a simple data plot with legend. Here we will draw a Simple Scatter plot. Loading Library First, load the ggplot2 package by using library() function. library("ggplot2") Create a DataFrame for example. H
3 min read
Control Line Color and Type in ggplot2 Plot Legend in R
In this article, we will see how to control line color and type in ggplot2 plot legend in the R programming language. Using Default Parameters In this method, inbuilt attributes are passed to the function with appropriate values to generate the requirement. Thus, in order to change the color, col or color attribute has to be passed with the column
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 that load the ggplot2 package by using library() fu
3 min read
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 legend is produced by ggplot. col attribute can be s
2 min read
Change Display Order of ggplot2 Plot Legend in R
In this article, we will see how to change display order of ggplot2 legend in R Programming Language. For that, first we should load ggplot2 package using library() function. Syntax for loading or installing ggplot2 package is given below. to install ggplot2 package, write following command to R Console. install.packages("ggplot2") library("ggplot2
3 min read
Change Spacing Between Horizontal Legend Items of ggplot2 Plot in R
In this article, we are going to see how to change the space between horizontal legend items using the ggplot2 bar plot in R programming language. Here we are using a bar graph the same can be done to any other plot. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters : stat : S
2 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 identify the mode.fill : Represents color inside t
5 min read
Set Legend Alpha of ggplot2 Plot in R
In this article, we are going to see how to set the legend alpha of the ggplot2 Plot in R Programming Language. Setting the legend alpha of the plot using the alpha argument of the guide_legend function from the ggplot2 package. Syntax: guide_legend(override.aes = list(alpha)) Parameters: override.aes: allows the user to change only the legend appe
2 min read
Set Axis Limits of ggplot2 Facet Plot in R - ggplot2
In this article, we will discuss how to set the axis limits of the ggplot2 facet plot in the R programming language. Method 1: Set axis limits of ggplot2 facet plot with Free Scales Here the role of the ggplot2 package is to plot the facet plot and provide some functionalities to the user, further the user needs to set the argument of the scales fu
5 min read
Plot Only One Variable in ggplot2 Plot in R
In this article, we will be looking at the two different methods to plot only one variable in the ggplot2 plot in the R programming language. Draw ggplot2 Plot Based On Only One Variable Using ggplot &amp; nrow Functions In this approach to drawing a ggplot2 plot based on the only one variable, firstly the user needs to install and import the ggplo
5 min read
Article Tags :