As you can imagine, data analysts use a ton of libraries in their work. For example, we use the tidyverse to read in our data and create new objects, and for data visualisation we use ggplot2 and its associated themes. Themes such as tidyplot2, thenewtidy, and the tidyverse itself.

Themes are usually a great way to make a blog look more consistent and inviting, but if you use them, you can end up with a bunch of lines or other visual elements that are redundant. In the example below, I’m going to show you how you can use the built-in ggplot2 theme in R to make your theme more consistent.

The theme I will be discussing is ggplot2. It is a very popular and powerful themes used primarily by data geeks but also found in many others’ work.

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. ggplot2 theme element blank tips word-image-2715

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”) Scatterpot with legend, title, tag and caption with default theme word-image-2716

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 word-image-2717

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 word-image-2718

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 word-image-2719

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 word-image-2720

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 word-image-2721

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 word-image-2722

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() word-image-2723A new and unique way to create plots is available for you in ggplot2, with the function element_blank(). The ggplot2 team is working on a very nice and useful library called geom_blank() which can be used in place of the popular base::element_blank() plot function in ggplot2.. Read more about ggplot2 themes and let us know what you think.

Related Tags:

best ggplot themesggplot themes cheat sheetggthemesggplot2 themesggplot2 themes galleryggplot themes for publication,People also search for,Privacy settings,How Search works,best ggplot themes,ggplot themes cheat sheet,ggthemes,ggplot2 themes,ggplot2 themes gallery,ggplot themes for publication,ggplot themes package,ggplot scientific theme

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *