3

I already looked through other questions like these (Annotate ggplot2 facets with number of observations per facet), but didn't find the answer for carrying out an annotation of the single bars of a boxplot with facets.

Here's my sample code for creating the boxplot:

require(ggplot2)
require(plyr)
mms <- data.frame(deliciousness = rnorm(100),
                  type=sample(as.factor(c("peanut", "regular")), 
                              100, replace=TRUE),
                  color=sample(as.factor(c("red", "green", "yellow", "brown")), 
                               100, replace=TRUE))

ggplot(mms, aes(x=type, y=deliciousness, fill=type)) + 
   geom_boxplot(notch=TRUE)+
   facet_wrap(~ color,nrow=3, scales = "free")+
   xlab("")+
   scale_fill_manual(values = c("coral1", "lightcyan1", "olivedrab1"))+
   theme(legend.position="none")

And here the corresponding plot:

enter image description here

Now I want to annotate individually for each facet of the color the number of observations per group (peanut/regular), as shown in my drawing:

enter image description here

What I already did, was summarizing the number of observations with dpyr per color and per group (peanut/regular) with this code:

 mms.cor <- ddply(.data=mms, 
                  .(type,color), 
                  summarize, 
                  n=paste("n =", length(deliciousness)))

However, I do not know how to add this summary of the data to the ggplot. How can this be done?

Olympia
  • 457
  • 8
  • 19

1 Answers1

4

Try this approach using dplyr and ggplot2. You can build the label with mutate() and then format to have only one value based on max value of deliciousness. After that geom_text() can enable the text as you want. Here the code:

library(dplyr)
library(ggplot2)
#Data
mms <- data.frame(deliciousness = rnorm(100),
                  type=sample(as.factor(c("peanut", "regular")), 
                              100, replace=TRUE),
                  color=sample(as.factor(c("red", "green", "yellow", "brown")), 
                               100, replace=TRUE))
#Plot
mms %>% group_by(color,type) %>% mutate(N=n()) %>%
  mutate(N=ifelse(deliciousness==max(deliciousness,na.rm=T),paste0('n=',N),NA)) %>%
  ggplot(aes(x=type, y=deliciousness, fill=type,label=N)) + 
  geom_boxplot(notch=TRUE)+
  geom_text(fontface='bold')+
  facet_wrap(~ color,nrow=3, scales = "free")+
  xlab("")+
  scale_fill_manual(values = c("coral1", "lightcyan1", "olivedrab1"))+
  theme(legend.position="none")

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
  • The explanation seems good. However, when I run the code, I get the following error: Error: `n()` must only be used inside dplyr verbs. – Olympia Nov 18 '20 at 18:39
  • @Olympia Try loading `dplyr` and `ggplot2` in a new fresh session and test initially with the data you shared. It looks like you do not have the package loaded – Duck Nov 18 '20 at 18:42
  • @Olympia Fantastic! It is also a great question :) – Duck Nov 18 '20 at 18:52
  • 1
    thanks, if you liked the question, can you vote it up? :-) – Olympia Nov 19 '20 at 19:32