![9f6aae9ea6155661d1027ad06574f123.png](https://i-blog.csdnimg.cn/blog_migrate/5760d2fa9440243a97a1c01bc2b619e0.png)
![57bf214b1106422034afcb28af37b09c.png](https://i-blog.csdnimg.cn/blog_migrate/8c6a00d01485c3ddf0c8b8486f726d4c.jpeg)
图中标示了箱线图中每条线和点表示的含义,其中应用到了分位数的概念 线的主要包含五个数据节点,将一组数据从大到小排列,分别计算出他的上边缘(Maximum),上四分位数(Q3),中位数(Median),下四分位数(Q1),下边缘(Minimum) 不在上边缘与下边缘的范围内的为异常值,用点表示。
数据准备
data <- data.frame(Value = rnorm(300),
Repeat = rep(paste("Repeat", 1:3, sep = "_"), 100),
Condition = rep(c("Control", "Test"), 150))
> head(data)
Value Repeat Condition
1 -1.1395507 Repeat_1 Control
2 0.7319707 Repeat_2 Test
3 -0.2219461 Repeat_3 Control
4 -1.1454664 Repeat_1 Test
5 1.0740937 Repeat_2 Control
6 0.3741845 Repeat_3 Test
boxplot函数(R自带)
最方便的方法就是用boxplot函数,不需要依赖任何包
boxplot(data$Value, ylab="Value")
![5316b8e792c96ecc70569561d272ca9d.png](https://i-blog.csdnimg.cn/blog_migrate/99b824f97fdc7ee3dcfe9f1eba22a5af.jpeg)
根据不同的条件,加上颜色
boxplot(Value ~ Condition, data=data, ylab="Value", col=c("darkred", "darkgreen"))
boxplot(Value ~ Condition * Repeat, data=data, ylab="Value", col="darkgreen")
![7f4da8972bbe0d037c22df46f0e55982.png](https://i-blog.csdnimg.cn/blog_migrate/b05e9310e980f6e961a8858351f12870.jpeg)
多个分组(condition和repeat)的箱线图
boxplot(Value ~ Condition + Repeat, data=data, ylab="Value", col="darkgreen")