Home > Back-end >  Manually draw boxplot using ggplot
Manually draw boxplot using ggplot

Time:02-10

I think my question is very similar to enter image description here

With "group" as the color, instead of "fellowship". The problem is, that graph was drawn from "complete" data (with 800ish rows), and I clearly only have summary data above. I realize it won't be able to draw outliers but that is ok. Any help would be appreciated! I'm specifically struggling with how I would draw the ymin/max and the edges of the notch. Thank you

CodePudding user response:

You can use geom_boxplot() with stat = "identity" and fill in the five boxplot numbers as aesthetics.

library(ggplot2)

# show <- structure(...) # omitted for previty

ggplot(show, aes(Decade, fill = Group))  
  geom_boxplot(
    stat = "identity",
    aes(lower  = FirstQuartile,
        upper  = ThirdQuartile,
        middle = Median,
        ymin   = FirstQuartile - 1.5 * IQR, # optional
        ymax   = ThirdQuartile   1.5 * IQR) # optional
  )

As pointed out by jpsmith in the comments below, the 1.5 * IQR rule becomes hairy if you don't have the range of the data. However, if you have information about the data extrema or the data domain, you can limit the whiskers as follows:

# Dummy values assuming data is >= 0 up to infinity
show$min <- 0
show$max <- Inf

ggplot(show, aes(Decade, fill = Group))  
  geom_boxplot(
    stat = "identity",
    aes(lower  = FirstQuartile,
        upper  = ThirdQuartile,
        middle = Median,
        ymin   = pmax(FirstQuartile - 1.5 * IQR, min),
        ymax   = pmin(ThirdQuartile   1.5 * IQR, max))
  )
  • Related