在R中并排绘制几组箱形图

在R中并排绘制几组箱形图

本文介绍了在R中并排绘制几组箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一图中绘制两个箱形图,每个都在同一类别中。
我可以分别生成箱线图,但是当我尝试将它们绘制到同一张图上时会陷入困境。

I am trying to plot two box-plots in the same plot, each within the same category.I can generate the boxplots individually, but am stumped when I try to get them onto the same graph.

这是我到目前为止的内容:

Here is what I have so far:

a<-matrix(nrow=100,ncol=3,data=runif(300,max=2))
b<-matrix(nrow=100,ncol=3,data=runif(300,max=1))
colnames(a)<-c("case 1","case 2","case 3")
colnames(b)<-c("case 1","case 2","case 3")
boxplot(cbind(a,b))

此图导致6个箱型图,前3个代表a,然后3个代表b。

This plot results in 6 boxplots, first 3 for a, then 3 for b.

我是否缺少技巧/简单的选项,它会给我
a和b的第一个值,然后是第二个,最后是第三组值,所有绘制方式都只有x轴上的三个刻度,每组三个刻度?

Is there a trick/simple option that I am missing that will give mefirst value for a and b, then second and finally the third set of values, all plotted in such a way there are is only three ticks on the x-axis, one for each of the sets?

任何指针都会受到赞赏,

Any pointers greatly appreciated,

Iain

推荐答案

boxplot(a, at = 0:2*3 + 1, xlim = c(0, 9), ylim = range(a, b), xaxt = "n")
boxplot(b, at = 0:2*3 + 2, xaxt = "n", add = TRUE)
axis(1, at = 0:2*3 + 1.5, labels = colnames(a), tick = TRUE)

请注意 ylim = range(a,b)参数。绘图比例由第一个命令确定,但是如果b包含a中的值范围之外的值(在这种情况下不是,但是尝试交换a和b),则它们将不在绘图范围内。这就是为什么通常应该在此处指定ylim的原因。

Note the ylim = range(a, b) parameter. The plot scale is determined by the first command, but if b contained values out of range of values in a (not in this case, but try to swap a and b), they would lie out of the plot. That's why in general you should specify the ylim here.

还可以在 axis()命令,我认为它更好。
如果您不喜欢组之间的空间,请使用 0:2 * 2 而不是 0:2 * 3 ,并适当地更改xlim。

You can also set tick = FALSE in the axis() command, I think it is nicer.If you don't like the space between the groups, use 0:2*2 instead of 0:2*3, and change the xlim appropriatelly.

这篇关于在R中并排绘制几组箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:02