How to add a number of observations per group and use group mean in ggplot2 boxplot?

情到浓时终转凉″ 提交于 2019-11-27 01:21:58
Ben

Is this anything like what you're after? With stat_summary, as requested:

# function for number of observations 
give.n <- function(x){
  return(c(y = median(x)*1.05, label = length(x))) 
  # experiment with the multiplier to find the perfect position
}

# function for mean labels
mean.n <- function(x){
  return(c(y = median(x)*0.97, label = round(mean(x),2))) 
  # experiment with the multiplier to find the perfect position
}

# plot
ggplot(mtcars, aes(factor(cyl), mpg, label=rownames(mtcars))) +
  geom_boxplot(fill = "grey80", colour = "#3366FF") +
  stat_summary(fun.data = give.n, geom = "text", fun.y = median) +
  stat_summary(fun.data = mean.n, geom = "text", fun.y = mean, colour = "red")

Black number is number of observations, red number is mean value. joran's answer shows you how to put the numbers at the top of the boxes

hat-tip: https://stackoverflow.com/a/3483657/1036500

I think this is what you're looking for maybe?

myboxplot <- ddply(mtcars,
                    .(cyl),
                    summarise,
                    min = min(mpg),
                    q1 = quantile(mpg,0.25),
                    med = median(mpg),
                    q3 = quantile(mpg,0.75),
                    max= max(mpg),
                    lab = length(cyl))
ggplot(myboxplot, aes(x = factor(cyl))) + 
    geom_boxplot(aes(lower = q1, upper = q3, middle = med, ymin = min, ymax = max), stat = "identity") + 
    geom_text(aes(y = max,label = lab),vjust = 0)

I just realized I mistakenly used the median when you were asking about the mean, but you can obviously use whatever function for the middle aesthetic you please.

Answer to the first problem. To show value above the box you should provide x values as numeric not as level names. So, to plot the value above first value give x=1.

data(ToothGrowth)
ggplot(ToothGrowth,aes(supp,len))+geom_boxplot()+
   annotate("text",x=1,y=32,label=30)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!