1

I am wondering what is the idiomatic approach for constructing a ggplot2 boxplot given precomputated values for each boxplot

> df
    base p10 p90 lower_quartile     mean median upper_quartile
1      1  32  35             33 33.63740     34             34
2      2  32  35             33 33.77753     34             35
3      3  32  36             33 33.89361     34             35
4      4  33  36             33 33.89691     34             35
5      5  32  35             33 33.85145     34             35
6      6  35  37             37 36.48259     37             37

Attempting to draw these plots with

ggplot(df, aes(base)) +
  geom_boxplot(aes(ymin = p10,
                   lower = lower_quartile,
                   middle = median,
                   upper = upper_quartile,
                   ymax = p90),
               stat = "identity")

does not give the desired plots. What am I missing?

0

1 Answer 1

2

I don't know what base represents in your data.frame but in order to do it correctly your x-axis is supposed to be discrete (to show the different boxplots). Then for the y axis you need a ymin, a lower, a middle, an upper and a ymax which you have provided. The x-axis is the variable that is used to plot the different boxplots. So, if you turn it into a factor then it works:

library(ggplot2)
#I have added base as factor
ggplot(df, aes(factor(base))) +
  geom_boxplot(aes(ymin = p10,
                   lower = lower_quartile,
                   middle = median,
                   upper = upper_quartile,
                   ymax = p90),
               stat = "identity")

Output:

enter image description here

And this way it works.

Sign up to request clarification or add additional context in comments.

Comments

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.