Boxplot show the value of mean

后端 未结 4 1810
名媛妹妹
名媛妹妹 2020-11-30 18:42

In this boxplot we can see the mean but how can we have also the number value on the plot for every mean of every box plot?

 ggplot(data=PlantGrowth, aes(x=g         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 19:29

    You can also use a function within stat_summary to calculate the mean and the hjust argument to place the text, you need a additional function but no additional data frame:

    fun_mean <- function(x){
      return(data.frame(y=mean(x),label=mean(x,na.rm=T)))}
    
    
    ggplot(PlantGrowth,aes(x=group,y=weight)) +
    geom_boxplot(aes(fill=group)) +
    stat_summary(fun.y = mean, geom="point",colour="darkred", size=3) +
    stat_summary(fun.data = fun_mean, geom="text", vjust=-0.7)
    

    enter image description here

提交回复
热议问题