Open In App

Remove Legend in ggplot2 in R

Improve
Improve
Like Article
Like
Save
Share
Report

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

Legend in ggplot2 Graph

In 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 the aes() function. If you want to remove the legend from a ggplot2 plot.

Method 1: Using theme()

theme() function is a powerful way to customize the non-data components of your plots: i.e. titles, labels, fonts, background, gridlines, 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.

Calling theme function with legend.position set to none will get the job done.

Create one Random dataset

R




# Load the ggplot2 library
library(ggplot2)
 
# Set seed for reproducibility
set.seed(42)
 
# Create a sample dataframe
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:

  Group    Value
1     A 63.70958
2     A 44.35302
3     A 53.63128
4     A 56.32863
5     A 54.04268
6     A 48.93875

In this example, we create a dataframe with three groups (A, B, and C) and a numeric variable.

Create a box plot with outliers

R




# Create a box plot with outliers
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

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.

Now remove legend from plot

To remove the legend from our box plot with outliers in ggplot2, we can add the guides() function and set fill = FALSE within it.

R




# Create a box plot with outliers and remove the legend
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

In this code legend appearance in ggplot2 are often made 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.

We can also remove legend using (legend.position = “none”) here are the example.

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()
 
# Remove Legend from plot
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, it should be set to none.

Syntax: guides(color/fill=”none”) 

Remove legend for a particular aesthetic

R




# Create a new dataset
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))
)
 
# Convert 'Category' to a factor
custom_data$Category <- as.factor(custom_data$Category)
 
# View the new dataset
head(custom_data)


Output:

  Category       Value Shape
1        A -0.56047565     a
2        A -0.23017749     c
3        A  1.55870831     a
4        A  0.07050839     b
5        A  0.12928774     a
6        A  1.71506499     a

Now, you can use this custom_data dataset for creating a scatter plot.

R




# Load ggplot2
library(ggplot2)
 
# Scatter plot with custom shapes
plot <- 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))
 
# Show the plot
plot


Output:

gh

Remove Legend in ggplot2 in R

Now Remove one Legend

R




# Load ggplot2
library(ggplot2)
 
# Scatter plot with custom shapes and without one legend
plot <- 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
# Show the plot
plot


Output:

gh

Remove Legend in ggplot2 in R

  1. Load Libraries: The code starts by loading the ggplot2 library for creating data visualizations.
  2. Create Scatter Plot: Using the ggplot() function, a scatter plot is created with the ‘Category’ variable on the x-axis, ‘Value’ on the y-axis, and points colored by ‘Category’ and shaped by the ‘Shape’ factor. Custom shapes are specified with scale_shape_manual.
  3. Legend Adjustment: The code includes guides(shape = FALSE) to remove the legend associated with the ‘Shape’ aesthetic, simplifying the plot.
  4. Color Scale: The color scale for the points is adjusted using scale_color_viridis_d().
  5. Display Plot: Finally, the plot is displayed, showcasing the scatter plot with custom shapes and one legend removed for enhanced clarity.


Last Updated : 28 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads