简体   繁体   中英

Combining boxplots in R

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!). Ie 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:

# plot the results
boxplot(base, xlim=c(1,4), col="gray", xaxt="n", ylab="Base values", outline=FALSE)
axis(side=1,1,labels=('base'))
par(new=TRUE)
boxplot(exps, col="red", ylim=c(-200,200), outline=FALSE, axes=FALSE)
axis(4)
axis(side=1,1:3,labels=c("% exp1","% exp2","% exp3"))
grid()

So I added every label after creating the boxplot . First plot the base and label it, then plot exps and 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:

#divide your plottin area into 2 columns with one row.
par(mfrow = c(1, 2))
# plot the results
boxplot(base, col="gray", xaxt="n", ylab="Base values", outline=FALSE,axes=FALSE)
axis(2)
axis(side=1,1,labels=('base'))
segments(0,0,1,0)
boxplot(exps,col="red", xaxt="n", ylim=c(-200,200), outline=FALSE, axes=FALSE)
axis(4)
axis(side=1,at=(1:3),labels=c("% exp1","% exp2","% exp3"))

you can have more information about it from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM