Skip to content

Data Science Tutorials

For Data Science Learners

  • How to move from Junior Data Scientist
    How to move from Junior Data Scientist Machine Learning
  • Creating a Histogram of Two Variables in R R
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • Changing the Font Size in Base R Plots
    Changing the Font Size in Base R Plots R
  • Add Footnote to ggplot2 R
  • Survival Plot in R
    How to Perform a Log Rank Test in R R
  • How to Display Percentages on Histogram IN R
    How to Display Percentages on Histogram in R R
  • How to test the significance of a mediation effect
    How to test the significance of a mediation effect R
How to Add a title to ggplot2 Plots in R

How to Add a caption to ggplot2 Plots in R?

Posted on September 1September 1 By Admin No Comments on How to Add a caption to ggplot2 Plots in R?

How to Add a caption to ggplot2 Plots in R?, the graphs in ggplot2 can have captions added using one of the methods listed below.

With the following data frame in R, the following examples demonstrate how to use each technique in practice.

A Side-by-Side Boxplot in R: How to Do It – Data Science Tutorials

Let’s create a data frame

df <- data.frame(x=c(1, 2, 2, 3, 4, 5, 6, 7, 8),
y=c(23, 36, 39, 31, 32, 33, 36, 39, 23))

Now we can view the data frame

df
  x  y
1 1 23
2 2 36
3 2 39
4 3 31
5 4 32
6 5 33
7 6 36
8 7 39
9 8 23

Example 1: Add Caption in Default Location

The code that follows demonstrates how to make a scatter plot in gglot2 and insert a caption in the plot’s default place (the bottom right corner):

library(ggplot2)

Let’s create a scatter plot with a caption in the bottom left corner

ggplot(df, aes(x=x, y=y)) +
  geom_point(size=3) +
  labs(caption = "Based on 2022 Asia Cup Data")

A caption has been added to the lower right corner of the image outside the plot, as you can see.

Change ggplot2 Theme Color in R- Data Science Tutorials

Example 2: Add Caption in Custom Location

The code below demonstrates how to make a scatter plot in gglot2 and add a caption to the plot’s bottom left corner.

library(ggplot2)

Now we can create a scatter plot with a caption in the default location

ggplot(df, aes(x=x, y=y)) +
  geom_point(size=3) +
  labs(caption = "Based on 2022 Asia Cup Data") +
  theme(plot.caption = element_text(hjust=0))

The lower left corner of the image outside the plot now has a caption.

Note: You can use hjust=0.5 to center the caption outside of the plot in the bottom right corner.

How to Get a Job as a Data Engineer? – Data Science Tutorials

Example 3: Customize Text & Add Caption

The gglot2 code below demonstrates how to make a scatter plot and add a caption with a personalized color, font size, and style.

library(ggplot2)

Let’s try to create a scatter plot with a caption in the default location

ggplot(df, aes(x=x, y=y)) +
  geom_point(size=3) +
  labs(caption = "Based on 2022 Asia Cup Data") +
  theme(plot.caption = element_text(size=16, color="red", face="italic"))

A caption with a unique color, text size, and style has been added to the bottom right corner of the image outside the plot.

Top 10 online data science programs – Data Science Tutorials

Check your inbox or spam folder to confirm your subscription.

R Tags:ggplot2

Post navigation

Previous Post: Top 10 online data science programs
Next Post: How to Implement the Sklearn Predict Approach?

Related Posts

  • Checking Missing Values in R
    Checking Missing Values in R R
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • Understanding Machine Learning and Data Science R
  • Select the First Row by Group in R
    Select the First Row by Group in R R
  • How to convert characters from upper to lower case in R
    How to convert characters from upper to lower case in R? R
  • Replace NA with Zero in R
    Replace NA with Zero in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • Best Prompt Engineering Books R
  • Box Cox transformation in R
    Box Cox transformation in R R
  • Top Data Science Examples You Should Know 2023
    Top Data Science Applications You Should Know 2023 Machine Learning
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • Best Data Visualization Books Course
  • Descriptive statistics in R R
  • Wrap a character string in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme