• 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
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to Remove Legend in ggplot2

datavizpyr · February 11, 2020 ·

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")

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? Default ThumbnailHow 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

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

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

Go to mobile version