In this post, we will see 10 tips to annotate a plot with a title and to customize title text of a plot made with ggplot2. We will start with how to add title to a plot made with ggplot2 using two functions ggtitle() and labs(). And then we will learn how to use ggplot2’s theme() function’s element_text() to change size and add colors to title text. Finally we will learn how to customize title text using ggtext R package.
Let us load the packages needed.
library(tidyverse) theme_set(theme_bw())
We will use the palmer penguin dataset.
penguins %>% head() # A tibble: 6 × 8 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g <fct> <fct> <dbl> <dbl> <int> <int> 1 Adelie Torgersen 39.1 18.7 181 3750 2 Adelie Torgersen 39.5 17.4 186 3800 3 Adelie Torgersen 40.3 18 195 3250 4 Adelie Torgersen NA NA NA NA 5 Adelie Torgersen 36.7 19.3 193 3450 6 Adelie Torgersen 39.3 20.6 190 3650 # ? 2 more variables: sex <fct>, year <int>
When we make a plot with ggplot2, by default, it does not add any title text. Here is an example scatter plot made with ggplot2.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()
We will work with the scatter plot about to add title text.
1. Add title to a plot using ggtitle()
One of the ways we can add a title text to a plot made with ggplot2 is to use ggtitle() function with the text as argument and add it as another layer to our plot.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ ggtitle("Penguins: Body mass vs Flipper length") ggsave("ggplot_title_with_ggtitle.png")
2. Add title to a plot using labs()
Another way we can add a title text to a plot made with ggplot2 is to use labs() function. labs() is more verstaile and we can use it to specify axis label texts in addition to title. We need to use title as argument to labs() function and specify a title text as shown below.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title="Penguins: Body mass vs Flipper length") ggsave("ggplot_title_with_labs.png")
3. Increase title text size using theme’s element_text()
We can increase the text size of the title using theme() function. To change the title text size, we will use plot.title argument with element_text() function.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title="Penguins: Body mass vs Flipper length") + theme(plot.title = element_text(size = 16)) ggsave("increase_ggplot_title_text_size.png")
4. Make title text bold using theme’s element_text()
To make the plot title text bold font we use theme() function with plot.title argument as shown below.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title="Penguins: Body mass vs Flipper length") + theme(plot.title = element_text(face = "bold")) ggsave("make_ggplot_title_text_bold.png")
5. Change title text color using theme’s element_text()
We can use theme() function with plot.title argument to change the color of the title text. In the example below we have set the title text color to purple.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title="Penguins: Body mass vs Flipper length") + theme(plot.title = element_text(color = "purple")) ggsave("change_ggplot_title_text_color.png")
6. Customize a ggplot by changing title text to bold, changing color and larger
In the previous examples, wetshowed how to change title text size, font, and color separately. We can customize title text size, font, and color at the same time with theme() function’s plot.title argument.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title="Penguins: Body mass vs Flipper length") + theme(plot.title = element_text(face = "bold", color = "purple", size = 16)) ggsave("customize_ggplot_title_text_bold_color_size.png")
7. Align title text to the right
Sometimes you might want to change the position of the title text, either place them at the middle or move to the right side. We can place title text using hjust argument within element_text(). In the example below we move the title text to the right side of the plot.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title="Penguins: Body mass vs Flipper length") + theme(plot.title = element_text(face = "bold", color = "purple", size = 16, hjust=1)) ggsave("align_ggplot_title_right_customize_bold_color_size.png")
8. Adjust title text margin
We can also adjust the margin of title text, the space around title with margin argument within element_text(). In the example below, we provide space around the top and bottom of the title text.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title="Penguins: Body mass vs Flipper length") + theme(plot.title = element_text(face = "bold", color = "purple", size = 16, margin = margin(t = 10, b = 10))) ggsave("customize_ggplot_title_text_margin.png")
9. Add colors selectively to title text using ggtext
The R package ggtext, offers numerous functionalities to annotate a plot made with ggplot. We can use ggtext’s element_textbox_simple() function within theme to change text color and size selectively within title text. For example, we have customized part of title text with different color, font family and font size in the example below.
library(ggtext) penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title= "<span style = 'font-size:14pt; font-family:Helvetica;'>Penguins:</span> <span style = 'color:#0072B2;'>bodymass</span> vs <span style = 'color:#D55E00;'>flipper length</span>")+ theme( plot.title = element_textbox_simple( size = 16, lineheight = 1, padding = margin(0, 0, 5, 0) ) ) ggsave("customize_ggplot_title_text_with_colors_font_using_ggtext.png")
10. Handle long title text using ggtext
We can handle long title text, that typically runs over the plot, using ggtext’s element_textbox_simple() function. element_textbox_simple function can naturally adjust long text to fold within the size of the plot.
penguins %>% ggplot(aes(x = body_mass_g, y = flipper_length_mm, color = species))+ geom_point()+ labs(title= "<span style = 'font-size:14pt; font-family:Helvetica;'>Palmer Penguins:</span> Strong linear association between <span style = 'color:#0072B2;'>body mass</span> and <span style = 'color:#D55E00;'>flipper length</span>")+ theme( plot.title = element_textbox_simple( size = 14, lineheight = 1, margin = margin(t = 10, b = 10) #padding = margin(0, 0, 5, 0) ) ) ggsave("customize_long_ggplot_title_text_using_ggtext.png")