How to put Values on Boxplot in R for several boxplot in one image

后端 未结 2 807
南笙
南笙 2021-01-16 15:52

I want to plot Delta~Project.Types in R. I have 10 Project Types. I know how to do the boxplot : boxplot(Delta~Project.Types). However, how can I put the fivenum (min, max,

2条回答
  •  生来不讨喜
    2021-01-16 16:01

    The stats you want can also be obtained with fivenum

    five <- by(InsectSprays$count, InsectSprays$spray, fivenum)
    do.call(cbind, five)
    #         A    B   C    D   E  F
    # [1,]  7.0  7.0 0.0  2.0 1.0  9
    # [2,] 11.0 12.0 1.0  3.5 2.5 12
    # [3,] 14.0 16.5 1.5  5.0 3.0 15
    # [4,] 18.5 18.0 3.0  5.0 5.0 23
    # [5,] 23.0 21.0 7.0 12.0 6.0 26
    

    Alternatively, these stats are one of the return values of boxplot (note that you need to use range = 0 to get the min and max since there are some values which are outlying):

    bp <- boxplot(count ~ spray, data = InsectSprays, col = "lightgray", range = 0)
    bp$stats
    #      [,1] [,2] [,3] [,4] [,5] [,6]
    # [1,]  7.0  7.0  0.0  2.0  1.0    9
    # [2,] 11.0 12.0  1.0  3.5  2.5   12
    # [3,] 14.0 16.5  1.5  5.0  3.0   15
    # [4,] 18.5 18.0  3.0  5.0  5.0   23
    # [5,] 23.0 21.0  7.0 12.0  6.0   26
    

    Then just add to each box:

    text(x = col(bp$stats) - .5, y = bp$stats, labels = bp$stats)
    

    enter image description here

提交回复
热议问题