R中的箱线图显示平均值 [英] Boxplot in R showing the mean

查看:3129
本文介绍了R中的箱线图显示平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道在R中用对应于平均值的值中的一行(或另一个符号)生成箱形图的方法吗?

Does anybody know of a way of generating a boxplot in R with a line (or another symbol) in the value corresponding to the mean?

谢谢!

推荐答案

abline(h=mean(x))

用于水平线(如果将箱形图水平放置,则使用v而不是h表示垂直线),或者

for a horizontal line (use v instead of h for vertical if you orient your boxplot horizontally), or

points(mean(x))

一点.使用参数pch更改符号.您可能还需要给它们上色以提高可见性.

for a point. Use the parameter pch to change the symbol. You may want to colour them to improve visibility too.

请注意,在绘制箱线图之后将调用它们.

Note that these are called after you have drawn the boxplot.

如果使用公式接口,则必须构造均值向量.例如,以?boxplot中的第一个示例为例:

If you are using the formula interface, you would have to construct the vector of means. For example, taking the first example from ?boxplot:

boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
means <- tapply(InsectSprays$count,InsectSprays$spray,mean)
points(means,col="red",pch=18)

如果您的数据包含缺失值,则可能需要用function(x) mean(x,na.rm=T)

If your data contains missing values, you might want to replace the last argument of the tapply function with function(x) mean(x,na.rm=T)

这篇关于R中的箱线图显示平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆