如何在 R 中使用均值和标准差创建箱形图?
用于创建箱形图的主要统计参数是均值和标准差,但一般而言,箱形图是用全部数据创建的,而不是用这些值创建的。如果我们没有全部数据,但可以得到均值和标准差,那么可以使用均值作为中心趋势度量,通过查找箱形图的所有限制来创建箱形图。
示例
考虑下表数据框
> df<-data.frame(mean=c(24,25,27,24),sd=c(1.1,2.1,1.5,1.8),Category=as.factor(c("A","B","C","D"))) > df
输出
mean sd Category 1 24 1.1 A 2 25 2.1 B 3 27 1.5 C 4 24 1.8 D
加载 ggplot2 包,创建 df 中各个类别对应的箱形图
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
> library(ggplot2) > ggplot(df,aes(x=Category))+geom_boxplot(aes(lower=mean-sd,upper=mean+sd,middle=mean,ymin=mean-3*sd,ymax=mean+3*sd),stat="identity")
输出
广告