I would like to change the way base boxplot function displays my data.
I would like to have the interdeciles as my whiskers instead of what boxplot is currently do now (min-max or the interquartile range multiplied by a given number).
So I tried to save the boxplot in a variable (p
), and replace its "stats" attribute (which defines the median, the IQR and the whiskers) with my quantiles of choice.
However, I am not able to redraw the object (with ggplot I probably would just call p
, but in the case of boxplot, it draws as soon as I instantiate it (even when saved into a variable) and then nothing happens.
x <- c(0.94063886031258,1.03136025036433,0.952395711064118,
0.941071695551853,0.862653162449957,0.814470708156107,
1.05880072370121,0.984234604877728,1.01529921088281,
0.946563805766234,0.931529458894898,1.05621485359968)
set.seed(300)
x.boot <- sample(abs(1-x),replace=TRUE,size=1000)
x.q <- quantile(x.boot,c(.1,.25,.5,.75,.9))
p <- boxplot(x.boot)
p[["stats"]] <- matrix(x.q,ncol=1)
p
EDIT
I saw that I can actually do this to save the plot into an editable object that can be then redrawn, but this is far from being ideal, so if there is no other alternative I will probably use ggplot instead.
boxplot(x.boot)
# save the plot into an editable object
p <- recordPlot()
# make some edits in the right places
p[[1]][[9]][[2]][[3]] <- c(x.q[2],x.q[2],x.q[4],x.q[4])
p[[1]][[8]][[2]][[5]] <- c(x.q[1],x.q[5])
p[[1]][[8]][[2]][[3]] <- c(x.q[1],x.q[5])
p[[1]][[7]][[2]][[3]] <- c(x.q[1],x.q[5])
p[[1]][[7]][[2]][[5]] <- c(x.q[2],x.q[4])
p[[1]][[4]][[2]][[3]] <- c(x.q[2],x.q[2],x.q[4],x.q[4])
# redraw
plot.new()
p
What you need is bxp()
. It draws box plots based on the given summaries.
p1 <- p2 <- boxplot(x.boot)
x.q <- quantile(x.boot, c(.1, .25, .5, .75, .9))
p2[["stats"]] <- matrix(x.q, ncol = 1)
par(mfrow = c(1, 2))
bxp(p1, main = "Before")
bxp(p2, main = "After")