1

I have the following data set:

depth <- data.frame(Sample = c("AD_001", "AD_009", "AD_017", "AD_025", 
                           "AD_033", "AD_041", "AD_049", "AD_057", 
                           "AD_065", "AD_073", "AD_081", "AD_089"), 
                median = c(12, 13, 11, 12, 12, 12, 13, 13, 14, 15, 15, 13), 
                granular_first_quartile = c(5, 6, 5, 6, 5, 6, 6, 6, 7, 7, 7, 6), 
                granular_third_quartile = c(23, 25, 21, 22, 23, 23, 24, 25, 27, 28, 28, 24))

and would like to create a boxplot but, the graphs I'm generating do not have an equally separated x field.

ggplot(depth, aes(as.factor(Sample))) + geom_boxplot(aes(middle = median, lower = granular_first_quartile, upper = granular_third_quartile, ymin = granular_first_quartile, ymax = granular_third_quartile), stat = 'identity') + coord_flip()

Thanks for the help!

2
  • 1
    the last example in ?geom_boxplot shows you how to make a boxplot from summary statistics. as does stackoverflow.com/questions/22212885/… Commented May 4, 2016 at 20:38
  • 1
    Your data have only one value of median per factor level in Sample. If you create a box plot of a single value, it will only return a line at that value, which I assume is what your plot looks like - a line at the value of median for each level of Sample. The plot wont have any whiskers. Commented May 4, 2016 at 21:00

1 Answer 1

2

You already have all (median, Q1, and Q3), and just need to assign lower, upper, middle, ymin, and ymax

(FYI, https://en.wikipedia.org/wiki/Box_plot#/media/File:Boxplot_vs_PDF.svg)

ggplot(depth, aes(Sample, median)) +
geom_boxplot(aes(lower = granular_first_quartile, upper = granular_third_quartile, 
middle = median, 
ymin = granular_first_quartile - 1.5*(granular_third_quartile-granular_first_quartile), 
ymax = granular_third_quartile+1.5*(granular_third_quartile-granular_first_quartile)),
stat="identity")+ coord_flip()
Sign up to request clarification or add additional context in comments.

2 Comments

great answer. I don't need the whiskers so I set the ymin and ymax to granular_first_quartile and granular_third_quartile, respectively. The only problem I have is that the x-axis is not being evenly spaced.
but, if I use your answer, the x-axis IS evenly spaced.

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.