我有一个脚本,用于生成带有多行 strip 文本的多面图。但这不再起作用了。下面是一个 MWE,应该从中解析 strip 文本,例如"bold(A)\nreally~long~extra"
到:
A
really 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))
编辑:
当我们在做的时候,我只是想出了这个解决方法,因为我以前使用
label_bquote()
的解决方案不工作了。请看 this other question ,也许你也可以帮我解决这个问题?
最佳答案
不确定这是否适合您。但实现预期结果的一种方法是使用 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))
对于您之前帖子中的第二个问题,解决方案可能如下所示:
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())
关于r - ggplot2 facet 标签 - 第二行不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63710781/