2

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

1 Answer 1

6

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))
  )
Sign up to request clarification or add additional context in comments.

6 Comments

I think to exactly mimic that plot, he can also add + labs(y="Publications") after the boxplot function and change the parameter fill for color. That would be better but your answer is on point ! Edit: also a + geom_point() would help to visualize points out of the boxes
You're right, that would make the plot more like the example plot. I've omitted such details to have the answer address the problem at hand (how to draw boxplots with summary data alone), not replicating flavour aspects of the example plot.
I would caution the use of ymin and ymax being + or - 1.5 * IQR since that results in negative publications, which is not logical given the data, and just present the boxes (i.e., ymin and ymax set to their respective quartiles.
Yes I agree, but the extrema for the data aren't included in the summary statistics.
Yes, exactly, that is why I am suggesting not to use them
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.