• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / 8 tips to use element_blank() in ggplot2 theme

8 tips to use element_blank() in ggplot2 theme

June 13, 2021 by cmdlinetips

In this tutorial, we will see examples of using element_blank() to control the look of non-data aspects of a plot made with ggplot2. This is fourth in the series of understanding how to change four different elements of ggplot2’s theme systems.
  1. element_text()
  2. element_line()
  3. element_rect()
  4. element_blank()

We saw how to use element_text() to change the looks of text in a plot, how to use element_line() to change line elements of a plot and how to use element_rect() to customize the rectangular elements of a plot made with ggplot2. Here we will take a look at the fourth and final ggplot2’s theme element, element_blank().

What does element_blank() do?

If you look at the ggplot2′ theme documentation page, you can see that element_blank() does not take any arguments. So what does it do? As it says element_blank()

draws nothing, and assigns no space.

We can use element_blank() if we don’t want any specific non-data plot elements drawn on the plot. In the three posts, we saw 32 tips in total to customize three ggplot2 theme elements. With element_blank() we can suppress or remove change each one of them.

In this tutorial, we will first make scatter plot with default theme, but with legend, tag and caption. Then we will show how can we use element_blank() to suppress some the elements of the plot. At first, we will remove one element at time to illustrate the use of element_blank and then finally suppress multiple elements of the plot using element_blank for each feature of the plot. As the plot below shows, we will focus on just a few elements of the plot and use element_blank to remove it from the plot.

Remove ggplot2 theme elements with element_blank()
ggplot2 theme element blank tips

Scatter plot with default ggplot2 theme elements

Let us use Palmer Penguins data to make a scatter plot with default ggplot2 theme, but with legend, title, subtitle, caption and tag to annotate the plot.

library(tidyverse)
library(palmerpenguins)

We will store the plot as a variable and use the variable to modify the plot using element_blank for various options.

p <- penguins %>%
  drop_na() %>%
  ggplot(aes(x=flipper_length_mm,
             y=bill_length_mm, 
             color=species,
             shape=sex))+
  geom_point()+
  labs(title="Flipper length vs Bill length",
       subtitle="Palmer Penguins",
       caption="cmdlinetips.com",
       tag="A")

p
ggsave("scatterplot_ggplot2_default.png")
Scatterplot with legend, title, tag and caption
Scatterpot with legend, title, tag and caption with default theme

Remove Major Grid Lines with element_blank()

To remove or suppress the major grid lines we see in the default scatter plot, we can specify panel.grid.major argument to element_blank().

p + theme(panel.grid.major=element_blank())
ggsave("remove_major_grid_lines_with_element_blank_ggplot2_theme.png")

How to Remove Major Grid Lines in ggplot2 with element_blank
How to Remove Major Grid Lines in ggplot2

Remove Minor Grid Lines with element_blank()

Similarly, to remove or suppress the minor grid lines we see in the default scatter plot, we use element blank with panel.grid.minor.

# Remove Minor Grid Lines
p + theme(panel.grid.minor=element_blank())
ggsave("remove_minor_grid_lines_with_element_blank_ggplot2_theme.png")
How to Remove Minor Grid Lines in ggplot2
How to Remove Minor Grid Lines in ggplot2

Remove Panel Background with element_blank()

By default ggplot2 make the plot with grey background, we can remove the grey background, using panel.background=element_blank().

# Remove Panel Background
p + theme(panel.background = element_blank())
ggsave("remove_panel_background_with_element_blank_ggplot2_theme.png")
How to Remove Panel Background with element_blank
How to Remove Panel Background with element_blank

Remove Axis Ticks with element_blank()

# Remove Axis Ticks
p + theme(axis.ticks = element_blank())
ggsave("remove_axis_ticks_with_element_blank_ggplot2_theme.png")

How to Remove Axis Ticks in ggplot2
How to Remove Axis Ticks in ggplot2

Remove Axis Text with element_blank()

p + theme(axis.text = element_blank())
ggsave("remove_axis_text_label_with_element_blank_ggplot2_theme.png")
How to remove axis text label in ggplot2
How to remove axis text label in ggplot2

Remove Legend Key with element_blank()

# Remove Legend Key
p + theme(legend.key=element_blank())
ggsave("remove_legend_key_with_element_blank_ggplot2_theme.png")
How to remove legend key in ggplot2

Remove Multiple Theme Elements with element_blank()

p + theme(panel.background = element_blank(),
          axis.ticks = element_blank(),
          axis.text = element_blank(),
          legend.key=element_blank(),
          plot.tag=element_blank(),
          plot.caption=element_blank())
ggsave("remove_multiple_theme_elements_with_element_blank_ggplot2_theme.png")

Remove Multiple Theme Elements with element_blank()
Remove Multiple Theme Elements with element_blank()

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Tips to Customize line elements in ggplot215 Tips to Customize lines in ggplot2 with element_line() Anatomy of Rectangle Elements in ggplot27 Tips to customize rectangle elements in ggplot2 element_rect() Tips to Customize Text in ggplot2 plot10 Tips to Customize Text Color, Font, Size in ggplot2 with element_text() Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R

Filed Under: R, R Tips, tidyverse 101 Tagged With: ggplot2 element_blank, R, R Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version