异常值超过6组数据

时间:2016-03-28 20:38:30

标签: r ggplot2 boxplot

我正在尝试为我的数据绘制boxplot。我写了一个定制的函数来改变胡须和异常值的选择。

我的数据中没有任何积分作为异常值,所以在经过一些调查和某些人的外部帮助后,我发现了问题,

我的代码:

f <- function(x) {r <- c(    quantile(x,probs=c(0.25))-(1.5*(quantile(x,probs=c(0.75))-quantile(x,probs=c(0.25))))     ,quantile(x, probs = c(0.25)), quantile(x, probs = c(0.5)), quantile(x, probs = c(0.75)),     quantile(x,probs=c(0.75))+(1.5*(quantile(x,probs=c(0.75))-quantile(x,probs=c(0.25)))) );names(r) <- c("ymin", "lower", "middle", "upper", "ymax"); r}

o <-function(x) {   subset(x,x < (quantile(x, probs = c(0.25)) - (1.5 * (quantile(x, probs = c(0.75)) - quantile(x, probs = c(0.25))))) | x > (quantile(x, probs = c(0.75)) + (1.5 * (quantile(x, probs = c(0.75)) - quantile(x, probs = c(0.25))))))}

dt=read.table("C:/Users/msi161/Desktop/R/test.txt",header=TRUE,sep=",")
data2<-data.frame(x=dt$x,day=dt$day)
data3=data2[order(data2$day),]
data<-data.frame(x=data3$x,day=data3$day)
 dev.new();ggplot(data, aes(day,x)) +   stat_summary(fun.data=f, geom='boxplot')+stat_summary(fun.y =o, geom='point',col='red')

一旦我更改数据量就可以正常工作,

工作正常:

datadd=head(data,43*6)
dev.new();ggplot(datadd, aes(factor(day),x,fill=factor(day))) +   stat_summary(fun.data=f, geom='boxplot')+stat_summary(fun.y =o, geom='point',size=1)

但是我收到了第7个boxplot和后来的boxplots(或我所有数据)的以下错误:

问题:

datadd=head(data,43*7)
dev.new();ggplot(datadd, aes(factor(day),x,fill=factor(day))) +   stat_summary(fun.data=f, geom='boxplot')+stat_summary(fun.y =o, geom='point',size=1)
Warning message:
Computation failed in `stat_summary()`:
arguments imply differing number of rows: 1, 0 

1 个答案:

答案 0 :(得分:1)

问题与我的功能有关,我更正了如下

array = array.sort_by {|k| k[:value] }.reverse

puts array[0][:value]