Math - 9 months ago 49
R Question

# lower and upper quartiles in boxplot in R

I have

X=c(20 ,18, 34, 45, 30, 51, 63, 52, 29, 36, 27, 24)


With
boxplot
, i'm trying to plot the
quantile(X,0.25)
and
quantile(X,0.75)

but this is not realy the same lower and upper quartiles in boxplot in R

boxplot(X)
abline(h=quantile(X,0.25),col="red",lty=2)
abline(h=quantile(X,0.75),col="red",lty=2)


Do you know why?

The values of the box are called hinges and may coincide with the quartiles (as calculated by quantile(x, c(0.25, .075))), but are calculated differently.

From ?boxplot.stats:

The two ‘hinges’ are versions of the first and third quartile, i.e., close to quantile(x, c(1,3)/4). The hinges equal the quartiles for odd n (where n <- length(x)) and differ for even n. Whereas the quartiles only equal observations for n %% 4 == 1 (n = 1 mod 4), the hinges do so additionally for n %% 4 == 2 (n = 2 mod 4), and are in the middle of two observations otherwise.

To see that the values coincide with an odd number of observations, try the following code:

set.seed(1234)
x <- rnorm(9)

boxplot(x)
abline(h=quantile(x, c(0.25, 0.75)), col="red")