1

I would like to create a boxplot with ggplot in R, but I have some problems. I know how to create in general a boxplot. For example like this:

p <- ggplot(data, aes(y=number, x=pod)) + geom_boxplot()
print(p)

However, in my case I have only the mean (0,892), median (0,863 +-0,0858) and the range (0,621-1,132). I am not sure how to create a boxplot if I do not have all the values. Is it possible to create a boxplot with just these three parameters? Can anyone help me? Thank you very much. Best regards, Hannah

stefan
  • 90,330
  • 6
  • 25
  • 51
Dolphin94
  • 319
  • 1
  • 2
  • 8

1 Answers1

2

You could make a boxplot from the summary stats like so:

library(ggplot2)

ggplot() +
  geom_boxplot(aes(x = factor(1), middle = .863, lower = .863 - .0858, upper = .863 + .0858, ymin = .621, ymax = 1.132), stat = "identity", width = .5)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you so much for your quick reply. I am trying to add the mean to the boxplot. I have tried this code: mean(0.892) ggplot() + geom_boxplot(aes(x = factor(1), middle = .863, lower = .863 - .0858, upper = .863 + .0858, ymin = .621, ymax = 1.132), stat = "identity", width = .5) +stat_summary(fun.y=mean, colour="red"). Unfortunately, it is not working. Do you have a hint for me? – Dolphin94 Dec 23 '20 at 17:56
  • stat_summary will not work as there is no data. But you could add your mean using e.g. `geom_pointrange(aes(xmin = .75, xmax = 1.25, x = factor(1), y = 0.892), colour="red")` which gives the mean as a pointrange. BTW: The xmin and xmax are set as 1 +/- width / 2 – stefan Dec 23 '20 at 18:18
  • Is there an easy way to add the outliers (multiple per group of data), other than using `geom_point` separately ? – steveb Jan 13 '22 at 23:48
  • Hi @steveb. I just had a look at the docs. But I'm afraid there isn't an option to pass a vector of outliers. So you have to put them in a dataframe and add them via `geom_point`. – stefan Jan 14 '22 at 10:36
  • @stefan It appears that way. I just did that but also had to include the additional columns with `middle`, `lower`, `upper`, `ymin`, and `ymax` (I put zero in for values) because ggplot complained about them being missing. I will have to just use `geom_point`. Thanks for looking into this. – steveb Jan 14 '22 at 17:30