问题
I have two groups of data (x1 and x2 versus y1 and y2), which I would like to display as two groups of boxplots.
I tried the following, but it displays the wrong data because the vectors x1 and x2 (and y1 and y2) are not of the same lengths:
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")
This is what it shows (of course, I wanted the whiskers of the first boxplot to go from 2 to 4 instead, according to the range of x1, etc.):
回答1:
Yup, or you could have used.
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")
On a complete side not based on the comments...
> 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
回答2:
Another options is to use the ggplot2 package. You need a bit more work to put your data into one data.frame. But then it is very easy.
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")
来源:https://stackoverflow.com/questions/7883190/how-to-display-two-groups-of-boxplots