Boxplot problem created by error bar

Wants to create a boxplot with error bar, but there is issue to display the whisker properly, following is what I did:


# Prepare data
library(ggplot2)

data(airquality)
airquality$Month <- factor(airquality$Month,
                           labels = c("May", "Jun", "Jul", "Aug", "Sep"))

# Generate new category variable
airquality_trimmed <- airquality[which(airquality$Month == "Jul" |
                                         airquality$Month == "Aug" |
                                         airquality$Month == "Sep"), ]
airquality_trimmed$Temp.f <- factor(ifelse(airquality_trimmed$Temp > mean(airquality_trimmed$Temp), 1, 0),
                                    labels = c("Low temp", "High temp"))


# Create boxplot without whsker, it works well
ggplot(airquality_trimmed, aes(x = Month, y = Ozone, fill = Temp.f)) +
  geom_boxplot(alpha=0.7)

# However, the chart messed up if adding error bar, how to fit this problem? Thank you!
ggplot(airquality_trimmed, aes(x = Month, y = Ozone, fill = Temp.f)) +
  geom_boxplot(alpha=0.7) + stat_boxplot(geom="errorbar", width=0.2)
1 Like

I really don't understand what you are trying to achieve. Your boxplot already shows the whiskers, why do you want to add another errorbar?

Whisker is a horizontal bar, it doesn't show when run the first piece of code, it doesn't show properly in the second piece of code.

The problem is that by setting the width argument in stat_boxplot() it no longer matches the default width value in the geom_boxplot() layer. If you set the width to be the same in each layer, they'll be positioned correctly:

ggplot(airquality_trimmed, aes(x = Month, y = Ozone, fill = Temp.f)) +
    stat_boxplot(geom="errorbar", width=0.2) +
    geom_boxplot(position = position_dodge(width = 0.2))

You might still not be satisfied with the result, though. If you leave your alpha set at 0.7 (as in your original code), then you'll see the vertical line of the error bars running underneath the boxes, which is ugly. If you leave out the alpha argument as I've done, you'll see the end-caps on the error bars where they are not obscured by the boxes, but the whiskers drawn by geom_boxplot() will still appear. That's not really satisfactory either. I don't think there's a way to make geom_boxplot() draw the end-caps directly in the way that you want.

It doesn't work, thank you for the response!

Are you just trying to get end caps on the whiskers? If so, see the code below, which is a modification of kjhealy's answer. Note that:

  1. The stat_boxplot and geom_boxplot calls are dodged by the same amount, so that they will align properly.
  2. The width argument in geom_boxplot is set to the same value as the dodging width, so that the dodged boxplots will abut each other. Change to a smaller value if you'd like a little space between the boxplots within each pair.
  3. The width argument to stat_boxplot sets the width of the whisker end caps.
pd = position_dodge(width = 0.5)

ggplot(airquality_trimmed, aes(x = Month, y = Ozone, fill = Temp.f)) +
  stat_boxplot(geom="errorbar", position=pd, width=0.2) +
  geom_boxplot(width=0.5, position=pd)

Rplot

5 Likes

Yes, it is what I needed. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.