0

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

enter image description here

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

Bart
  • 473
  • 6
  • 15
  • 4
    As far as I know, it's not possible to put a line break in a parsed expression (which is what you're generating here with `parse=TRUE` to get superscripts and other formatting), even though `"\n"` will create a line break with plain text (i.e., not parsed). You can try using the `atop()` function to get multiple lines with expressions (there are several SO questions related to that). – eipi10 Oct 14 '19 at 22:58
  • 4
    [This question](https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression) describes use of `atop()` and some other potential options. – eipi10 Oct 14 '19 at 23:00

1 Answers1

1

There are a couple of work around solutions:

@eipi10 suggests in the comment above using 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

enter image description here

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 <- 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

enter image description here

I'll have to adjust the value to get the line spacing just right, but this will work.

Bart
  • 473
  • 6
  • 15