2
votes

I want to add the number of observations below each boxplot (as in the figure- no need for the red square). :) However, I don't know how to annotate this type of boxplot (see figure below).multiple boxplot annotate number of observations

Does anyone know how to do it?

This is the code that I used to plot this figure.

ggplot(data=MIOT1, aes(stage, time, fill=resp)) +
geom_boxplot(color= "black", lwd=0.3) +
stat_summary(fun.y=mean, geom="point", shape=0, size=1, colour="black", position=position_dodge(width=0.75)) +
scale_fill_manual(values=c("grey25", "grey50", "grey67")) +
annotation_custom(mygrobA) +
scale_y_continuous(limits=c(-10,124)) +
theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    strip.background = element_rect(colour="black"),
    panel.border = element_rect(colour = "black", fill="transparent")) +
xlab(bquote(' ')) +
ylab(bquote('Minimum Consecutive Time (s)')) +
labs(title="SATIATION\n") +
theme(axis.title.y = element_text(colour="black",size=10,face="bold"),
    axis.text.x = element_text(colour="black",size=8, face="plain"),
    axis.text.y = element_text(colour="black",size=8, face="plain"),  
    axis.title.x = element_text(colour="black",size=10,face="bold")) +  
theme(panel.background = element_rect(fill = "white")) +
theme(plot.title = element_text(lineheight=.8, size=10, face="bold")) +
theme(legend.title=element_blank(), legend.key = element_rect(fill = NA, colour = NA)) +
theme(legend.position="none") +
theme(legend.background = element_rect(fill=NA)) +
theme(plot.margin = unit(c(.25,.25,.0,.0), "cm"))<i>

EXAMPLE DATA MIOT1 is a numeric variable (y-axis), and I am considering two grouping factors (development stage- x axis) and the response (unresponsive, coastal, lagoon).

Something like

stage  resp  time
pre       U      100
pre       U      80
pre       U       50
pre       C       20
flex       U      80
flex       U       90
flex       C       10
flex        C      20
post      U        40
post      U        30
post      U        60
post       C      80
post      C       100
post       L       50
post       L       40

Thank you! Pedro

2
Please provide us some sample data that we can work with. Right now we have no idea what MIOT1 is which makes it hard for us to replicate your problem. A dput(MIOT1) would be helpful. For more information check out: stackoverflow.com/questions/5963269/… - Mike H.
I now added an example data in the question. Thank you! - Pedro Miguel

2 Answers

4
votes

Here's a simple example of how to do it, using the built-in mtcars data frame:

ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() +
  geom_text(stat="count", aes(label=..count..), y=min(mtcars$mpg)- 0.6)

In your case, it will be something like

ggplot(data=MIOT1, aes(stage, time, fill=resp)) +
  geom_boxplot(color= "black", lwd=0.3) +
  geom_text(stat="count", aes(label=..count..), y=min(MIOT1$time)) 

where you may have to adjust the y location of the text labels and you might also need to adjust the range of the y-axis to make room for the labels.

UPDATE: I was able to reproduce the error you reported, but I'm not sure how to fix it. Instead, you can pre-summarize the data and then add it to the plot. Here's an example:

library(dplyr)

# Get counts by desired grouping variables
counts = mtcars %>% group_by(cyl, am) %>% tally


ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + 
  geom_boxplot(position=position_dodge(0.9)) +
  geom_text(data=counts, aes(label=n, y=min(mtcars$mpg) - 0.6),
        position=position_dodge(0.9))
1
votes

IN SUMMARY EIPI10 ANSWERED MY QUESTION:

library(dplyr)

# Get counts by desired grouping variables
    counts = mtcars %>% group_by(cyl, am) %>% tally


    ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + 
    geom_boxplot(position=position_dodge(0.9)) +
    geom_text(data=counts, aes(label=n, y=min(mtcars$mpg) - 0.6), position=position_dodge(0.9)