The Wayback Machine - https://web.archive.org/web/20241005211330/https://www.geeksforgeeks.org/how-to-change-legend-title-in-ggplot2-in-r/
Open In App

How to change legend title in ggplot2 in R?

Last Updated : 30 May, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will see how to change the legend title using ggplot2 in R Programming. 

We will use ScatterPlot. For the Data of Scatter Plot, we will pick some 20 random values for the X and Y axis both using rnorm() function which can generate random normal values, and here we have one more parameter for naming the legend named ‘Users’. We use sample() function to generate data for Users vector. sample() function takes a sample of the specified size of elements with or without replacement. Then create a DataFrame and assign it to ‘data’ data object.

Now we will create a Plot using ggplot() function and make it Scattered using geom_point() function. Here for creating a simple Scatterplot, we use only the size argument of geom_point() function to set the size of the point.

R




# Load Package
library("ggplot2")
  
# Create DataFrame for plotting
data<-data.frame(x = rnorm(20),
               y = rnorm(20),
               Users = sample(c("User 1", "User 2", "User 3",
                                "User 4", "User 5"),
                              20, replace=TRUE))
  
# Create ScatterPlot using ggplot2 
ggplot(data,aes(x, y, color = Users))+
  geom_point(size = 10)


Output:

ScatterPlot with Legend

ScatterPlot with Legend

Method 1: Change Legend Title using guides() Function.

Now if we want to change Legend Title then we have to add guides and guide_legend functions to the geom_point function. Inside guides() function, we take parameter named ‘color’ because we use color parameter for legend in ggplot() function. ‘color’ has call to guide_legend() guide function as value. Inside guide_legend() function, we take an argument called ‘title’ , which has the new title of legend as a value.

Syntax : guide_legend(title)

Parameter :

  • title : A string, which is the New Title of ggplot2 Legend. if title has value NULL, the title is not shown.

Return : Legend Guides for various scales

Here title “Users” is replaced with “Users By guides” By using guides() function.

R




# Load Package
library("ggplot2")
  
# Create DataFrame for plotting
data<-data.frame(x = rnorm(20),
               y = rnorm(20),
               Users = sample(c("User 1", "User 2", "User 3",
                                "User 4", "User 5"),
                              20, replace = TRUE))
  
# Create ScatterPlot with Changed Title of 
# Legend using guides() 
ggplot(data,aes(x, y, color = Users))+
  geom_point(size = 10)+
  guides(color = guide_legend(title = "Users By guides"))


Output:

ScatterPlor with changed title of Legend using guides()

ScatterPlor with changed title of Legend using guides()

Method 2: Change Legend Title using labs() Function.

Generally labs() function is widely used for assigning title, subtitle, caption, and tags to plot, but it can also change the title of other aesthetics. Like here we have one extra aesthetic inside ggplot() function named ‘color’ for legend. So, we can also change the title of ‘color’ aesthetic (i.e. Legend) For that we have to add lab function to geom_point and assign the parameter ‘color’ to its function and give it the new title of ggplot2 plot Legend.

Syntax : labs(…)

Parameter :

  • Generally labs() has many parameters like title, subtitle, caption, tags, label. we can use them as per our requirements. here we use only one plot aesthetic as a parameter which changes the title of plot Legend.
  • … : List of new aesthetic pairs.

Return : Labels which useful to make plot more understandable.

Here title “Users” is replaced with “Users By labs” By using labs() function.

R




# Load Package
library("ggplot2")
  
# Create DataFrame for plotting
data<-data.frame(x = rnorm(20),
               y = rnorm(20),
               Users = sample(c("User 1", "User 2", "User 3",
                                "User 4", "User 5"),
                              20, replace=TRUE))
  
# Create ScatterPlot with Changed Title
# of Legend using labs() 
ggplot(data,aes(x, y, color = Users))+
  geom_point(size = 10)+
  labs(color = "Users By labs")


Output:

ScatterPlor with changed title of Legend using labs()

ScatterPlor with changed title of Legend using labs()



Previous Article
Next Article

Similar Reads

How to Change Font Size of Plot Title When the Title Is a Variable in ggplot2 in R?
In data visualization with ggplot2, one often needs to customize plot titles to enhance readability and aesthetics. There are situations where the title might be stored as a variable, and you want to control the font size dynamically. This can be useful when you're generating multiple plots programmatically or working with dynamic reports using R P
3 min read
How to Add Legend for Regional Map with a Legend Describing Associated Labels Using ggplot2 in R
Visualizing regional data on a map using ggplot2, it is crucial to provide a clear legend that describes the regions or associated labels. This article will walk you through the theory and examples of adding a legend to a map created using ggplot2, which links map regions to meaningful labels (such as categories, names, or quantitative measures. Ad
4 min read
How to remove legend title in R with ggplot2 ?
At times, the legend keys are enough to display what they are supposed to describe. Thus, in such cases, the legend title can be dropped. In this article, we will discuss how the legend title can be removed using ggplot2 in the R programming language. Let us first draw a regular plot with a legend title: C/C++ Code library("ggplot2") func
2 min read
How to change legend title in R using ggplot ?
A legend helps understand what the different plots on the same graph indicate. They basically provide labels or names for useful data depicted by graphs. In this article, we will discuss how legend names can be changed in R Programming Language. Let us first see what legend title appears by default. Example: C/C++ Code library("ggplot2")
2 min read
How to change the legend shape using ggplot2 in R?
In this article, we will discuss how to change only legend shape using ggplot2 in R programming language. Here ScatterPlot is used the same can be applied to any other plot. Syntax : sample(x, size, replace = TRUE) Parameters : x : either a vector of one or more values from which we want to choose the values or a positive integer. here we use colum
3 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
How to Change the Y-axis Title to Horizontal Using ggplot2 in R
In ggplot2, the default orientation for the y-axis title is vertical. However, in some cases, you may want to rotate the y-axis title to be horizontal to improve the readability or appearance of your plot. This article explains how to change the orientation of the y-axis title to horizontal using ggplot2 in R. Overview of ggplot2ggplot2 is a powerf
3 min read
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
How To Fold Legend into Two Rows in ggplot2 in R
In this article, we are going to see how to draw a ggplot2 legend with two Rows in R Programming Language. If we want to draw ggplot2 Legend with two rows, we have to add guides and guide_legend functions to the theme() function. Inside guides() function, we take parameter named color, which has call to guide_legend() guide function as value. Insid
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
Control Size of ggplot2 Legend Items in R
In this article, we will see how to control the size of ggplot2 Legend Items in R Programming Language. To create an R plot, we use ggplot() function and for making a scatter plot geom_point() function is added to ggplot() function. Let us first create a regular plot without any modifications so that the difference is apparent. Example: C/C++ Code
2 min read
Reversing the order of a ggplot2 legend
In this article, we will see how to Reverse the order of Legend Items of ggplot2 plots in the R programming language. Getting Started First, load the ggplot2 package by using the library() function. If you have not installed it yet, you can simply install it by writing the below command in R Console. install.packages("ggplot2") Let us first create
2 min read
Combine Multiple ggplot2 Legend in R
In this article, we will see how to combine multiple ggplot2 Legends in the R programming language. Installation First, load the ggplot2 package by using the library() function. If you have not installed it yet, you can simply install it by writing the below command in R Console. install.packages("ggplot2") To create an R plot, we use ggplot() func
2 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
How to move or position a legend in ggplot2?
In this article, we will discuss how to control a legend position in ggplot using an 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 attri
3 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
Divide Legend of ggplot2 Plot in R
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 mul
2 min read
Add legend for multiple lines in R using ggplot2
In this article, we are going to see how to add legends for multiple line plots in R Programming Language using ggplot2. First, you need to install the ggplot2 package if it is not previously installed in R Studio. The functions used to create the line plots are : geom_line( ) : To plot the line and assign its size, shape, color, etc. Syntax: geom_
3 min read
How to make legend key fill transparent in ggplot2 in R?
In this article, we will see how to make legend key fill transparent in ggplot2 in R Programming Language. Note: Here we will be using a line graph, the same can be applied to any other plot. Let us first draw a regular plot, so that the difference is apparent. Example: C/C++ Code # Load Package library("ggplot2") # Create DataFrame DF
2 min read
How to Hide Legend in ggplot2 in R ?
In this article we will discuss how can we hide a legend in R programming language, using ggplot2. Note: Here, a line plot is used for illustration the same can be applied to any other plot. Let us first draw a regular plot, with a legend, so that the difference is apparent. For this, firstly, import the required library and create dataframe, the d
2 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
Add Common Legend to Combined ggplot2 Plots in R
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
3 min read
Remove Legend in ggplot2 in R
In this article, we will discuss how to remove a legend from a plot using an R programming language. Legend in ggplot2 GraphIn ggplot2 in R, the legend is a key component that provides information about the mapping between aesthetics and data variables in a plot. The legend is automatically generated based on the aesthetic mappings you specify in t
5 min read
Wrap legend text in ggplot2 in R
When creating plots with ggplot2 in R, you often encounter cases where the legend text is too long, causing it to overlap with other elements or extend beyond the plot area. This can make the plot look cluttered and difficult to interpret. To address this issue, you can wrap the legend text to fit within a specific width, ensuring that your plots r
4 min read
How to put the title inside the plot using ggplot2 in R?
A title to a plot gives information about the graph so that it is easier for the reader to interpret what relations the variables are supposed to depict. This article discusses how we can put a title inside a plot and further discusses various ways in which the title can be formatted. The examples are given below use bar plot. To add title within a
3 min read
ggplot2 - Title and Subtitle with Different Size and Color in R
A Title and the subtitle to a plot give a piece of information about the graph that what the graph actually wants to represent. This article describes how to add a Title and Subtitle with Different Sizes and Colors using ggplot2 in R Programming. To add a Title and Subtitle within a plot, first, we have to import ggplot2 library using library() fun
5 min read
How To Remove facet_wrap Title Box in ggplot2 in R ?
In this article, we will discuss how facet_wrap works in R Programming Language. we will discuss all the types and methods. facet_wrap in RIn R Programming Language facet_wrap() is a function from the ggplot2 package that allows you to create multiple plots, or facets, based on a categorical variable. It is commonly used to create a grid of plots w
4 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
Article Tags :