• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to wrap long title in ggplot2

datavizpyr · December 13, 2022 ·

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.

A plot with really long title
A plot with really long title

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.

How to wrap long ggplot title with a newline character (\n)
How to wrap long ggplot title with a newline character (\n)

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.

Break a long ggplot2 into two lines using subtitle within labs()
Break a long ggplot2 into two lines using subtitle within labs()

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")
How to change size of title text to make it fit in a line
How to change size of title text to make it fit in a line

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")
How to wrap a long ggplot2 title using stringr's str_wrap() function
How to wrap a long ggplot2 title using stringr’s str_wrap() function

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")
How to wrap a long ggplot title into multiple lines. using ggtext package
How to wrap a long ggplot title into multiple lines using ggtext package

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, R Tagged With: wrap ggplot2 title

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version