Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Manually draw boxplot using ggplot

I think my question is very similar to this one, the only difference being that I’d love to use ggplot (and the answer with ggplot was missing a tiny bit of detail). I have data like this:

show<-structure(list(Median = c(20, 39, 21, 52, 45.5, 24, 36, 20, 134, 
27, 44, 43), IQR = c(4, 74, 28, 51.5, 73.5, 18, 47.5, 26.5, 189.5, 
46, 54, 61), FirstQuartile = c(`25%` = 19, `25%` = 24, `25%` = 12, 
`25%` = 30.5, `25%` = 36.5, `25%` = 18, `25%` = 16.5, `25%` = 13, 
`25%` = 53.5, `25%` = 15, `25%` = 24.5, `25%` = 27), ThirdQuartile = c(`75%` = 23, 
`75%` = 98, `75%` = 40, `75%` = 82, `75%` = 110, `75%` = 36, 
`75%` = 64, `75%` = 39.5, `75%` = 243, `75%` = 61, `75%` = 78.5, 
`75%` = 88), Group = c("Program Director", "Editor", "Everyone", 
"Board Director", "Board Director", "Program Director", "Editor", 
"Everyone", "Board Director", "Everyone", "Editor", "Program Director"
), Decade = c("1980's", "1980's", "1980's", "1980's", "1990's", 
"1990's", "1990's", "1990's", "2000's", "2000's", "2000's", "2000's"
)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"
))

And I would like to draw a graph like this:
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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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,
        ymax   = ThirdQuartile + 1.5 * IQR)
  )

As pointed out by jpsmith in the comments below, 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))
  )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading