How to Add a ggplot2 Title & Subtitle with Different Size & Color in R (Example Code)

This tutorial illustrates how to change size and color of ggplot2 titles in the R programming language.

Preparing the Example

my_df <- data.frame(x = 1:5,            # Example data
                    y = c(2, 1, 4, 2, 3))
install.packages("ggplot2")               # Install & load ggplot2 package
library("ggplot2")
my_ggp <- ggplot(my_df, aes(x, y)) +    # ggplot2 plot with default titles
  geom_line() +
  labs(title = "This is the Title",
       subtitle = "This is the Subtitle")
my_ggp                                  # Draw plot

r graph figure 1 how to add a ggplot2 title & subtitle different size & color r

Example: Modify Size & Color of ggplot2 Title & Subtitle

my_ggp +                                # Change colors & size
  theme(plot.title = element_text(size = 30, color = "red"),
        plot.subtitle = element_text(size = 15, color = "green"))

r graph figure 2 how to add a ggplot2 title & subtitle different size & color r

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top