How can I add a caption to my ggplot2 plots? 2

How can I add a caption to my ggplot2 plots?

Adding a caption to a ggplot2 plot is a simple and effective way to provide contextual information and enhance the understanding of the displayed data. To add a caption, one can use the “labs()” function in ggplot2 and specify the desired title and subtitle within the parentheses. This function allows for customization of the font, size, and position of the caption, providing a professional and polished appearance to the plot. Additionally, the “ggtitle()” function can be used to add a main title to the plot, further improving the clarity and organization of the information presented. By utilizing these functions, users can easily add captions and titles to their ggplot2 plots, making them more informative and visually appealing.

Add Caption to ggplot2 Plots (3 Examples)


You can use the following methods to add a caption to plots in ggplot2:

Method 1: Add Caption in Default Location

p +
  labs(caption = "This is my caption")

Method 2: Add Caption in Custom Location

p +
  labs(caption = "This is my caption") +
  theme(plot.caption = element_text(hjust=0))

Method 3: Add Caption & Customize Text

p +
  labs(caption = "This is my caption") +
  theme(plot.caption = element_text(size=16, color="red", face="italic"))

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(assists=c(1, 2, 2, 3, 5, 6, 7, 8, 8),
                 points=c(3, 6, 9, 14, 20, 23, 16, 19, 26))

#view data frame
df

  assists points
1       1      3
2       2      6
3       2      9
4       3     14
5       5     20
6       6     23
7       7     16
8       8     19
9       8     26

Example 1: Add Caption in Default Location

The following code shows how to create a scatter plot in gglot2 and add a caption in the default location (bottom right corner below plot):

library(ggplot2)

#create scatter plot with caption in bottom right corner
ggplot(df, aes(x=assists, y=points)) +
  geom_point(size=3) +
  labs(caption = "Based on 2022 Basketball Data")

add caption to plot in ggplot2

Notice that a caption has been added to the bottom right corner outside the plot.

Example 2: Add Caption in Custom Location

The following code shows how to create a scatter plot in gglot2 and add a caption in the bottom left corner below the plot:

library(ggplot2)

#create scatter plot with caption in bottom left corner
ggplot(df, aes(x=assists, y=points)) +
  geom_point(size=3) +
  labs(caption = "Based on 2022 Basketball Data") +
  theme(plot.caption = element_text(hjust=0))

Notice that a caption has been added to the bottom left corner outside the plot.

Note: You can specify hjust=0.5 to place the caption in the bottom center outside the plot.

Related:

Example 3: Add Caption & Customize Text

The following code shows how to create a scatter plot in gglot2 and add a caption with a custom color, font size, and style:

library(ggplot2)

#create scatter plot with custom caption in bottom right corner
ggplot(df, aes(x=assists, y=points)) +
  geom_point(size=3) +
  labs(caption = "Based on 2022 Basketball Data") +
  theme(plot.caption = element_text(size=16, color="red", face="italic"))

caption in ggplot2 with custom text

Notice that a caption has been added to the bottom right corner outside the plot with a custom color, font size, and style.

The following tutorials explain how to perform other common tasks in ggplot2:

Cite this article

stats writer (2024). How can I add a caption to my ggplot2 plots?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-caption-to-my-ggplot2-plots/

stats writer. "How can I add a caption to my ggplot2 plots?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-caption-to-my-ggplot2-plots/.

stats writer. "How can I add a caption to my ggplot2 plots?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-caption-to-my-ggplot2-plots/.

stats writer (2024) 'How can I add a caption to my ggplot2 plots?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-caption-to-my-ggplot2-plots/.

[1] stats writer, "How can I add a caption to my ggplot2 plots?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I add a caption to my ggplot2 plots?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top