In this post, we will learn how to wrap a long title into multiple lines in ggplot2. When your title to a plot made. with ggplot2 is really long, you only see a part of it in the plot. Here we will see examples of dealing with long ggplot2 title in four different ways.
We will use palmer penguins dataset to make a plot with really long title.
library(tidyverse) library(palmerpenguins) theme_set(theme_bw(16))
A violin plot with a long title text
Let us make a violin plot using Palmer penguins dataset with a long title that does not fit in a single line.
penguins %>% ggplot(aes(x=species, y= body_mass_g, fill=species))+ geom_violin()+ theme(legend.position="none")+ labs(title = "Distributions of Body Mass of three species of penguins from Palmer Penguin Data") ggsave("ggplot2_with_long_title.png")
We can see that the long title get cuts off the plot.
Breaking the long title with a new line character
One of the solutions to wrap a long title is to use new line character “\n” at the right place and break the title into two lines. Note the “\n” symbol within title text.
penguins %>% ggplot(aes(x=species, y= body_mass_g, fill=species))+ geom_violin()+ theme(legend.position="none")+ labs(title = "Distributions of Body Mass of three species\nof penguins from Palmer Penguin Data") ggsave("ggplot2_folding_long_title_with_newline_character.png")
And here is how it looks after breaking the long title into two lines.
Two line title with title and subtitle
Another hack to deal with a long title is to manually split into two lines, then using the first line with title argument to labs() and the second line with subtitle argument to labs().
penguins %>% ggplot(aes(x=species, y= body_mass_g, fill=species))+ geom_violin()+ theme(legend.position="none")+ labs(title = "Distributions of Body Mass of three species", subtitle="of penguins from Palmer Penguin Data") ggsave("ggplot2_folding_long_title_using_subtitle.png")
This is not fully satisfactory, as the size of subtitle text will be smaller than the title text. And also we need to manually break the long text.
Reducing the size of title text using element_text()
A crude solution that might work sometimes is to reduce the title text’s size. Here we use element_text() function to reduce the size. Note that this approach tries to fit the text in a single line instead of wrapping it into multiple lines.
penguins %>% ggplot(aes(x=species, y= body_mass_g, fill=species))+ geom_violin()+ labs(title = "Distributions of Body Mass of three species of penguins from Palmer Penguin Data")+ theme(legend.position="none", plot.title = element_text(size = 10)) ggsave("ggplot2_reduce_size_of_long_title_to_fit.png")
Wrapping the long title with stringr’s str_wrap() function
One of the better solutions to wrap a long title text is to use stringr’s str_wrap() function and fold the text by specifying the width we would like.
Here is an example of using str_wrap() function to wrap a long title text.
penguins %>% ggplot(aes(x=species, y= body_mass_g, fill=species))+ geom_violin()+ labs(title = stringr::str_wrap("Distributions of Body Mass of three species of penguins from Palmer Penguin Data", width=50)) + theme(legend.position="none") ggsave("ggplot2_wrap_long_title_with_stringr_str_wrap.png")
Wrapping the long title with ggtext’s element_textbox_simple() function
Another better solution is to use the element_textbox_simple() function in ggtext package. element_textbox_simple() in ggtext package is versatile with multiple options, here we use it to just wrap the title text automatically into multiple lines.
library(ggtext) penguins %>% ggplot(aes(x=species, y= body_mass_g, fill=species))+ geom_violin()+ theme(legend.position="none")+ labs(title = "Distributions of Body Mass of three species of penguins from Palmer Penguin Data")+ theme(plot.title = element_textbox_simple()) ggsave("ggplot2_wrap_long_title_with_ggtext_element_textbox_simple.png")