3

I am trying to customize boxplot, and I was pretty successful except one point. The point that I cannot understand is how I can use stat_summary to show outliers. I have different box plots in one big plot and the outliers do not show on the output. However, if I modify my data and only fed the R with one type of data(only for single box plot) my code works fine and I can see the ourliers crystal clear in the output. I appreciate any help.

Many thanks,

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) { print(x); if (length(x) > 7) { 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)))))); return (pp)} else { return (NA)} }

dt=read.table("C:/...../test.txt",header=TRUE,sep=",")
data<-data.frame(x=dt$x,day=dt$day)
dev.new();ggplot(data, aes(x,day)) +   stat_summary(fun.data=f, geom='boxplot')+stat_summary(fun.data =o, geom='point', col='red')#+  stat_summary(fun.y = o2, geom='point', col='red')
Mohsen Sichani
  • 1,002
  • 12
  • 33

2 Answers2

5

The key is to keep all of your data in the same column and add a label column to differentiate variables. You can do away with the stat_summary function altogether if you use a custom function to detect outliers within the data frame.

Adapted from Labeling Outliers of Boxplots in R.

library(ggplot2)
library(data.table)

# Generate sample
set.seed(123)
n <- 500
dat <- data.table(group=c(rep("A", n/2) , rep("B", n/2)), value=rnorm(n))

Notice how our sample data includes the variables A and B in the same column, differentiated by only the group identifier.

# Create outlier function
check_outlier <- function(v, coef=1.5){
  quantiles <- quantile(v, probs=c(0.25,0.75) )
  IQR <- quantiles[2] - quantiles[1]
  res <- (v < ( quantiles[1]- coef*IQR )) | (v > ( quantiles[2]+ coef*IQR ))
  return(res)
}

# Apply with data.table "by" method
dat[, outlier:=check_outlier(value), by=group]

This function is not common, and it part of the data.table package, but the main point is that is appends a column to the data frame where outliers have label TRUE.

# Plot
ggplot(dat, aes(x=group,y=value)) + 
  geom_boxplot()

Customize your plot from there.

Community
  • 1
  • 1
Chris Conlan
  • 2,774
  • 1
  • 19
  • 23
1

Thanks Chris for your help.

The problem was related to my o function,

 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)}}
Mohsen Sichani
  • 1,002
  • 12
  • 33