I am looking to create a graph with ggplot2
, containing:
I tried using the code below, which is placing the sub-subtitle on a position which has a distance from the subtitle:
# Common packages loading
library(ggplot2)
library(reshape)
library(readxl)
library(ggrepel)
library(ggtext)
# Dataframe creation
Dati <- data.frame(
Time = c("0w", "0w", "0w", "6w", "6w", "6w", "8w", "8w", "8w", "10w", "10w", "10w"),
Peel_max = c(3.91, 4.4, 4.17, 3.24, 3.77, 2.91, 2.68, 2.79, 2.24, 1.84, 1.8, 1.54),
Foil_break = c("Foil break", "Foil break", "Foil break", "Foil break", "Foil break", "Foil break", "", "", "", "", "", ""),
FoilSide = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C")
)
# Definition of the factors
Dati$Time = factor(Dati$Time, levels = c("0w","6w","8w","10w"))
# Generic grapoh creation
Graph2 <- ggplot(Dati, aes(x= `Time`, col=`FoilSide`)) +
geom_point(aes(y= `Peel max`, color=`FoilSide`), shape = 1, size = 3.5) +
geom_line(aes(x=as.numeric(Dati$`Time`), y= `Peel max`, col=`FoilSide`)) +
theme_classic() +
scale_x_discrete(name="Time (Weeks)", expand=c(0.05, 0)) +
ggtitle("This is the title", subtitle = "This is the subtitle") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))
# Addition of the SubSubtitle, which is placing it too below
Graph2 = Graph2 +
annotate("text",
x = mean(range(as.numeric(Dati$`Time`))),
y = 6, # Imposta un valore negativo
label = "Max peel measured when foil breaks, otherwise reported the average peel",
size = 2.5, fontface = "italic", hjust = 0.5)
Graph2
If I increase the value of the y
position it stretch the graph maitaining the same distance, because the annotation is written on the graph, subtitle is on the label
part.
Is there any way to ad a subsubtitle just below the subtitle with smaller size and centered?
Since you’re already using ggtext, you can include the sub-subtitle in the subtitle, format with CSS, then use ggtext::element_markdown()
.
library(ggplot2)
library(ggtext)
Graph2 <- ggplot(Dati, aes(x = Time, y = Peel_max, color = FoilSide)) +
geom_point(shape = 1, size = 3.5) +
geom_line(aes(x = as.numeric(Time))) +
theme_classic() +
scale_x_discrete(name = "Time (Weeks)", expand = c(0.05, 0)) +
ggtitle(
"This is the title",
subtitle = paste(
"This is the subtitle<br>",
"<i style='font-size: 9pt;'>Max peel measured when foil breaks, otherwise reported the average peel</i>",
collapse = ""
)
) +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_markdown(hjust = 0.5)
)