【问题标题】:R: How do I get ggplot2::geom_text() to insert a line break along with other formatted text?R:如何让 ggplot2::geom_text() 与其他格式化文本一起插入换行符?
【发布时间】:2020-02-11 13:05:47
【问题描述】:

我正在尝试使用此问题的最佳答案中描述的方法将注释放入多面箱线图中:Annotating text on individual facet in ggplot2

并使用此处选择的方法格式化文本:https://ggplot2.tidyverse.org/reference/annotate.html

我似乎无法弄清楚为什么geom_text() 不会在我放置\n 的代码中插入换行符。这是一个简化的版本:

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

在其他一些废话中,我已经尝试过:

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.)"

...但没有任何效果。

如果不明显,我想要的是 CMV 结果在一行,MDD 结果在另一行,同时保持粗体字体和上标“2”。我的最终图表将包括一张带有一个面的图表和一张使用grid.arrange() 将三个面粘在一起的图表,但我的示例只是一张图表。

谢谢

【问题讨论】:

  • 据我所知,即使@ 987654332@ 将使用纯文本(即未解析)创建换行符。您可以尝试使用 atop() 函数来获取多行表达式(有几个与此相关的 SO 问题)。
  • This question 描述了atop() 和其他一些潜在选项的使用。

标签: r ggplot2 boxplot quotes geom-text


【解决方案1】:

有几个解决方案:

@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

但是,我对格式很挑剔,希望两行都左对齐。因此,我将在 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

我必须调整该值以使行距恰到好处,但这会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多