问题
你想要绘制一个箱线图。
方案
该方案教你如何快速地使用标准绘图方法绘制简单的箱线图。
样例数据
这里的例子使用ToothGrowth
数据集,它有两个独立变量以及一个依赖变量。
head(ToothGrowth)
#> len supp dose
#> 1 4.2 VC 0.5
#> 2 11.5 VC 0.5
#> 3 7.3 VC 0.5
#> 4 5.8 VC 0.5
#> 5 6.4 VC 0.5
#> 6 10.0 VC 0.5
在不同 supp
和dose
条件下,变量len
的箱线图:
boxplot(len ~ supp, data=ToothGrowth)
# 即使`dose`是一个数值型变量,`boxplot`也会将其转换为因子
boxplot(len ~ dose, data=ToothGrowth)


组合 supp
和 dose
不同水平(即交互项)下len
的箱线图()
boxplot(len ~ interaction(dose,supp), data=ToothGrowth)

注意plot
除了添加了横轴标签和不会自动将数值变量转换为因子变量,它与boxplot
有非常相似的输出。
plot(len ~ interaction(dose,supp), data=ToothGrowth)

网友评论