在R中的箱线图上标记异常值 [英] Labeling outliers on boxplot in R

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

问题描述

我想将矩阵的每一列绘制为箱线图,然后将每个箱线图中的异常值标记为它们在矩阵中所属的行名.举个例子:

I would like to plot each column of a matrix as a boxplot and then label the outliers in each boxplot as the row name they belong to in the matrix. To use an example:

vv=matrix(c(1,2,3,4,8,15,30),nrow=7,ncol=4,byrow=F)
rownames(vv)=c("one","two","three","four","five","six","seven")
boxplot(vv)

我想将每个图中的异常值(在本例中为 30)标记为它所属的行名,因此在本例中 30 属于第 7 行.有没有简单的方法可以做到这一点?我看到了与此问题类似的问题,但似乎没有一个问题能按照我想要的方式工作.

I would like to label the outlier in each plot (in this case 30) as the row name it belongs to, so in this case 30 belongs to row 7. Is there an easy way to do this? I have seen similar questions to this asked but none seemed to have worked the way I want it to.

推荐答案

在给出的示例中,这有点无聊,因为它们都是同一行.但这里是代码:

In the example given it's a bit boring because they are all the same row. but here is the code:

bxpdat <- boxplot(vv)
text(bxpdat$group,                                              # the x locations 
     bxpdat$out,                                                # the y values
     rownames(vv)[which(vv == bxpdat$out, arr.ind=TRUE)[, 1]],  # the labels
     pos = 4)  

这会在 boxplot 的结果中选择值等于out"列表(即异常值)的行名.Boxplot 从 boxplot.stats 调用并返回值.看看:

This picks the rownames that have values equal to the "out" list (i.e., the outliers) in the result of boxplot. Boxplot calls and returns the values from boxplot.stats. Take a look at:

 str(bxpdat)

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

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