-2

I am trying to generate a boxplot in R using already computed confidence intervals and min and max. For time 1,2,3,4,5 (x-axis), I have MN which represents array of 5 elements, each describing the mean at time point. I also have CI1, CI2, MINIM, and MAXM, each as an array of 5 elements, one for each time step, representing upper CI, lower CI , minimum and maximum. I want to generate 5 box plots bars at each time step.

I have tried the usual box plot function, but I could get it to work with already computed CIs and min max.

It would be great if the method work for normal plot function, though ggplot woll be fine too.

show_stopper
  • 288
  • 5
  • 17
  • 1
    Please post the data you have **in the question**. – Rui Barradas Jul 26 '18 at 18:34
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Show the code you tried and describe exactly how it doesn't work. – MrFlick Jul 26 '18 at 18:35
  • Possible duplicate: https://stackoverflow.com/questions/11129432/draw-bloxplots-in-r-given-25-50-75-percentiles-and-min-and-max-values – MrFlick Jul 26 '18 at 18:35

1 Answers1

1

Since you have not posted data, I will use the builtin iris dataset, keeping the first 4 columns.

data(iris)
iris2 <- iris[-5]

The function boxplot computes the statistics it uses and then calls bxp to do the printing, passing it those computed values.
If you want a different set of statistics you will have to compute them and pass them to bxp manually.

I am assuming that by CI you mean normal 95% confidence intervals. For that you need to compute the standard errors and the mean values first.

s <- apply(iris2, 2, sd)
mn <- colMeans(iris2)
ci1 <- mn - qnorm(0.95)*s
ci2 <- mn + qnorm(0.95)*s
minm <- apply(iris2, 2, min)
maxm <- apply(iris2, 2, max)

Now have boxplot create the data structure used by bxp, a matrix.

bp <- boxplot(iris2, plot = FALSE)

And fill the matrix with the values computed earlier.

bp$stats <- matrix(c(
  minm,
  ci1,
  mn,
  ci2,
  maxm
), nrow = 5, byrow = TRUE)

Finally, plot it.

bxp(bp)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • thanks. This is exactly what I was looking for. But I notice is that the x axis always starts from 1. is there a way to change the numbering on the x-axis. For example if the array of time is t<-c (2,4,7,9) instead of t<-c(1,2,3,4) – show_stopper Jul 26 '18 at 19:54
  • 1
    figured it out. i should use bp$name<-t – show_stopper Jul 26 '18 at 20:01
  • @show_stopper Yes, either change `bp$name` or set argument `xaxt = "n"` and then customize the axis with function `axis`. – Rui Barradas Jul 26 '18 at 21:14