Home > Software design >  Adding a multi line geom_text above a facet_wrap with some lines in italics
Adding a multi line geom_text above a facet_wrap with some lines in italics

Time:12-19

Given the plot below, I'd like to amend the labels in the call to geom_text so that the middle line of each label is in italics. Is this a job for an expression and paste?

library(ggplot2)

# given this plot
p <- ggplot(mtcars, aes(mpg, wt))  
  geom_point()  
  facet_grid(. ~ cyl)
p
# Add custom above and outside the plot for each facet. 
# Each bit of text will have three lines. One of the lines will
# be italicized, the other two will not.

theText <- data.frame(label = c("Facet 1: Line 1\nLine 2 in italics\nLine 3 not ital",
                                "Facet 2: Line 1\nItal here\nBut not here",
                                "Facet 3: Line 1\nAlso ital\nNo ital here"),
                      cyl = c(4,6,8))

p   geom_text(data    = theText,
              mapping = aes(x = -Inf, y = -Inf, label = label),
              x  = 10, y=6,
              hjust = 0, vjust = -1)  
  coord_cartesian(ylim = c(0, 6), clip = 'off')     
  theme(plot.margin = unit(c(5,1,1,1), "lines"), 
        strip.background = element_blank(),
        strip.text = element_blank())

E.g.,

Or alternatively, facet by your text and then set theme(strip.text = ggtext::element_markdown()):

library(ggplot2)
library(dplyr)
library(ggtext)

mtcars2 <- mtcars %>%
  mutate(label = recode(
    cyl,
    `4` = "Facet 1: Line 1<br><i>Line 2 in italics</i><br>Line 3 not ital",
    `6` = "Facet 2: Line 1<br><i>Ital here</i><br>But not here",
    `8` = "Facet 3: Line 1<br><i>Also ital</i><br>No ital here"
  ))

ggplot(mtcars2, aes(mpg, wt))  
  geom_point()   
  facet_grid(. ~ label)  
  theme(plot.margin = unit(c(5,1,1,1), "lines"), 
        strip.background = element_blank(),
        strip.text = element_markdown(hjust = 0))

CodePudding user response:

The only option I can think of is to add a geom_text() for each line of your header, then change the face for the 'middle' line using fontface = "italic", i.e.

library(tidyverse)

# given this plot
p <- ggplot(mtcars, aes(mpg, wt))  
  geom_point()  
  facet_grid(. ~ cyl)
#p
# Add custom above and outside the plot for each facet. 
# Each bit of text will have three lines. One of the lines will
# be italicized, the other two will not.

theText_1 <- data.frame(label = c("Facet 1: Line 1",
                                "Facet 2: Line 1",
                                "Facet 3: Line 1"),
                      cyl = c(4,6,8))
theText_2 <- data.frame(label = c("Line 2 in italics",
                                 "Ital here",
                                 "Also ital"),
                       cyl = c(4,6,8))
theText_3 <- data.frame(label = c("Line 3 not ital",
                                 "But not here",
                                 "No ital here"),
                       cyl = c(4,6,8))

p   geom_text(data    = theText_1,
              mapping = aes(x = -Inf, y = -Inf, label = label),
              x  = 10, y=7.5,
              hjust = 0, vjust = -1)  
  geom_text(data    = theText_2,
              mapping = aes(x = -Inf, y = -Inf, label = label),
              x  = 10, y=7,
              hjust = 0, vjust = -1,
              fontface = "italic")  
  geom_text(data    = theText_3,
            mapping = aes(x = -Inf, y = -Inf, label = label),
            x  = 10, y=6.5,
            hjust = 0, vjust = -1)  
  coord_cartesian(ylim = c(0, 6), clip = 'off')     
  theme(plot.margin = unit(c(5,1,1,1), "lines"), 
        strip.background = element_blank(),
        strip.text = element_blank())

Created on 2022-12-19 with reprex v2.0.2

  • Related