bam*_*phe 4 label r ggplot2 facet-grid ggtext
我有一个脚本,用于生成带有多行条带文本的多面图。但这不再起作用了。下面是一个 MWE,应该从中解析条带文本,例如"bold(A)\nreally~long~extra":
一个
很长的额外
正如您通过调试功能看到的那样,第二行被切断了。我什至增加了利润,但无济于事......
任何想法是什么问题?
exmpl = data.frame(a = 1:100,
b = rep(1:5, 20),
f = factor(rep(LETTERS[1:5], each = 20))) %>%
as_tibble() %>%
mutate(f2 = paste0("bold(",f, ")\nreally~long~extra"))
ggplot(exmpl, aes(x = b, y = a)) +
facet_grid(. ~ f2, labeller = label_parsed) +
geom_point() +
theme(strip.text.x = element_text(size = 10, hjust = 0, margin = margin(.5, 0, .5, 0, "cm"), debug = T))
Run Code Online (Sandbox Code Playgroud)
当我们这样做时,我只是想出了这个解决方法,因为我之前使用的解决方案label_bquote()不再有效。请看看另一个问题,也许你也可以帮我解决这个问题?
不确定这是否适合您。但是实现所需结果的一种方法是使用该ggtext包,它允许您使用 HTML 和 CSS 设置构面标签的样式。为此ggtext引入了新的主题元素element_markdown。尝试这个:
library(ggplot2)
library(dplyr)
exmpl = data.frame(a = 1:100,
b = rep(1:5, 20),
f = factor(rep(LETTERS[1:5], each = 20))) %>%
as_tibble() %>%
mutate(f2 = paste0("<b>", f, "</b><br>", "really long extra"))
ggplot(exmpl, aes(x = b, y = a)) +
facet_grid(. ~ f2) +
geom_point() +
theme(strip.text.x = ggtext::element_markdown(size = 10, hjust = 0))
Run Code Online (Sandbox Code Playgroud)

对于您之前帖子中的第二个问题,解决方案可能如下所示:
mylabel <- function(x) {
mutate(x, Species = paste0(letters[Species], " <i>", Species, "</i>"))
}
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
p + facet_grid(. ~ Species, labeller = mylabel) +
theme(strip.text.x = ggtext::element_markdown())
Run Code Online (Sandbox Code Playgroud)
