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
And formatting the text using methods picked up here: 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
. 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". 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.
Thanks