r - ggplot2 facet 标签 - 第二行不显示

标签 r ggplot2 label facet-grid ggtext

我有一个脚本,用于生成带有多行 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))
enter image description here
编辑:
当我们在做的时候,我只是想出了这个解决方法,因为我以前使用 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/

相关文章:

r - 如何将字符串连接到可能的空字符向量的每个元素?

r - 在 semplot > sempaths 中标记节点时遇到问题

r - ggplot2 geom_bar 填充美学没有改变

excel - 如何将 Excel 标签标题中的文本垂直居中?

CSS 标签丢失边距/填充

r - 提取特殊字符 "/"之间的倒数第二个单词

r - 使用 ggplot2 和 geom_area 堆叠负/正时间序列

r - 更改ggplot2中的字体

r - 手动设置 scale_fill_distiller() 的比例

colors - 如何在gnuplot中设置标签和线条相同的颜色