Remove Legend in ggplot2 in R
Last Updated :
30 Apr, 2025
In ggplot2 library
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 specified in the aes()
function. We can also remove the legend based on our requirements.
Different Methods to Remove Legend
We can remove the legend from the plot using two methods.
Method 1: Using theme()
theme() is a function to customize the non-data components of your plots (like: titles, labels, fonts, background, grid-lines, and legends). This function can also be used to give plots a consistent customized look.
Syntax:
theme (legend.position)
Parameter:
- legend.position: changes the legend position to some specified value.
1. Create one Random dataset
First we create a data frame with three groups (A
, B
, and C
) and a numeric variable.
R
set.seed(42)
data <- data.frame(
Group = rep(c("A", "B", "C"), each = 30),
Value = c(rnorm(30, mean = 50, sd = 10),
rnorm(30, mean = 60, sd = 15),
rnorm(30, mean = 55, sd = 8))
)
head(data)
Output:
Sample Data2. Create a Box Plot
The geom_boxplot
function is used to create the box plot, and outlier.shape = NA
is used to hide the default points for outliers. Then, geom_point
is used to add customized red points for outliers using the position_jitterdodge()
function.
R
install.packages("ggplot2")
library(ggplot2)
ggplot(df, aes(x = Group, y = Value, fill = Group)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitterdodge(), color = "red") +
labs(title = "Box Plot with Outliers",
x = "Group",
y = "Value") +
theme_minimal()
Output:
Remove Legend in ggplot2 in R3. Now remove legend from plot
To remove the legend from our box plot in ggplot2
, we can add the guides()
function and set fill = FALSE
within it. Here, legend appears using the guides()
function, where we can specify which legends to modify and how. In this case, setting fill = FALSE
removes the legend associated with the 'Group' variable.
R
ggplot(df, aes(x = Group, y = Value, fill = Group)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitterdodge(), color = "red") +
labs(title = "Box Plot with Outliers",
x = "Group",
y = "Value") +
theme_minimal() +
guides(fill = FALSE)
Output:
Remove Legend in ggplot2 in RWe can also remove legend using (legend.position = "none") . This will remove the legend from the plot since no position for the legend is specified.
R
Box_Plot<-ggplot(df, aes(x = Group, y = Value, fill = Group)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitterdodge(), color = "red") +
labs(title = "Box Plot with Outliers",
x = "Group",
y = "Value") +
theme_minimal()
Box_Plot + theme(legend.position = "none")
Output:
Remove Legend in ggplot2 in RMethod 2: Using guides()
Another alternative is to call guides() method with an appropriate term that has been used to set the color difference for the plot objects produced. Either fill or color, should be set to none.
Syntax: guides(color/fill=”none”)
1. Create Sample Data
We are creating a custom data frame custom_data
with 30 rows, where the Category
column has values 'A', 'B', and 'C', the Value
column contains random normal values, and the Shape
column is a factor with random letters. We then convert the Category
column to a factor and display it.
R
set.seed(123)
n <- 30
custom_data <- data.frame(
Category = rep(c('A', 'B', 'C'), each = n/3),
Value = rnorm(n),
Shape = factor(sample(letters[1:3], n, replace = TRUE))
)
custom_data$Category <- as.factor(custom_data$Category)
head(custom_data)
Output:
Sample Data2. Ploting a Scatter Plot
We are creating a scatter plot using ggplot2
, where the Category
is mapped to the x-axis and Value
to the y-axis. We customize the plot by assigning colors based on Category
using the viridis
color scale, and apply custom shapes for the Shape
variable using scale_shape_manual()
R
library(ggplot2)
ggplot(data = custom_data, aes(x = Category, y = Value)) +
geom_point(aes(color = Category, shape = Shape), size = 4) +
scale_color_viridis_d() +
scale_shape_manual(values = c(16, 17, 18))
Output:
Remove Legend in ggplot2 in R3. Remove legend for a particular aesthetic
We are creating a scatter plot using ggplot2
with the same structure as the previous code, mapping Category
to the x-axis and Value
to the y-axis. The key difference here is the addition of guides(shape = FALSE)
, which removes the shape legend from the plot, unlike the previous version where the shape legend was included. This allows for a cleaner plot without the shape legend
R
library(ggplot2)
ggplot(data = custom_data, aes(x = Category, y = Value)) +
geom_point(aes(color = Category, shape = Shape), size = 4) +
scale_color_viridis_d() +
scale_shape_manual(values = c(16, 17, 18)) +
guides(shape = FALSE)
Output:
Remove Legend in ggplot2 in RIn this article, we discussed how to remove a legend from a plot using an R programming language.
Similar Reads
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 w
4 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, impor
2 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 t
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 le
2 min read
How to change legend title in ggplot2 in R?
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 p
3 min read
Reorder Facets in ggplot2 Plot in R
In this article, we will be looking at an approach to reorder the facets in the ggplot2 plot in R programming language. To reorder the facets accordingly of the given ggplot2 plot, the user needs to reorder the levels of our grouping variable accordingly with the help of the levels function and requ
2 min read
Remove NA Values from ggplot2 Plot in R
In this article, we are going to see how to remove the NA values from the ggplot2 plot in the R programming language. Using complete.cases() function complete.cases() function: This function will be returning a logical vector indicating which cases are complete, i.e., have no missing values. Syntax:
2 min read
Remove Axis Labels using ggplot2 in R
In this article, we are going to see how to remove axis labels of the ggplot2 plot in the R programming language. We will use theme() function from ggplot2 package. In this approach to remove the ggplot2 plot labels, the user first has to import and load the ggplot2 package in the R console, which i
2 min read
Move Axis Labels in ggplot in R
In this article, we are going to see how to move the axis labels using ggplot2 bar plot in the R programming language. First, you need to install the ggplot2 package if it is not previously installed in R Studio. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(s
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 tha
2 min read