【发布时间】: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