将异常值显示为百分比

时间:2016-01-25 08:01:04

标签: r percentage boxplot outliers

如何在箱线图中显示异常值作为百分比?

我不希望看到异常值显示为点。我希望看到异常值在箱线图中显示为数字百分比。下面是我想要异常值的图片:

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个解决方案:我们使用boxplot的输出计算异常值的数量,从图中删除异常值,然后绘制比例。

# simulate data
x <- rnorm(1000)
# first boxplot to get the stats
p <- boxplot(x)

# computing the proportion of outliers
outsiders <- data.frame(text= paste0(c(length(p$out < p$stats[1])/p$n,length(p$out > p$stats[5])/p$n)*100,"%"),
                        y = p$stats[c(1,5)])

# real plot
boxplot(x,outline=FALSE,ylim=c(p$stats[1]-1,p$stats[5]+1))
# we add the text in two steps because we need different adj
text(x=1,labels=outsiders$text[1],y=outsiders$y[1],adj=c(0,1))
text(x=1,labels=outsiders$text[2],y=outsiders$y[2],adj=c(0,-.5))

enter image description here

相关问题