• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How to Remove Legend in ggplot2

datavizpyr · February 11, 2020 ·

Last updated on September 14, 2021

In this post, we will learn how to remove a legend from a plot made with ggplot2 in R. There are a few ways to remove legend in ggplot2. We will see examples using two functions in ggplot2 to remove legend from a plot. We will first use theme() function to remove legend in ggplot2 and then see an example using guides() function to remove legend.

Load tidyverse and data

First, let us load tidyverse and gapminder package to make a plot using ggplot2 with legend. Let us also choose ggplot2 theme to theme_bw() and this mainly will change background color to white from default grey.

library(gapminder)
library(tidyverse)
theme_set(theme_bw(base_size = 16))

Simple plot with legend in ggplot2

When you make a plot with ggplot2, it adds a legend automatically when you use three variables in aes() function in a plot. Typically, the first two variables will be x and y axis and the third variable can be fill, color, shape, or size. Sometimes having a legend cane be redundant and you might want to remove legend.

Let us make a boxplot using ggplot2 and fill the boxes by a variable. This adds a legend automatically.

gapminder %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent)) +
  geom_boxplot()
A ggplot with legend
A ggplot with legend

How To Remove Legend in ggplot using theme()

We can see that in the above boxplot, the legend is on the right hand side of the plot and it is redundant as it gives the same information on the x-axis. We can improve the plot by removing the legend from the plot. One of the ways to remove legend from ggplot2 is to use theme() function as another layer with argument legend.position = “None”.

gapminder %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent)) +
  geom_boxplot() +
  # remove legend with theme()
  theme(legend.position = "None")+
  labs(subtitle="Removing Legend with theme(legend.position = 'None')")
Remove Legend in ggplot using theme()
How to remove legend in ggplot with theme?

How To Remove Legend in ggplot2 using guides()

Another way to remove legend from ggplot2 is to use guides() function with specific argument depending on the type of ggplot2.

For example, if we make a boxplot with fill argument, we use “guides(fill=FALSE)” to remove legend.

gapminder %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent)) +
  geom_boxplot() +
  # remove legend using guides()
  guides(fill=FALSE)+
  labs(subtitle="Removing Legend with guides(fill=FALSE)")
How to remove legend in ggplot?
How to remove legend in ggplot with guides()?

Similarly, if we have a boxplot with color argument, we use “guides(color=FALSE)” to remove legend.

gapminder %>%
  ggplot(aes(x=continent, y=lifeExp, color=continent)) +
  geom_boxplot() +
  guides(color=FALSE)

Note the advantage of using theme(legend.position = “None”) is that it can remove legend irrespective of the type of ggplot2.

gapminder %>%
  ggplot(aes(x=continent, y=lifeExp, color=continent)) +
  geom_boxplot() +
  theme(legend.position = "None")

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

Related posts:

Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? Customizing Legend Inside Scatter Plot ggplot2How To Place Legend Inside the Plot with ggplot2? How to move the facet strip label to the bottom in ggplot2How to Move Facet strip label to the bottom Default ThumbnailHow to change axis tick label size in ggplot2

Filed Under: ggplot2, R, remove legend ggplot2 Tagged With: remove legend ggplot2

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version