Title and Subtitle With Different Size

To change the size of the title and subtitle, we add the theme() function to labs() or ggtitle() function, whatever you used. Here we use labs() function. Inside theme() function, we use plot.title parameter for doing changes in the title of plot and plot.subtitle for doing changes in Subtitle of Plot. We use element_text() function as a value of plot.title and plot.subtitle parameter. We can change the appearance of texts using element_text() function. To change the size of the title and subtitle, we use the size parameter of element_text() function. Here we set the size of the title as 30 and the size of the subtitle as 20.

Below is the implementation:

R




library(ggplot2)
  
data <- data.frame(
  Name = c("A", "B", "C", "D", "E") ,  
  Value=c(3, 12, 5, 18, 45)
)
  
# Create a BarPlot with title
# of size 30 and subtitle of size 20
ggplot(data, aes(x = Name, y = Value)) + 
  geom_bar(stat = "identity", fill = "green")+
  labs(title = "Title For Barplot",
       subtitle = "This is Subtitle"
       )+
  theme(plot.title = element_text(size = 30),
        plot.subtitle = element_text(size = 20)
        )


Output:

Title and Subtitle with Different size

ggplot2 – Title and Subtitle with Different Size and Color in R

A Title and the subtitle to a plot give a piece of information about the graph that what the graph actually wants to represent. This article describes how to add a Title and Subtitle with Different Sizes and Colors using ggplot2 in R Programming.

To add a Title and Subtitle within a plot, first, we have to import ggplot2 library using library() function. If you have not installed yet, you can simply install it by writing a command install.packages(“ggplot2”) in R Console.

library(ggplot2)

Consider the following data for the example:

data <- data.frame(
  name=c("A","B","C","D","E") ,  
  value=c(3,12,5,18,45)
)
  Name Value
1 A 3
2 B 12
3 C 5
4 D 18
5 E 45

Creating a Plot using ggplot() function with the value of X-axis as Name and Y-axis as Value and make it a barplot using geom_bar() function of the ggplot2 library. Here we use the fill parameter to geom_bar() function to color the bars of the plots.

R




# Load Package
library(ggplot2)
  
# Create a Data
data <- data.frame(
  Name=c("A", "B", "C", "D", "E") ,  
  Value=c(3, 12, 5, 18, 45)
)
  
# Create a Simple BarPlot with green color.
ggplot(data, aes(x = Name, y = Value)) + 
  geom_bar(stat = "identity", fill = "green")


Output:

Similar Reads

Adding Title and Subtitle To R Plot

...

Title and Subtitle With Different Size

Method 1. By Using ggtitle() function:...

Title and Subtitle With Different Color

...