Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get boxplot notches in ggplot2?

Tags:

r

ggplot2

boxplot

Yes, I know it's been around, I've also found Hadley's answer on google groups that there is no notches yet for ggplot2 boxplots. So my question is twofold: Has this changed (there's a native implementation of notches already) and if not is there something one could do about it.

I mean I do not need the notch optic, representing the confidence bounds by some shaded area that is suitably placed in another layer over the boxplot, would look nice, too.

Also added a screenshot because I heard a graphics question is never complete without the graphic enter image description here

like image 521
Matt Bannert Avatar asked Nov 15 '11 10:11

Matt Bannert


People also ask

What is notched boxplot in R?

notch : logical value. If TRUE, make a notched box plot. The notch displays a confidence interval around the median which is normally based on the median +/- 1.58*IQR/sqrt(n). Notches are used to compare groups; if the notches of two boxes do not overlap, this is a strong evidence that the medians differ.

What is a notched boxplot?

Notched box plots apply a "notch" or narrowing of the box around the median. Notches are useful in offering a rough guide of the significance of the difference of medians; if the notches of two boxes do not overlap, this will provide evidence of a statistically significant difference between the medians.

What do Ggplot Boxplots show?

The boxplot compactly displays the distribution of a continuous variable. It visualises five summary statistics (the median, two hinges and two whiskers), and all "outlying" points individually.

How do I make a boxplot in ggplot2?

In ggplot2, geom_boxplot() is used to create a boxplot. Let us first create a regular boxplot, for that we first have to import all the required libraries and dataset in use. Then simply put all the attributes to plot by in ggplot() function along with geom_boxplot.


1 Answers

Update In addition to the options detailed below, version 0.9.0 of ggplot2 includes this feature in geom_boxplot. Examining ?geom_boxplot reveals a notch and notchwidth argument:

+ geom_boxplot(notch = TRUE, notchwidth = 0.5)

Not elegant graphics but here is an example:

# confidence interval calculated by `boxplot.stats`
f <- function(x) {
    ans <- boxplot.stats(x)
    data.frame(ymin = ans$conf[1], ymax = ans$conf[2])
}

# overlay plot (upper panel below)
p <- ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() +
  stat_summary(fun.data = f, geom = "linerange", colour = "skyblue", size = 5)
p

# base graphics (lower panel below)
boxplot(Sepal.Length ~ Species, data = iris, notch = TRUE)

you can change the apparence of CI bar by tweaking the arguments of stat_summary.

enter image description hereenter image description here

crossbar version:

f <- function(x) {
  ans <- boxplot.stats(x)
  data.frame(ymin = ans$conf[1], ymax = ans$conf[2], y = ans$stats[3])
}

p <- ggplot(iris, aes(Species, Sepal.Length)) + 
  geom_boxplot(width = 0.8) +
  stat_summary(fun.data = f, geom = "crossbar", 
    colour = NA, fill = "skyblue", width = 0.8, alpha = 0.5)
p

enter image description here

like image 104
kohske Avatar answered Sep 29 '22 10:09

kohske