在facet_wrap上方添加多行geom_text,其中一些行为斜体

cbjzeqam  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(113)

根据下面的图,我想修改geom_text调用中的标签,使每个标签的中间行都是斜体。这是expressionpaste的作业吗?

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

例如,Plot

l3zydbqr

l3zydbqr1#

使用ggtext::geom_richtext()

library(ggplot2)
library(ggtext)

theText <- data.frame(label = c("Facet 1: Line 1<br><i>Line 2 in italics</i><br>Line 3 not ital",
                                "Facet 2: Line 1<br><i>Ital here</i><br>But not here",
                                "Facet 3: Line 1<br><i>Also ital</i><br>No ital here"),
                      cyl = c(4,6,8))

p + geom_richtext(data = theText,
                  aes(x = -Inf, y = -Inf, label = label),
                  x  = 10, y=6,
                  hjust = 0, vjust = -1,
                  label.padding = unit(0, "lines"),
                  label.color = NA) +
  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())

或者,按文本划分面,然后设置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))

bkhjykvo

bkhjykvo2#

我能想到的唯一选择是为标题的每一行添加一个geom_text(),然后使用fontface = "italic"更改“中间”行的外观,即

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

创建于2022年12月19日,使用reprex v2.0.2

相关问题