How to put values on a boxplot for median, 1st quartile and last quartile?

后端 未结 4 1178
猫巷女王i
猫巷女王i 2020-12-10 16:29

How to put values on boxplot and control its width?

X<-c(1,2,,3,4,4,5,5,6,6,6,6,6,7)

I need to write values for min, max, 1st quartile,

4条回答
  •  星月不相逢
    2020-12-10 17:28

    The answer provided by mnel is perfect, assuming —as in the example— that there are not outliers. In that case, the lower and upper whiskers are then equal to the minimum and maximum. But, if there are outliers and we don't specify range = 0 in the boxplot function, we don't get the right values for the labels. Instead, we could use:

    text(x=boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, y = 1.25)
    

    Let's see an example:

    Horizontally

    X <- c(1,2,3,3,4,4,5,5,6,6,6,6,10,15)
    boxplot(X, horizontal = TRUE, axes = FALSE, staplewex = 1)
    
    1. Incorrect valuestext(x = fivenum(X), labels = fivenum(X), y = 1.25)

    enter image description here

    1. Correct values: text(x = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, y = 1.25)

    enter image description here

    Vertically

    Switching the arguments for x and y inside text.

    boxplot(X, axes = FALSE, staplewex = 1)
    text(y = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, x = 1.25)
    

提交回复
热议问题