Skip to content
wch edited this page Dec 11, 2011 · 3 revisions

I've added an option for notches to geom_boxplot. There are two new parameters for geom_boxplot:

  • notch: TRUE / FALSE
  • notchwidth: the relative width of the notch to the overall width (default .5)

Some examples:

set.seed(110)
dat <- data.frame(x=LETTERS[1:3], y=round(rnorm(90),2))

# Notched box plot
ggplot(dat, aes(x=x, y=y)) + geom_boxplot(notch=TRUE)

# Can set notch width
ggplot(dat, aes(x=x, y=y)) + geom_boxplot(notch=TRUE, notchwidth=.2)

# Manually-specified positions
ggplot(NULL, aes(x = 1, y=NULL, ymin = 0, lower = 25, middle = 50,
                 upper = 75, ymax = 100, notchupper=60, notchlower=40)) +
  geom_boxplot(stat="identity", notch=TRUE) 

# Gives a warning if notches go past hinges
ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(notch=TRUE) + ylim(10,35)
# Warning messages:1: In get(x, envir = this, inherits = inh)(this, ...) :
#   notch went outside hinges. Try setting notch=FALSE.
# 2: In get(x, envir = this, inherits = inh)(this, ...) :
#   notch went outside hinges. Try setting notch=FALSE.

# Regular boxplot function gives similar warning
boxplot(mpg ~ cyl, mtcars, notch=TRUE)
# Warning message:
# In bxp(list(stats = c(21.4, 22.8, 26, 30.4, 33.9, 17.8, 18.65, 19.7,  :
#   some notches went outside hinges ('box'): maybe set notch=FALSE

(You may have noticed that the outliers are different between geom_boxplot and the base boxplot. This is a known issue with geom_boxplot.)

The notch option also supports weighted boxplots. Normally the notch size is:

median +- 1.58*IQR*sqrt(n)

Where n is the number of observations. Weighted boxplots require a change to the calculation of n, so it uses the sum of all weights at observations where weight and y values are not NA. For example, three unweighted observations at a given y value are the same as one observation with weight=3, in terms of calculating notch size.

Adding the notch required some changes to geom_crossbar; instead of drawing the body with a GeomRect, it now uses GeomPolygon.

Clone this wiki locally