1

I'd like to know how to annotate each facet in my bar plot. Right now, I'm using the geom_signif function which works perfectly except that it duplicates the annotation on one facet onto the other facet.

My code is as such:

geom_signif(annotation = c("p=0.01"),
            y_position = c(9), xmin = c(2), xmax = c(3))

My bar plot:

enter image description here

Please advise. I've read through some similar solutions here, tried some other ways but I still can't seem to figure it out.. This is the closest and easiest solution to what I got so far except that I want 2 different annotations (labeling of p-values in this case -I ran ANOVA separately) on the facets.

neilfws
  • 32,751
  • 5
  • 50
  • 63
MZZ
  • 35
  • 2
  • 4

1 Answers1

1

Here is an example of to do it by manually deconstructing the plot and reconstructing with new annotations. I understood it as you wanted manual text annotations per plot. This (very manual) solution is based on another answer, How do I annotate p-values onto a faceted bar plots on R?, which might be exactly what you are looking for.

df <- data.frame(iris,type = c(1,2))

## Construct your plot exactly as you have already done
## Annotations are replicated.
myplot <- ggplot(df, aes(x=Species,y = Sepal.Length)) + 
    geom_boxplot() + 
    facet_grid(.~type) + 
    geom_signif(annotation = c("foo"),xmin = 1, xmax = 2,y_position = 7.5)
myplot

Original output with repeated annotations

## Disassemble plot
myplot2 <- ggplot_build(myplot)
myplot2$data[[2]]
 x xend     y  yend annotation group PANEL shape colour textsize angle hjust vjust alpha family fontface lineheight
1 1    1 7.392 7.500        foo     1     1    19  black     3.88     0   0.5     0    NA               1        1.2
2 1    2 7.500 7.500        foo     1     1    19  black     3.88     0   0.5     0    NA               1        1.2
3 2    2 7.500 7.392        foo     1     1    19  black     3.88     0   0.5     0    NA               1        1.2
4 1    1 7.392 7.500        bar     1     2    19  black     3.88     0   0.5     0    NA               1        1.2
5 1    2 7.500 7.500        bar     1     2    19  black     3.88     0   0.5     0    NA               1        1.2
6 2    2 7.500 7.392        bar     1     2    19  black     3.88     0   0.5     0    NA               1        1.2
  linetype size
1        1  0.5
2        1  0.5
3        1  0.5
4        1  0.5
5        1  0.5
6        1  0.5
## Note there are 6 observations, 3 for each "PANEL". 
## Now, change the annotation on each "PANEL".
myplot2$data[[2]]$annotation <- c(rep("foo",3),rep("bar",3))

## Reconstruct plot
myplot3 <- ggplot_gtable(myplot2)
plot(myplot3)

Edited output

Therkel
  • 1,379
  • 1
  • 16
  • 31
  • Hi @Therkel. What if I don't have the entire data set for this? I'm ggplotting it based on mean scores for each group/bar plot. – MZZ Jul 17 '17 at 10:29
  • What do you mean you don't have the data? If you call the plot in your question for `myplot`, then only code from `myplot2 <- ggplot_build(myplot)` and below should be relevant for you. The code before that is just to make a verifiable example. – Therkel Jul 17 '17 at 10:34
  • I hope that makes sense! – Therkel Jul 17 '17 at 10:41
  • Hi @Therkel. Sorry, I just noticed the geom_signif annotation instead of geom_signif comparisons that didn't work w my incomplete data set. Geom_signif annotation works perfectly well!! Thank you so much!! – MZZ Jul 18 '17 at 02:07
  • Glad it helped. Now, since it wasn't obvious to you, perhaps my answer is not well enough written. For example, does it help you that there is an example of the output we are manipulating or is it superfluous? – Therkel Jul 18 '17 at 08:11
  • 1
    having an example of the output definitely helped. :) I was seeing a lot of other solutions like having a separate data frame or geom_signif(comparisons=list(c("A", "B" ... so I totally missed out on the possibility of geom_signif(annotation... when I was speed reading your code which was the easiest solution in my case. Thanks again! – MZZ Jul 18 '17 at 08:54
  • Just if somebody stumbles across this. For some reason I had to change `myplot2$data[[2]]` to `myplot2$data[[3]]`. The rest stayed exactly the same. Thanks again for the brilliant answer – nhaus Apr 05 '22 at 09:15