16

In the past, I have been able to create boxplots using ggplot2 by providing the lower whisker, lower quantile, median, upper quantile, and upper whisker along with x-axis labels. For example:

DF <- data.frame(x=c("A","B"), min=c(1,2), low=c(2,3), mid=c(3,4), top=c(4,5), max=c(5,6))
ggplot(DF, aes(x=x, y=c(min,low,mid,top,max))) +
geom_boxplot()

would make a boxplot for two sets of data (A & B). This no longer works for me. I get the following error:

Error: Aesthetics must either be length one, or the same length as the dataProblems:x

Does anyone know if something has been changed in ggplot2?

zx8754
  • 52,746
  • 12
  • 114
  • 209
user1399932
  • 195
  • 1
  • 5
  • 1
    Since it could be a change between versions of ggplot2, what version are you using (`sessionInfo()`). I'm using ggplot2_0.9.0 in R 2.15.0 and it works for me, so I guess it must be something to do with the R or ggplot2 version. – mathematical.coffee May 17 '12 at 02:49

1 Answers1

21

This works using ggplot2 version 0.9.1 (and R 2.15.0)

library(ggplot2)

DF <- data.frame(x=c("A","B"), min=c(1,2), low=c(2,3), mid=c(3,4), top=c(4,5), max=c(5,6))

ggplot(DF, aes(x=x, ymin = min, lower = low, middle = mid, upper = top, ymax = max)) +
  geom_boxplot(stat = "identity")

enter image description here

See the "Using precomputed statistics" example here

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • Is there an easy way to include the outliers (assuming they are known), w/o having to use `geom_point` separately ? – steveb Jan 13 '22 at 23:50