根据下面的图,我想修改geom_text
调用中的标签,使每个标签的中间行都是斜体。这是expression
和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())
例如,Plot
2条答案
按热度按时间l3zydbqr1#
使用
ggtext::geom_richtext()
:或者,按文本划分面,然后设置
theme(strip.text = ggtext::element_markdown())
:bkhjykvo2#
我能想到的唯一选择是为标题的每一行添加一个
geom_text()
,然后使用fontface = "italic"
更改“中间”行的外观,即创建于2022年12月19日,使用reprex v2.0.2