2

I would like to do a boxplot with two different groups with only three different measurements (is that even possible?). Here is my data:

data <- data.frame( "County" = 1:6, Median = c(5,7,8,2,4, 5), Low = c( 0.5,2,4,1,2,3),
                    High = c(10,12,11,9,10,15), ID = c("TRUE", "TRUE", "FALSE", "TRUE", "FALSE", "FALSE"))  

I would like to create a boxplot with County on the x-axis, median, low and high on the y-axis, and ID (either true/false) as fill. As a result, I want six different (in this case) boxplots (three false and three true). But I am not sure how to do this with my data, since I don't have ymin and ymax.

I have tried this, but it does not take lower and upper into account:

ggplot(dat, aes(x = County, y = Median, lower = Low, upper = High, fill = ID)) +
  geom_boxplot()

Have anyone experienced the same problem?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
paula456
  • 101
  • 9

1 Answers1

3

A boxplot is parameterised by:

  • ymin: the lower whisker
  • lower: the 25th percentile
  • middle: the 50th percentile
  • upper: the 75th percentile
  • ymax: the upper whisker

As you correctly pointed out, it seems that we cannot fit these 5 parameters with only three observations per group. However, you might be interested in the geom_crossbar() layer, which will give you a boxplot-like appearance without the whiskers, and it requires only 3 parameters. Example below:

library(ggplot2)

data <- data.frame( "County" = 1:6, Median = c(5,7,8,2,4, 5), Low = c( 0.5,2,4,1,2,3),
                    High = c(10,12,11,9,10,15), ID = c("TRUE", "TRUE", "FALSE", "TRUE", "FALSE", "FALSE")) 

ggplot(data, aes(x = as.factor(County), 
                 y = Median, 
                 ymin = Low, 
                 ymax = High)) +
  geom_crossbar(aes(colour = ID))

teunbrand
  • 33,645
  • 4
  • 37
  • 63