简体   繁体   English

R:如何让 ggplot2::geom_text() 与其他格式的文本一起插入换行符?

[英]R: How do I get ggplot2::geom_text() to insert a line break along with other formatted text?

I am trying to put annotation in a faceted boxplot using the method described in the top answer of this question: Annotating text on individual facet in ggplot2我正在尝试使用此问题的最佳答案中描述的方法将注释放在多面箱线图中: Annotating text on individual facet in ggplot2

And formatting the text using methods picked up here: https://ggplot2.tidyverse.org/reference/annotate.html并使用此处选择的方法格式化文本: https://ggplot2.tidyverse.org/reference/annotate.html

I can't seem to figure out why geom_text() will not insert the line break in my code where I have place a \n .我似乎无法弄清楚为什么geom_text()不会在我放置\n的代码中插入换行符。 Here is a simplified version:这是一个简化版本:

p.data <- data.frame(Main = rep("Ratio", 100),
                     CAT = c('A','B','C','D'),
                     value = rnorm(100, mean = 1.5, sd = 1.5))
p.text <- data.frame(Main = "Ratio",
                     CAT = 'B',
                     value = 7,
                     lab = "Text")
p <-  ggplot(data = p.data, aes(x = CAT, y = value, fill = CAT)) +
      geom_boxplot() +
      scale_y_continuous(breaks = c(0:6), limits = c(0,8)) +
      facet_wrap(~ Main, scales = 'fixed', nrow = 1, ncol = 1) +
      geom_text(data = p.text, hjust = 0, parse = TRUE,
                label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \n bold(MDD): n.s.)")
p

在此处输入图像描述

Among some other nonsense, I have tried:在其他一些废话中,我尝试过:

label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \n bold(MDD): n.s.)"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \n, bold(MDD): n.s.)"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \"\n\", bold(MDD): n.s.)"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \"\n bold(MDD): n.s.\")"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\"\n, bold(MDD): n.s.)"

...but nothing has worked. ...但没有任何效果。

In case it is not obvious, what I want is the CMV results on one line and MDD results on another line, while keeping the bold fonts and superscripted "2".如果不明显,我想要的是一行上的 CMV 结果和另一行上的 MDD 结果,同时保持粗体 fonts 和上标“2”。 My final graph will consist of one graph with one facet and one with three facets stuck together using grid.arrange() , but my example is just the one graph.我的最终图表将包括一个带有一个面的图表和一个使用grid.arrange()粘在一起的三个面的图表,但我的示例只是一个图表。

Thanks谢谢

There are a couple of work around solutions:有几个解决方案:

@eipi10 suggests in the comment above using atop() : @eipi10 在上面的评论中建议使用atop()

p.data <- data.frame(Main = rep("Ratio", 100),
                 CAT = c('A','B','C','D'),
                 value = rnorm(100, mean = 1.5, sd = 1.5))
p.text <- data.frame(Main = "Ratio",
                 CAT = 'B',
                 value = 7,
                 lab = "Text")
p <- ggplot(data = p.data, aes(x = CAT, y = value, fill = CAT)) +
     geom_boxplot() +
     scale_y_continuous(breaks = c(0:6), limits = c(0,8)) +
     facet_wrap(~ Main, scales = 'fixed', nrow = 1, ncol = 1) +
     geom_text(data = p.text, hjust = 0, parse = TRUE,
               label = "atop(paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\"), 
                             bold(MDD): n.s.)")
p

在此处输入图像描述

However, I am being picky about formatting and would like both lines to be left aligned.但是,我对格式很挑剔,希望两行都左对齐。 So, I will just create another line in the p.text data frame with a lower value to place another geom_text :因此,我将在p.text数据框中创建另一行,其值较低,以放置另一个geom_text

p.text <- data.frame(Main = "Ratio",
                     CAT = 'B',
                     value = c(7, 6),
                     lab = "Text")
p <- ggplot(data = p.data, aes(x = CAT, y = value, fill = CAT)) +
  geom_boxplot() +
  scale_y_continuous(breaks = c(0:6), limits = c(0,8)) +
  facet_wrap(~ Main, scales = 'fixed', nrow = 1, ncol = 1) +
  geom_text(data = p.text[1,], hjust = 0, parse = TRUE,
            label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\")") +
  geom_text(data = p.text[2,], hjust = 0, parse = TRUE,
            label = "paste(bold(MDD): n.s.)")
p

在此处输入图像描述

I'll have to adjust the value to get the line spacing just right, but this will work.我将不得不调整该值以使行距恰到好处,但这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM