r - Add a number of observations per group AND SUBGROUP in ggplot2 boxplot -


this might seem duplicate of this question, in fact want expand original question.

i want annote boxplot number of observations per group , subgroup in ggplot. following example or original post, here minimal example:

require(ggplot2)  give.n <- function(x){   return(c(y = median(x)*1.05, label = length(x)))    # experiment multiplier find perfect position }  ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(gear))) +   geom_boxplot() +   stat_summary(fun.data = give.n, geom = "text", fun.y = median) 

my problem number of samples line in center of group, rather plotting on appropriate boxplot (as picture below shows):annotes centering in middle of group rather plotting on appropriate boxplot

is want?

require(ggplot2)  give.n <- function(x){   return(c(y = median(x)*1.05, label = length(x)))    # experiment multiplier find perfect position }  ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(gear))) +   geom_boxplot() +   stat_summary(fun.data = give.n, geom = "text", fun.y = median, position=position_dodge(width=0.75)) 

enter image description here


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -