Is it possible to create a boxplot graph with whisker? Thank you!

Here is a sample code, the generated boxplot without whisker.

library(ggplot2)

ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + geom_boxplot()

Is it possible to create a boxplot graph with whisker? Thank you!

Here is a sample code, the generated boxplot without whisker.

library(ggplot2)

ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + geom_boxplot()

When I run that code, the boxplots do have whiskers (as expected, since whiskers are created by default for `geom_boxplot()`

):

Just to be clear, by â€śwhiskersâ€ť I mean the lines extending from the top and bottom of the boxes. Is this what your plot looks like? If so, can you give an example of how you want it to look instead?

2 Likes

Thank you for your response!

The whisker is horizontal line at 1.5 IQR of the upper quartile/lower quartile, if using boxplot, whisker will display, but ggplot can contol much more other parameters, I like to use ggplot.

attach(mtcars)

boxplot(vs, disp)

Hi there,

If you add `stat_boxplot(geom = 'errorbar')`

you'll get what you want. Compare `a`

(your code) to `b`

(your code augmented with `stat_boxplot()`

) below:

```
library(tidyverse)
library(gridExtra)
#>
#> Attaching package: 'gridExtra'
#> The following object is masked from 'package:dplyr':
#>
#> combine
a <- ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + geom_boxplot()
b <- ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + geom_boxplot() + stat_boxplot(geom = 'errorbar')
grid.arrange(a, b, nrow = 2)
```

Created on 2018-10-04 by the reprex package (v0.2.0).

Hope this helps!

7 Likes

A minor iteration on @kmprioliâ€™s answer: if you create the errorbar layer first, then it will appear behind the boxplot layer:

```
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
stat_boxplot(geom = "errorbar", width = 0.2) +
geom_boxplot()
```

As a note, the â€śwhiskersâ€ť are traditionally the vertical lines. The crossbars may or may not be present (they werenâ€™t included Tukeyâ€™s original design, see Box-and-Whisker Plot -- from Wolfram MathWorld).

6 Likes

Thank you so much! it works.

1 Like

Thank you!!!!!!!!!!!!!!!!!!!!111

Great! it works.

1 Like

As a note for the future, there was a follow-up question (involving box plots with more than one group) here: