• 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 / 7 Tips to customize rectangle elements in ggplot2 element_rect()

7 Tips to customize rectangle elements in ggplot2 element_rect()

June 7, 2021 by cmdlinetips

Continuing the blog post series on how to control theme elements in ggplot2, in this post we will look at how to customize rectangular elements in ggplot2 using element_rect() function.

ggplot2’s theme function helps us fully control the “non-data” elements of a plot made with ggplot2. ggplot2’s theme system comes with multiple element_ functions,

  1. element_text()
  2. element_line()
  3. element_rect()
  4. element_blank()

In earlier posts, we looked at how to customize texts with element_text() and lines with element_line(). In this post, we will learn 7 tips to customize rectangular elements in ggplot2.

Anatomy of ggplot2’s element_rect()

With element_rect(), we can customize all things that are rectangular in a plot. For example, we can customize the line and fill colors of rectangles that define borders and backgrounds in a plot..

Anatomy of Rectangle Elements in ggplot2
Anatomy of Rectangle Elements in ggplot2

Like other theme element functions, to control the color of the rectangle that defines the plot background, we will use “plot.background” element as argument to theme() function and use element_rect() to specify the fill color.

Here is element_rect() function with the available arguments and their default choices. In this tutorial, we will mainly focus on changing color, size, line type and arrow using theme().

element_rect(
  fill = NULL,
  colour = NULL,
  size = NULL,
  linetype = NULL,
  color = NULL,
  inherit.blank = FALSE
)

In total there are 7 aspects of rectangular elements we can control the three broad aspects/elements of lines in a plot made with ggplot2. Here is the list of the elements that we can control using using element_lines().

  1. plot.background: to customize plot background color
  2. panel.background: to customize panel background
  3. panel.border: to customize panel borderx
  4. legend.key: to customize legend key
  5. legend.background: to customize legend background
  6. legend.box.background: to customize legend box background
  7. strip.background: to customize strip background in a facet plot

Simple Scatter plot with legends

Let us start with making a simple scatter plot with default grey background theme.

p<- penguins %>%
  drop_na() %>%
  ggplot(aes(x=flipper_length_mm,
             y=bill_length_mm, 
             color=species,
             shape=sex))+
  geom_point()

p

Here is how the simple scatter plot with legends looks like. And we will learn how to customize using the 7 rectangular theme elements by changing just one element at a time.

Default Scatterplot with Legend in ggplot2

Default Scatterplot with Legend in ggplot2

1. Customizing ggplot2 plot background with plot.background

Note the default plot background, the area around the plot is white. Here we change the default plot background from white to “skyblue” using plot.background=element_rect(fill = “skyblue”).

p + theme(plot.background = element_rect(fill = "skyblue"))
ggsave("customize_plot_background_with_element_rect_ggplot2.png")
Customize Plot Background with element_rect()
Customize Plot Background with element_rect()

2. Customizing ggplot2 panel background with panel.background

We can change the panel background, where the actual graph is displayed, to “linen” color.

p + theme(panel.background =  element_rect(fill = "linen", 
                                           colour = "grey50"))
ggsave("customize_panel_background_with_element_rect_ggplot2.png")
Customize Panel Background with element_rect()
Customize Panel Background with element_rect()

3. Customizing ggplot2 panel border with panel.border

p + theme(panel.border = element_rect(color="purple",
                                      fill=NA))
ggsave("customize_panel_border_with_element_rect_ggplot2.png")
Customize Panel Border with element_rect()
Customize Panel Border with element_rect()

4. Customizing ggplot2 Legend Key with legend.key

p + theme(legend.key = element_rect(fill = "lightyellow", 
                                    color = "blue"))
ggsave("customize_legend_key_with_element_rect_ggplot2.png")
Customize Legend Key with element_rect()
Customize Legend Key with element_rect()

5. Customizing ggplot2 legend background with legend.background

p + theme(legend.background = element_rect(fill ="lightyellow",
                                 color ="black",
                                 linetype ="dashed"))
ggsave("customize_legend_background_with_element_rect_ggplot2.png")

Customize Legend Background with element_rect()
Customize Legend Background with element_rect()

6. Customizing ggplot2 legend box background with legend.box.background

p + theme(legend.box.background = element_rect(color="red",
                                               size=1))
ggsave("customize_legend_box_background_with_element_rect_ggplot2.png")
Customize legend box background with element_rect()
Customize legend box background with element_rect()

7. Customizing ggplot2 strip background in facet plot with strip.background

p + 
  facet_wrap( ~ sex)+
  theme(strip.background = element_rect(color = "purple", 
                                        size = 1))
ggsave("customize_strip_background_facet_wrap_with_element_rect_ggplot2.png")
Customize Strip Background for Faceting with element_rect()
Customize Strip Background for Faceting with element_rect()

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() remove ggplot2 elements with element_blank8 tips to use element_blank() in ggplot2 theme 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: ggplot customize element_rect(), ggplot2, R

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