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?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于水平线(如果您水平放置箱线图,则使用 v 而不是 h 表示垂直线)或
点。使用参数
pch
来更改符号。您可能还想给它们上色以提高可见度。请注意,这些是在绘制箱线图后调用的。
如果您使用公式界面,则必须构造均值向量。例如,以
?boxplot
中的第一个示例为例:如果您的数据包含缺失值,您可能需要将
tapply
函数的最后一个参数替换为function( x) 均值(x,na.rm=T)
for a horizontal line (use v instead of h for vertical if you orient your boxplot horizontally), or
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.
If you are using the formula interface, you would have to construct the vector of means. For example, taking the first example from
?boxplot
:If your data contains missing values, you might want to replace the last argument of the
tapply
function withfunction(x) mean(x,na.rm=T)
使用ggplot2:
With
ggplot2
:检查 chart.Boxplot 从包
PerformanceAnalytics.它允许您定义用于分布平均值的符号。
默认情况下,
chart.Boxplot(data)
命令将平均值添加为红色圆圈,将中位数添加为黑线。这是带有示例数据的输出; MWE:
Check chart.Boxplot from package
PerformanceAnalytics
. It lets you define the symbol to use for the mean of the distribution.By default, the
chart.Boxplot(data)
command adds the mean as a red circle and the median as a black line.Here is the output with sample data; MWE:
根据@James和@Jyotirmoy Bhattacharya的回答,我想出了这个解决方案:(
参见这个< /a> post 了解更多详细信息)
如果您想向水平箱形图添加点,请参阅 这篇文章。
Based on the answers by @James and @Jyotirmoy Bhattacharya I came up with this solution:
(See this post for more details)
If you would like to add points to horizontal box plots, please see this post.
我还认为 Chart.Boxplot 是最好的选择,它为您提供平均值的位置,但如果您有一个带有返回值的矩阵,您只需要一行代码即可获取一张图中的所有箱线图。
这是一个小型 ETF 投资组合示例。
返回矩阵
返回矩阵的箱线图
您可以尝试更改mean.symbol,并删除或更改median.symbol。
希望有帮助。 :)
I also think chart.Boxplot is the best option, it gives you the position of the mean but if you have a matrix with returns all you need is one line of code to get all the boxplots in one graph.
Here is a small ETF portfolio example.
Return Matrix
Box Plot of Return Matrix
You can try changing the mean.symbol, and remove or change the median.symbol.
Hope it helped. :)