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

后端 未结 3 1293
余生分开走
余生分开走 2020-11-28 09:50

I am doing a basic boxplot where y=age and x=Patient groups

age <- ggplot(data, aes(factor(group2), age))  + ylim(15, 80) 
age         


        
3条回答
  •  误落风尘
    2020-11-28 10:25

    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)
    

    enter image description here

    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.

提交回复
热议问题