Repositioning stat_summary count numbers in ggplot boxplot

Viewed 2383
df2<-data.frame(id=c("a","f","f","b","b","c","c","c","d","d","",""),
                var=c(12,20,15,18,10,30,5,8,5,5,3,5))

give.n <- function(x){
  return(c(y = mean(x), label = length(x)))
}
ggplot(data=subset(df2, id != ""), aes(x = reorder(id, -var), y = var)) +
  geom_boxplot()+
  stat_summary(fun.data = give.n, geom = "text", 
               position = position_jitter(height=1, width = 0))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size=11, vjust = -.005))+
  ggtitle("Title")+
  xlab("")+
  ylab("value")

I have the above plot, however I would like to position the counts above the median line in the boxplot so they are more visible. Using position_jitter in this way does not always prevent the count number from overlapping with the median bar. Any suggestions? *edited to provide df2

1 Answers
Related