简体   繁体   English

结合R中的箱线图

[英]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: 即在第一个y轴上的基本结果,在第二个y轴上的%diff:

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. 编辑(1):您需要定义X轴的刚性序列。 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? 我认为X轴的标签位置不正确? 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 . 所以我在创建boxplot之后添加了每个标签。 First plot the base and label it, then plot exps and label it. 首先绘制base并标记,然后绘制exps并标记。 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... 编辑:为了更清楚,您要添加一个具有3个值的新图,这就是为什么axis(side=1,1:3,labels=c("% exp1","% exp2","% exp3"))是1到3 ...

Edited (2): 编辑(2):

Why don't you use multi rows in the plot and try to plot 2 graphs? 为什么不在图表中使用多行并尝试绘制2个图形? 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 您可以从这里获得有关它的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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