• 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 selectively remove legends with ggplot2

datavizpyr · October 29, 2022 ·

In this post, we will see examples of removing some of the legends from a plot having multiple legends made with ggplot2. Legends in a plot help us understand the relationship between two variables in a plot with respect to other variables in the dataset.

We will learn that we can remove all legends in a plot using theme(legend.position=”none”). We will show how to use guides() function to remove legends selectively. We can selectively

  • remove legend from color using guides(color=”none”),
  • remove legend from fill argument using guides(fill=”none”),
  • remove legend from shape using guides(shape=”none”),
  • remove legend from size using guides(size=”none”).

Let us get started by loading the packages needed.

library(palmerpenguins)
theme_set(theme_bw(18))
library(tidyverse)

We will use random sample of Palmer Penguin dataset to make a plot with multiple legend groups.

# <a href="https://cmdlinetips.com/2022/07/randomly-select-rows-from-dataframe-dplyr/">randomly sample rows using sample_n()</a>
df <-  penguins %>%
  drop_na() %>%
  sample_n(50)

Our data for showing how to selectively remove legend looks like this.

df %>%
  head()

# A tibble: 6 × 8
  species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex  
  <fct>   <fct>           <dbl>         <dbl>            <int>       <int> <fct>
1 Gentoo  Biscoe           49.1          14.8              220        5150 fema…
2 Chinst… Dream            46.4          17.8              191        3700 fema…
3 Adelie  Dream            38.1          18.6              190        3700 fema…
4 Adelie  Biscoe           38.1          17                181        3175 fema…
5 Gentoo  Biscoe           45.4          14.6              211        4800 fema…
6 Chinst… Dream            51.3          19.9              198        3700 male 
# … with 1 more variable: year <int>

First, let us make a boxplot with multiple legends. In this example, we make a boxplot between species and flipper length of penguins. We use color legend to highlight the species variable, shape legend for showing sex and size for body mass.

df %>%
  ggplot(aes(x=species, 
             y=flipper_length_mm, 
             color=species))+
  geom_boxplot(outlier.shape=NA)+
  geom_jitter(width=0.1, alpha=0.5,aes(shape=sex, size=body_mass_g))
ggsave("boxplot_with_multiple_legends.png")
A plot made with multiple legends
A plot with multiple legends

Remove all legends using theeme(legend.position=”none”)

Using theme(legend.position=”none”) as additional layer to our ggplot2 code we can remove all the legends at once.

df %>%
  ggplot(aes(x=species, 
             y=flipper_length_mm, 
             color=species))+
  geom_boxplot(outlier.shape=NA)+
  geom_jitter(width=0.1, alpha=0.5,aes(shape=sex, size=body_mass_g))+
  theme(legend.position="none")
ggsave("remove_all_legends_with_theme.png")
remove legends with theme
Remove all legends with theme()

Selectively remove legend created by color

Using guides(color=”none”) as additional layer to our ggplot2 code we can remove. legend created by color argument within aes().

#. Selectively Remove a legend: Example 1
df %>%
  ggplot(aes(x=species, 
             y=flipper_length_mm, 
             color=species))+
  geom_boxplot(outlier.shape=NA)+
  geom_jitter(width=0.2, alpha=0.5,aes(shape=sex, size=body_mass_g))+
  guides(color="none")
ggsave("selectively_remove_a_legend_with_guides.png")
Selectively Remove Color Legend with guides()
Selectively Remove Color Legend with guides()

Selectively remove legend created by shape argument

Similarly, we can use guides(shape=”none”) as additional layer to our ggplot2 code we can remove legend created by shape argument.

df %>%
  ggplot(aes(x=species, 
             y=flipper_length_mm, 
             color=species))+
  geom_boxplot(outlier.shape=NA)+
  geom_jitter(width=0.2, alpha=0.5,aes(shape=sex, size=body_mass_g))+
  guides(shape="none")
ggsave("selectively_remove_a_legend_with_guides_example2.png")
Selectively Remove Shape Legend with guides()
Selectively Remove Shape Legend with guides()

Selectively remove multiple legends

We can also use guides() function to selectively remove multiple legends, but not all, by specifying the arguments that created the legends. For example, in the example below we remove the legends created by size and color arguments using guides(size=”none”, color=”none”).

df %>%
  ggplot(aes(x=species, 
             y=flipper_length_mm, 
             color=species))+
  geom_boxplot(outlier.shape=NA)+
  geom_jitter(width=0.2, alpha=0.5,aes(shape=sex, size=body_mass_g))+
  guides(size="none", color="none")
ggsave("selectively_remove_two_legends_with_guides.png")
Selectively remove two legends with guides()
Selectively remove two legends with guides()

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? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2 Tagged With: selectively remove legends in ggplot2

Reader Interactions

Comments

  1. Joel Laia says

    November 3, 2022 at 1:15 pm

    Hi there.

    I have a question regarding the post “How to Connect Data Points on Boxplot with Lines?”, June 8, 2020. This post had no room for comments.

    By creating the pipeline for the dataframe with the “paired” column, my rows were paired randomly, instead to the values of the column I chose.

    I simply switched the variable names to mine, ID number = country, and my own x and y, respectively, year and lifeExp.

    What can I do to pair my rows according to the ID number?

    Thank you in advance,

    Joel Laia

    • datavizpyr says

      November 3, 2022 at 11:10 pm

      May be I have not fully understood the question. I think you can use groupby on ID and create group numbers using one of the context dependent expression for creating paired data https://dplyr.tidyverse.org/reference/context.html

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