r - Sub-subtitle in a graph made with `ggplot2` - Stack Overflow

I am looking to create a graph with ggplot2, containing:A title;A subtitle;A sub-subtitleI tried usin

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
  • 1 Two suggestions: (1) use ggtext as in zephryl's answer; or (2) switch from "subsubtitle" to caption=: 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
  • Do not add titles to your plots. Create titles, captions, ... with the publication tool of your choice, e.g. LaTeX. – Friede Commented Mar 6 at 21:51
Add a comment  | 

3 Answers 3

Reset to default 6

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)
  )

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)
  )

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744965919a4603677.html

相关推荐

  • r - Sub-subtitle in a graph made with `ggplot2` - Stack Overflow

    I am looking to create a graph with ggplot2, containing:A title;A subtitle;A sub-subtitleI tried usin

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信