admin管理员组文章数量:1406020
I am looking to create a graph with ggplot2
, containing:
- A title;
- A subtitle;
- A sub-subtitle
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?
I am looking to create a graph with ggplot2
, containing:
- A title;
- A subtitle;
- A sub-subtitle
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?
Share Improve this question edited Mar 6 at 18:26 Jon Spring 67.4k4 gold badges39 silver badges66 bronze badges asked Mar 6 at 15:27 GiacomoDBGiacomoDB 4793 silver badges18 bronze badges 2 |3 Answers
Reset to default 6Since 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)
)
A quick method.
Added the sub-subtitle text directly to the subtitle with \n
for a new line, and ajust the theme to make the subtitle look right.
ggtitle("This is the title",
subtitle = "This is the subtitle\nMax peel measured when foil breaks, otherwise reported the average peel") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5, size = 8, face = "italic"))
Just out of curiosity and as a reference: A more recent option would be to use the marquee
package where I create all three titles using just one string and format the title levels as markdown headers:
library(ggplot2)
library(marquee)
s_set <- style_set(
base = base_style(align = "center", size = 11),
h1 = style(size = rem(1.2), margin = trbl(bottom = 5.5)),
h2 = style(margin = trbl(bottom = 5.5)),
h3 = style(italic = TRUE, size = rem(.8))
)
md_text <-
"
# This is the title
## This is the subtitle
### Max peel measured when foil breaks, otherwise reported the average peel
"
Dati$Time <- factor(Dati$Time, levels = c("0w", "6w", "8w", "10w"))
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(Time), y = Peel_max, col = FoilSide)) +
theme_classic() +
scale_x_discrete(name = "Time (Weeks)", expand = c(0.05, 0)) +
ggtitle(md_text) +
theme(
plot.title = element_marquee(hjust = 0.5, style = s_set)
)
本文标签: rSubsubtitle in a graph made with ggplot2Stack Overflow
版权声明:本文标题:r - Sub-subtitle in a graph made with `ggplot2` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965918a2634945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ggtext
as in zephryl's answer; or (2) switch from "subsubtitle" tocaption=
: it's on the bottom of the plot, certainly, but stylistically it is also a good option in many cases. – r2evans Commented Mar 6 at 16:04