I am trying to draw boxplot for my data. I wrote a customized function for changing whisker and outliers selection.
I did not get any points as outliers in my data, so after some investigation and some external help from some kind people, I found out the problem,
My code:
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')
As soon as I change the amount of data it would work fine,
Works fine:
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)
But I am getting the following error for the 7th boxplot and later boxplots (or all of my data):
Problem:
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
问题与我的o函数有关,我按如下纠正
o <-function(x) { pp= 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))))));if(length(pp)<1){pp=c(1);return(pp)}else { return (NA)}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.