r - Repositioning stat_summary count numbers in ggplot boxplot -
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 above plot, position counts above median line in boxplot more visible. using position_jitter in way not prevent count number overlapping median bar. suggestions? *edited provide df2
you need define adjustment in returned y = value. example, have return median + 0.1. have adjust 0.1 manually data.
give.n <- function(x){ return(c(y = median(x) + 0.1, label = length(x))) } ggplot(iris, aes(species, sepal.width)) + geom_boxplot() + stat_summary(fun.data = give.n, geom = "text") or if want centered between median , upper hinge, compute position this:
give.n <- function(x){ return(c(y = mean(fivenum(x)[3:4]), label = length(x))) } 
Comments
Post a Comment