问题描述
我有数据框,我想绘制分组的箱线图.这是数据的一部分.
I have data frame and I want to plot grouped boxplots. Here is part of the data.
a <- c(0, 100, 250, 500, 750, 0, 100, 250, 500, 750, 0, 100, 250, 500, 750)
b <- c(2.646, 3.756, 26.270, 30.997, 39.294, 2.646, 3.756, 26.270, 30.997, 39.294, 16.363, 25.950, 38.913,45.000, 47.698)
c <- c(0.0, 0.5, 0.1, 5.8, 21.9, 0.0, 0.5, 0.1, 5.8, 21.9, 9.7. 12.5, 25.1, 29.3, 31.9)
d <- c(14.7, 15.0, 13.8, 18.4, 28.2, 14.7, 15.0, 13.8, 18.4, 28.2, 23.6, 24.0, 25.7, 29.0, 33.1)
我合并向量以得到一个数据帧,如下所示.
I merged the vectors to get a data frame as follows.
data <- cbind(a, b, c, d)
然后,我尝试获取b,c和d的三个值的箱线图,作为a的函数,如下所示.
Then I tried to get boxplot for the three values of b, c and d as a funtion of a as follows.
boxplot(b~a, c~a, d~a, data = data)
但是我无法获得所需的分组箱线图,它是每个"a"值的三个箱线图.但是,如果这样做,我可以为每个"a"值获取一个箱线图:
but I couldn't get the desired grouped boxplot which is three boxplots for each value of "a". But I can get a boxplot for each value of "a" if I do:
boxplot(b~a, data = data)
我的分组箱图有什么问题?
What is wrong with my grouped boxplot?
推荐答案
首先以长格式而不是宽格式创建数据集.
First create a dataset in long format rather than a wide format.
wide <- data.frame(
a = c(0, 100, 250, 500, 750, 0, 100, 250, 500, 750, 0, 100, 250, 500, 750),
b = c(2.646, 3.756, 26.270, 30.997, 39.294, 2.646, 3.756, 26.270, 30.997, 39.294, 16.363, 25.950, 38.913,45.000, 47.698),
c = c(0.0, 0.5, 0.1, 5.8, 21.9, 0.0, 0.5, 0.1, 5.8, 21.9, 9.7, 12.5, 25.1, 29.3, 31.9),
d = c(14.7, 15.0, 13.8, 18.4, 28.2, 14.7, 15.0, 13.8, 18.4, 28.2, 23.6, 24.0, 25.7, 29.0, 33.1)
)
library(reshape2)
long <- melt(wide, id.vars = "a")
然后将a转换为因子,您可以使用它来定义组
Then convert a into a factor you you can use it to define groups
long$a <- factor(long$a)
最后,您可以使用ggplot2进行绘制
Finally you can plot this with ggplot2
library(ggplot2)
ggplot(long, aes(x = a, y = value, colour = variable)) + geom_boxplot()
这篇关于分组箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!