• 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

Multiple ways to remove legend in ggplot2

datavizpyr · April 19, 2023 ·

In this post, we will learn how to remove legend of a plot made with ggplot2 in 4 different ways. Adding legend to a plot is often helpful to easily understand a plot. However there are scenarios legends can be redundant or we don’t need them.

In ggplot2, we have multiple options to remove legend in a plot. In this tutorial we will see four ways to remove legend with examples.

Let us load the packages needed.

library(tidyverse)
theme_set(theme_bw(16))

How to remove legend in ggplot2

Let us make a plot with ggplot2 with legend added by color argument to aes() in the boxplot below. Note we can also add legend with fill argument to aes() function in the plot belpw.

mpg %>% 
  ggplot(aes(y = fct_reorder(manufacturer, cty), 
             x = cty,
             color=manufacturer))+
  geom_boxplot()+
  labs(y="manufacturer")
ggsave("how_to_remove_legend_in_ggplot.png")
Four ways to remove legend in ggplot
Four ways to remove legend in ggplot

1. Removing legend with theme()

One of the ways to remove legends in a ggplot is to use theme() layer with legend.position=”none’ argument. This will remove all the legends in the plot.

mpg %>% 
  ggplot(aes(y = fct_reorder(manufacturer, cty),  
             x=cty, 
             color=manufacturer))+
  geom_boxplot()+
  labs(y="manufacturer")+
  theme(legend.position="none")
ggsave("remove_legend_in_ggplot_with_theme.png")
How to remove legend in ggplot with theme()
How to remove legend in ggplot with theme()

2. Removing legend with guides()

mpg %>% 
  ggplot(aes(y = fct_reorder(manufacturer, cty), 
             x=cty, 
             color=manufacturer))+
  geom_boxplot()+
  labs(y="manufacturer")+
  guides(color="none")
ggsave("remove_legend_in_ggplot_with_guides.png")
Remove ggplot2 legend with guides()
Remove ggplot2 legend with guides()

Removing legend with show.legend=FALSE inside geom_*()

We can also remove legend using show.legend=FALSE within geom_*. In this example we use show.legend=FALSE argument inside geom_boxplot().

mpg %>% 
  ggplot(aes(y = fct_reorder(manufacturer, cty),
             x = cty, 
             color=manufacturer))+
  geom_boxplot(show.legend=FALSE)+
  labs(y="manufacturer")
ggsave("remove_legend_in_ggplot_with_show_legend.png")
remove legend in ggplot within geom_*. using show_legend()
remove legend in ggplot within geom_*. using show_legend()

Removing legend with ggeasy’s easy_remove_legend()

Fourth way to remove legend that may be easy to remember is to use ggeasy package and use the function easy_remove_legend().

library(ggeasy)
mpg %>% 
  ggplot(aes(y = fct_reorder(manufacturer, cty), 
             x=cty, 
             color=manufacturer))+
  geom_boxplot()+
  labs(y="manufacturer")+
  easy_remove_legend()
ggsave("remove_legend_in_ggplot_with_ggeasy.png")
remove legend in ggplot2 using. ggeasy
remove legend in ggplot2 using. ggeasy

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? Default ThumbnailHow to Move Facet strip label to the bottom Default ThumbnailHow to change axis tick label size in ggplot2

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

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