This article describes how to add and change a main title, a subtitle and a caption to a graph generated using the ggplot2 R package. We’ll show also how to center the title position, as well as, how to change the title font size and color.
In this R graphics tutorial, you will learn how to:
- Add titles and subtitles by using either the function ggtitle() or labs().
- Add caption to a ggplot and change the position.
- Split a long title into two lines or more using \n as a text separator.
- Change the font appearance (text size, color and face) of titles and caption. For example, to set a bold ggplot title, use this:
p + theme(plot.title = element_text(face = "bold"))
. The allowed values for the font face include: “plain”, “italic”, “bold” and “bold.italic”. - Change title position to the center or to any other locations (left, right). The default ggplot title alignment is not centered. It is on the left. It’s possible to put the title in the middle of the chart by specifying the argument
hjust = 0.5
in the functionelement_text()
:p + theme(plot.title = element_text(hjust = 0.5))
. The optionshjust = 1
andhjust = 0
place titles on the right and the left side of the plot, respectively.
Contents:
Key ggplot2 R functions
- Start by creating a box plot using the
ToothGrowth
data set:
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_boxplot()
- Add titles using one of the following functions:
- p + ggtitle(“Main title”, subtitle = “My subtitle”)
- p + labs(title = “Main title”, subtitle = “My subtitle”, caption = “My caption”)
- Key ggplot2 theme options to change the font of titles and captions:
theme(
plot.title = element_text(),
plot.subtitle.title = element_text(),
plot.caption = element_text()
)
Arguments of the function element_text()
includes:
- color, size, face, family: to change the text font color, size, face (“plain”, “italic”, “bold”, “bold.italic”) and family.
- lineheight: change space between two lines of text elements. Number between 0 and 1. Useful for multi-line plot titles.
- hjust and vjust: number in [0, 1], for horizontal and vertical adjustment of titles, respectively.
hjust = 0.5
: Center the plot titles.hjust = 1
: Place the plot title on the righthjust = 0
: Place the plot title on the left
Center the title position
Specify the argument hjust = 0.5
in the function element_text()
to put the title in the middle:
p + theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
Other locations adjustment for titles include:
hjust = 1
: Place the title on the righthjust = 0
: Place the title on the left
Conclusion
- Create an example of ggplot:
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_boxplot()
- Add titles, subtitles and captions:
# Add titles and a caption
p + labs(title = "Effect of Vitamin C on Tooth Growth",
subtitle = "Plot of length by dose",
caption = "Data source: ToothGrowth")+
theme(
plot.title = element_text(hjust = 0.5, size = 14), # Center title position and size
plot.subtitle = element_text(hjust = 0.5), # Center subtitle
plot.caption = element_text(hjust = 0, face = "italic")# move caption to the left
)
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
How to add multiple subtitles and captions with ggplot?
You can display multiple line subtitles by separating long text using “\n”:
What codes should I use if I want the axis titles to be bold?
`plot.subtitle.title = element_text()` should be
`plot.subtitle = element_text()`