How to display upper and lower quartiles through different line in a boxplot in R?


To display the upper and lower quartiles through different line in base R boxplot, we can use abline function but we need to find the quartiles inside abline using quantile for the respective quartiles. The lines created by using abline and quantiles and the boxplot function may not coincide because of the differences in calculation. The calculation method for boxplot is explained below −

The two ‘hinges’ are versions of the first and third quartile. 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.

Example

> x<-rnorm(100)
> boxplot(x)

Output

Example

> abline(h=quantile(x,c(0.25,0.75)),col="blue")

Output

Updated on: 04-Mar-2021

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements