R: How to remove outliers from a smoother in ggplot2?
- by John
I have the following data set that I am trying to plot with ggplot2, it is a time series of three experiments A1, B1 and C1 and each experiment had three replicates.
I am trying to add a stat which detects and removes outliers before returning a smoother (mean and variance?). I have written my own outlier function (not shown) but I expect there is already a function to do this, I just have not found it.
I've looked at stat_sum_df("median_hilow", geom = "smooth") from some examples in the ggplot2 book, but I didn't understand the help doc from Hmisc to see if it removes outliers or not.
Is there a function to remove outliers like this in ggplot, or where would I amend my code below to add my own function?
library (ggplot2)
data = data.frame (day = c(1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7), od =
c(
0.1,1.0,0.5,0.7
,0.13,0.33,0.54,0.76
,0.1,0.35,0.54,0.73
,1.3,1.5,1.75,1.7
,1.3,1.3,1.0,1.6
,1.7,1.6,1.75,1.7
,2.1,2.3,2.5,2.7
,2.5,2.6,2.6,2.8
,2.3,2.5,2.8,3.8),
series_id = c(
"A1", "A1", "A1","A1",
"A1", "A1", "A1","A1",
"A1", "A1", "A1","A1",
"B1", "B1","B1", "B1",
"B1", "B1","B1", "B1",
"B1", "B1","B1", "B1",
"C1","C1", "C1", "C1",
"C1","C1", "C1", "C1",
"C1","C1", "C1", "C1"),
replicate = c(
"A1.1","A1.1","A1.1","A1.1",
"A1.2","A1.2","A1.2","A1.2",
"A1.3","A1.3","A1.3","A1.3",
"B1.1","B1.1","B1.1","B1.1",
"B1.2","B1.2","B1.2","B1.2",
"B1.3","B1.3","B1.3","B1.3",
"C1.1","C1.1","C1.1","C1.1",
"C1.2","C1.2","C1.2","C1.2",
"C1.3","C1.3","C1.3","C1.3"))
> data
day od series_id replicate
1 1 0.10 A1 A1.1
2 3 1.00 A1 A1.1
3 5 0.50 A1 A1.1
4 7 0.70 A1 A1.1
5 1 0.13 A1 A1.2
6 3 0.33 A1 A1.2
7 5 0.54 A1 A1.2
8 7 0.76 A1 A1.2
9 1 0.10 A1 A1.3
10 3 0.35 A1 A1.3
11 5 0.54 A1 A1.3
12 7 0.73 A1 A1.3
13 1 1.30 B1 B1.1
This is what I have so far and is working nicely, but outliers are not removed:
r <- ggplot(data = data, aes(x = day, y = od))
r + geom_point(aes(group = replicate, color = series_id)) + # add points
geom_line(aes(group = replicate, color = series_id)) + # add lines
geom_smooth(aes(group = series_id)) # add smoother, average of each replicate