Boxplot show the value of mean

后端 未结 4 1817
名媛妹妹
名媛妹妹 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:22

    The Magrittr way

    I know there is an accepted answer already, but I wanted to show one cool way to do it in single command with the help of magrittr package.

    PlantGrowth %$% # open dataset and make colnames accessible with '$'
    split(weight,group) %T>% # split by group and side-pipe it into boxplot
    boxplot %>% # plot
    lapply(mean) %>% # data from split can still be used thanks to side-pipe '%T>%'
    unlist %T>% # convert to atomic and side-pipe it to points
    points(pch=18)  %>% # add points for means to the boxplot
    text(x=.+0.06,labels=.) # use the values to print text
    

    This code will produce a boxplot with means printed as points and values:

    I split the command on multiple lines so I can comment on what each part does, but it can also be entered as a oneliner. You can learn more about this in my gist.

提交回复
热议问题