Open In App

Remove Legend in ggplot2 in R

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

legend_remove_sample_data
Sample Data

2. 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:

gh
Remove Legend in ggplot2 in R

3. 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:

gh
Remove Legend in ggplot2 in R

We 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:

gh
Remove Legend in ggplot2 in R

Method 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:

legend_remove_sample_data2
Sample Data

2. 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:

gh
Remove Legend in ggplot2 in R

3. 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:

gh
Remove Legend in ggplot2 in R

In this article, we discussed how to remove a legend from a plot using an R programming language.


Next Article
Article Tags :

Similar Reads