10 Tips to Customize Text Color, Font, Size in ggplot2 with element_text()

Tips to Customize Text in ggplot2 plot
Tips to Customize Text in ggplot2 plot with element_text()

ggplot2’s theme system give us a great control over how the “non-data” elements of a plot should look like. The theme system helps elevate the plot you make by making finer changes and make it easy to look better. ggplot2’s theme system comes with multiple element_ functions,

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

And they help control how the non-data elements of a plot should like. In this tutorial we will focus on the theme element element_text(). We will learn 10 tips to have finer control over the “font size, colour and face” for many part of text elements in a plot, like title, subtitle, labels, and legend. We will use theme() function in combination with different elements associated with the plot.

Anatomy of ggplot2’s element_text()

While making a plot with ggplot2, it automatically chooses appropriate values for various aspects of element text. In total there are 10 (at least?) different aspects/elements of texts in a plot made with ggplot2. The figure below shows the anatomy of text elements and the key word in ggplot2 describing the element (Phew…My life time goal of making an “anatomy of something” plot is achieved :-)) .

With element_text(), we can customize the looks of the the texts. For example, to control the look of title of a plot, we will use “plot.title” element as argument to theme() function and use element_text() to specify the color, font and size of the plot title.

Tips to Customize Text in ggplot2 plot with element_text()

Here is the list of the elements (most common?) that we can control using using element_text().

  1. axis.title.x: to customize x-axis label/title
  2. axis.title.y : to customize y-axis label/title
  3. axis.text.x : to customize x-axis tick labels
  4. axis.text.y : to customize y-axis tick labels
  5. legend.title: to customize legend title text
  6. legend.text: to customize legend text
  7. plot.title: to customize plot title
  8. plot.subtitle: to customize plot subtitle
  9. plot.caption: to customize plot’s caption
  10. plot.tag: to customize plot’s tag

Let us get started by making a scatter plot using ggplot2 with common text annotations like title, subtitle, legend and tags.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

We use Palmer penguins data make the scatterplot. Check out the labs() function for variation addition to the plot. We use theme_set() option to make the plot in black and white theme with pre-specified font sizes.

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

p

And this is how the scatter plot looks like this. We have saved the code to make the plot as a variable and use it use it to customize 10 different text elements of ggplot2’s theme using element_text() function.

How To Customize ggplot2’s text with element_text()?

Using element_text() element, we can modify multiple aspects of the text as shown below.

element_text(
  family = NULL,
  face = NULL,
  colour = NULL,
  size = NULL,
  hjust = NULL,
  vjust = NULL,
  angle = NULL,
  lineheight = NULL,
  color = NULL,
  margin = NULL,
  debug = NULL,
  inherit.blank = FALSE
)

However, in this post we will mainly focus how to customize, color of the text, size of the text, font family and font face (bold font or not).

1. Customizing ggplot2 x-axis label with axis.title.x

We can use axis.title.x element of element_text() to change the color, size and angle of the x-axis label text or title.

p + theme(axis.title.x = element_text(size=16, 
                                      color="purple", 
                                      face="bold",
                                      angle=0))
ggsave("customize_x_axis_title_with_element_text_theme_ggplot2.png")

In this example, we set the size=16, color=”purple” and bold font for x-axis label.

Customize x-axis title

2. Customizing ggplot2 y-axis label with element_text()

We can use axis.title.y element of element_text() to change the color, size and angle of the y-axis label text or title.

p + theme(axis.title.y = element_text(size=16, 
                                      color="purple", 
                                      face="bold",
                                      angle=90))
ggsave("customize_y_axis_title_with_element_text_theme_ggplot2.png")

In this example, we set the size=16, color=”purple” angle=90 and bold font for y-axis label.

Customize y-axis title

3. Customizing ggplot2 x-tick marks with element_text()

We can use axis.text.x element of element_text() to change the color, size and angle of the x-axis tick label text.

# x axis tick mark labels
p + theme(axis.text.x= element_text(family = "Tahoma", 
                                         face="bold", colour="black",
                                         size=10))
ggsave("customize_x_axis_tick_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=10, color=”black” and bold font for x-axis tick label. In addition, we also change the Tahoma font using family argument.

Customize x-axis tick text ggplot2

4. Customizing ggplot2 y-tick marks with element_text()

We can use axis.text.y element of element_text() to change the color, size and angle of the y-axis tick label text.

# y axis tick mark labels
p + theme(axis.text.y = element_text(family = "Tahoma",
                                     face="bold",
                                     colour="black",
                                     size=10))
ggsave("customize_y_axis_tick_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=10, color=”black” and bold font for x-axis tick label.

Customize y-axis tick text with element_text()

5. Customizing ggplot2 legend title text with element_text()

We use legend.title element of element_text() to change the color, size and angle of legend’s title text.

p + theme(legend.title=element_text(color="purple",
                                         face="bold",size=12))
gsave("customize_legend_title_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=12, color=”purple” and bold font for legend title.

Customize legend title text with element_text()

6. Customizing ggplot2 legend text with element_text()

We use legend.text element of element_text() to change the color, size and angle of legend’s title text.

#legend.text = element_text(color, size, face)
p + theme(legend.text=element_text(face="bold", color="green",size=10))
ggsave("customize_legend_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=12, color=”green” and bold font for legend text.

Customize legend text with element_text()

7. Customizing ggplot2 title text with element_text()

We use plot.title element of element_text() to change the color, and size of the plot’s title text. Here, we set the size=24, color=”blue” and bold font for plot title.

#library(extrafont)
p + theme(plot.title= element_text(size=24,
                                   color="blue",
                                   face="bold",
                                   family = "Tahoma"))
ggsave("customize_plot_title_with_element_text_theme_ggplot2.png")
Customize plot title with element_text()

8. Customizing ggplot2 subtitle text with element_text()

We use plot.subtitle element of element_text() to change the color, and size of the plot’s title text. Here, we set the size=16, color=”red” and bold font for the plot’s subtitle.

p + theme(plot.subtitle= element_text(size=16,
                                      color="red",
                                      face="bold"))
ggsave("customize_plot_subtitle_with_element_text_theme_ggplot2.png")
Customize plot subtitle with element_text()

9. Customizing ggplot2 caption with element_text()

To change the plot’s caption text’s size, color and face, we can use plot.caption element.

p + theme(plot.caption= element_text(size=16,
                                   color="pink",
                                   face="bold"))
ggsave("customize_plot_caption_with_element_text_theme_ggplot2.png")
Customize plot caption with element_text()

10. Customizing ggplot2 tag with element_text()

Similarly, to change the plot’s tag size, color and face, we can use plot.tag element with element_text() function.

p + theme(plot.tag = element_text(size=16,
                                   color="red",
                                   face="bold"))
ggsave("customize_plot_tag_with_element_text_theme_ggplot2.png")
Customize plot tag with element_text()