I have a plotting question regarding boxplots (using base graphics).
I have several arrays of data which I wish to turn into box plots and compare. The arrays reflect different experiments and what I would like to show is the base results and the percentage difference for the experiments (on one plot!). I.e. the base results on the 1st y axis and the % diff on the second y axis:
base <- array(runif(12*24*3), dim=c(12,24,3))
exp1 <- array(runif(12*24*3), dim=c(12,24,3))
exp2 <- array(runif(12*24*3), dim=c(12,24,3))
exp3 <- array(runif(12*24*3), dim=c(12,24,3))
exp4 <- array(runif(12*24*3), dim=c(12,24,3))
# calc p.diff
p.diff <- function(mod,base) {
100.0*((mod-base)/base) }
a <- p.diff(exp1,base)
b <- p.diff(exp2,base)
c <- p.diff(exp3,base)
# combine the % diff arrays
exps <- list(a,b,c)
# plot the results
boxplot(base, xlim=c(1,4), col="gray", xaxt="n", ylab="Base values", outline=FALSE)
axis(side=1, 1:4, labels=c("base","% exp1","% exp2","% exp3") )
par(new=TRUE)
boxplot(exps, col="red", ylim=c(-200,200), outline=FALSE, axes=FALSE)
axis(4)
grid()
This almost works but I don’t get the positioning of the different box plots right (if you run my example you will see what I mean). So is there a better way to control the placement of the box plots? Or a better way to produce a similar type of figure?
Edited (1): You need to define the rigth sequences for the X axis. So that the plots don’t overlap. Just try to play with it.
I think the labels of the X axes are not at the right place? I don’t know a more elegant way of doing it but here is a solution:
So I added every label after creating the
boxplot. First plot thebaseand label it, then plotexpsand label it. Does it solve your problem?Edit: Just to be more clear, You are adding a new plot with 3 values, that is why
axis(side=1,1:3,labels=c("% exp1","% exp2","% exp3"))is from 1 to 3…Edited (2):
Why don’t you use multi rows in the plot and try to plot 2 graphs? Here is an example with your data:
you can have more information about it from here