7 Tips to customize rectangle elements in ggplot2 element_rect()

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

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

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

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()

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()

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()

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()

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()

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()

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()