我有两组数据(x1和x2对y1和y2),我想将它们显示为两组箱图。
我尝试了以下操作,但它显示错误的数据,因为向量x1和x2(以及y1和y2)的长度不同:
x1 <- c(2,3,4)
x2 <- c(0,1,2,3,4,5)
y1 <- c(3,4,5)
y2 <- c(1,2,3,4,5,6)
d0 <- matrix(c(x1, x2), ncol=2)
d1 <- matrix(c(y1, y2), ncol=2)
lmts <- range(d0,d1)
par(mfrow = c(1, 2))
boxplot(d0, ylim=lmts, xlab="x")
boxplot(d1, ylim=lmts, xlab="y")
这就是它所显示的(当然,根据x1的范围,我希望第一个箱图的胡须从2变为4):
答案 0 :(得分:3)
lmts <- range(x1,x2,y1,y2)
par(mfrow = c(1, 2))
boxplot(x1, x2, ylim=lmts,names=c("x1","x2"),xlab="x")
boxplot(y1, y2, ylim=lmts,names=c("y1","y2"),xlab="y")
完全不基于评论......
> quantile(c(2,3,4), type=1)
0% 25% 50% 75% 100%
2 2 3 4 4
> quantile(c(2,3,4), type=2)
0% 25% 50% 75% 100%
2 2 3 4 4
> quantile(c(2,3,4), type=3)
0% 25% 50% 75% 100%
2 2 3 3 4
> quantile(c(2,3,4), type=4)
0% 25% 50% 75% 100%
2.00 2.00 2.50 3.25 4.00
> quantile(c(2,3,4), type=5)
0% 25% 50% 75% 100%
2.00 2.25 3.00 3.75 4.00
> quantile(c(2,3,4), type=6)
0% 25% 50% 75% 100%
2 2 3 4 4
> quantile(c(2,3,4), type=7)
0% 25% 50% 75% 100%
2.0 2.5 3.0 3.5 4.0
> quantile(c(2,3,4), type=8)
0% 25% 50% 75% 100%
2.000000 2.166667 3.000000 3.833333 4.000000
> quantile(c(2,3,4), type=9)
0% 25% 50% 75% 100%
2.0000 2.1875 3.0000 3.8125 4.0000
答案 1 :(得分:1)
另一个选择是使用ggplot2包。您需要更多的工作才能将数据放入一个data.frame中。但那很容易。
library(ggplot2)
dataset <- data.frame(
Group = c(rep("x1", length(x1)), rep("x2", length(x2)), rep("y1", length(y1)), rep("y2", length(y2))),
Subplot = c(rep("x", length(x1) + length(x2)), rep("y", length(y1) + length(y2))),
Value = c(x1, x2, y1, y2))
ggplot(dataset, aes(x = Group, y = Value)) + geom_boxplot() + facet_wrap(~Subplot, scales = "free_x")