subset in geom_point SOMETIMES returns full dataset, instead of none.

Posted by Andreas on Stack Overflow See other posts from Stack Overflow or by Andreas
Published on 2010-06-11T15:33:39Z Indexed on 2010/06/11 19:42 UTC
Read the original article Hit count: 286

Filed under:
|

I ask the following in the hope that someone might come up with a generic description about the problem.Basically I have no idea whats wrong with my code.

When I run the code below, plot nr. 8 turns out wrong. Specifically the subset in geom_point does not work the way it should. (update: With plot nr. 8 the whole dataset is plottet, instead of only the subset).

If somebody can tell me what the problem is, I'll update this post.

SOdata <-     structure(list(id = 10:55, one = c(7L, 8L, 7L, NA, 7L, 8L, 5L, 
7L, 7L, 8L, NA, 10L, 8L, NA, NA, NA, NA, 6L, 5L, 6L, 8L, 4L, 
7L, 6L, 9L, 7L, 5L, 6L, 7L, 6L, 5L, 8L, 8L, 7L, 7L, 6L, 6L, 8L, 
6L, 8L, 8L, 7L, 7L, 5L, 5L, 8L), two = c(7L, NA, 8L, NA, 10L, 
10L, 8L, 9L, 4L, 10L, NA, 10L, 9L, NA, NA, NA, NA, 7L, 8L, 9L,
10L, 9L, 8L, 8L, 8L, 8L, 8L, 9L, 10L, 8L, 8L, 8L, 10L, 9L, 10L, 
8L, 9L, 10L, 8L, 8L, 7L, 10L, 8L, 9L, 7L, 9L), three = c(7L, 
10L, 7L, NA, 10L, 10L, NA, 10L, NA, NA, NA, NA, 10L, NA, NA, 
4L, NA, 7L, 7L, 4L, 10L, 10L, 7L, 4L, 7L, NA, 10L, 4L, 7L, 7L, 
7L, 10L, 10L, 7L, 10L, 4L, 10L, 10L, 10L, 4L, 10L, 10L, 10L, 
10L, 7L, 10L), four = c(7L, 10L, 4L, NA, 10L, 7L, NA, 7L, NA, 
NA, NA, NA, 10L, NA, NA, 4L, NA, 10L, 10L, 7L, 10L, 10L, 7L, 
7L, 7L, NA, 10L, 7L, 4L, 10L, 4L, 7L, 10L, 2L, 10L, 4L, 12L, 
4L, 7L, 10L, 10L, 12L, 12L, 4L, 7L, 10L), five = c(7L, NA, 6L, 
NA, 8L, 8L, 7L, NA, 9L, NA, NA, NA, 9L, NA, NA, NA, NA, 7L, 8L, 
NA, NA, 7L, 7L, 4L, NA, NA, NA, NA, 5L, 6L, 5L, 7L, 7L, 6L, 9L, 
NA, 10L, 7L, 8L, 5L, 7L, 10L, 7L, 4L, 5L, 10L), six = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2010-05-25", 
"2010-05-27", "2010-06-07"), class = "factor"), seven = c(0.777777777777778, 
0.833333333333333, 0.333333333333333, 0.888888888888889, 0.5, 
0.888888888888889, 0.777777777777778, 0.722222222222222, 0.277777777777778, 
0.611111111111111, 0.722222222222222, 1, 0.888888888888889, 0.722222222222222, 
0.555555555555556, NA, 0, 0.666666666666667, 0.666666666666667, 
0.833333333333333, 0.833333333333333, 0.833333333333333, 0.833333333333333, 
0.722222222222222, 0.833333333333333, 0.888888888888889, 0.666666666666667, 
1, 0.777777777777778, 0.722222222222222, 0.5, 0.833333333333333, 
0.722222222222222, 0.388888888888889, 0.722222222222222, 1, 0.611111111111111, 
0.777777777777778, 0.722222222222222, 0.944444444444444, 0.555555555555556, 
0.666666666666667, 0.722222222222222, 0.444444444444444, 0.333333333333333, 
0.777777777777778), eight = c(0.666666666666667, 0.333333333333333, 
0.833333333333333, 0.666666666666667, 1, 1, 0.833333333333333, 
0.166666666666667, 0.833333333333333, 0.833333333333333, 1, 1, 
0.666666666666667, 0.666666666666667, 0.333333333333333, 0.5, 
0, 0.666666666666667, 0.5, 1, 0.666666666666667, 0.5, 0.666666666666667, 
0.666666666666667, 0.666666666666667, 0.333333333333333, 0.333333333333333,     
1, 0.666666666666667, 0.833333333333333, 0.666666666666667, 0.666666666666667, 
0.5, 0, 0.833333333333333, 1, 0.666666666666667, 0.5, 0.666666666666667, 
0.666666666666667, 0.5, 1, 0.833333333333333, 0.666666666666667, 
0.833333333333333, 0.666666666666667), nine = c(0.307692307692308, 
NA, 0.461538461538462, 0.538461538461538, 1, 0.769230769230769, 
0.538461538461538, 0.692307692307692, 0, 0.153846153846154, 0.769230769230769, 
NA, 0.461538461538462, NA, NA, NA, NA, 0, 0.615384615384615, 
0.615384615384615, 0.769230769230769, 0.384615384615385, 0.846153846153846, 
0.923076923076923, 0.615384615384615, 0.692307692307692,     0.0769230769230769, 
0.846153846153846, 0.384615384615385, 0.384615384615385, 0.461538461538462, 
0.384615384615385, 0.461538461538462, NA, 0.923076923076923, 
0.692307692307692, 0.615384615384615, 0.615384615384615, 0.769230769230769, 
0.0769230769230769, 0.230769230769231, 0.692307692307692, 0.769230769230769, 
0.230769230769231, 0.769230769230769, 0.615384615384615), ten = c(0.875, 
0.625, 0.375, 0.75, 0.75, 0.75, 0.625, 0.875, 1, 0.125, 1, NA, 
0.625, 0.75, 0.75, 0.375, NA, 0.625, 0.5, 0.75, 0.875, 0.625, 
0.875, 0.75, 0.625, 0.875, 0.5, 0.75, 0, 0.5, 0.875, 1, 0.75, 
0.125, 0.5, 0.5, 0.5, 0.625, 0.375, 0.625, 0.625, 0.75, 0.875, 
0.375, 0, 0.875), elleven = c(1, 0.8, 0.7, 0.9, 0, 1, 0.9, 0.5, 
0, 0.8, 0.8, NA, 0.8, NA, NA, 0.8, NA, 0.4, 0.8, 0.5, 1, 0.4, 
0.5, 0.9, 0.8, 1, 0.8, 0.5, 0.3, 0.9, 0.2, 1, 0.8, 0.1, 1, 0.8, 
0.5, 0.2, 0.7, 0.8, 1, 0.9, 0.6, 0.8, 0.2, 1), twelve = c(0.666666666666667, 
NA, 0.133333333333333, 1, 1, 0.8, 0.4, 0.733333333333333, NA, 
0.933333333333333, NA, NA, 0.6, 0.533333333333333, NA, 0.533333333333333, 
NA, 0, 0.6, 0.533333333333333, 0.733333333333333, 0.6, 0.733333333333333, 
0.666666666666667, 0.533333333333333, 0.733333333333333, 0.466666666666667, 
0.733333333333333, 1, 0.733333333333333, 0.666666666666667, 0.533333333333333, 
NA, 0.533333333333333, 0.6, 0.866666666666667, 0.466666666666667, 
0.533333333333333, 0.333333333333333, 0.6, 0.6, 0.866666666666667, 
0.666666666666667, 0.6, 0.6, 0.533333333333333)), .Names = c("id", 
"one", "two", "three", "four", "five", "six", "seven", "eight", 
"nine", "ten", "elleven", "twelve"), class = "data.frame", row.names = c(NA, 
-46L))


iqr <- function(x, ...) {
  qs <- quantile(as.numeric(x), c(0.25, 0.5, 0.75), na.rm = T)
  names(qs) <- c("ymin", "y", "ymax")
  qs
}

magic <- function(y, ...) { 
high <- median(SOdata[[y]], na.rm=T)+1.5*sd(SOdata[[y]],na.rm=T)
low <-  median(SOdata[[y]], na.rm=T)-1.5*sd(SOdata[[y]],na.rm=T)
ggplot(SOdata, aes_string(x="six", y=y))+
stat_summary(fun.data="iqr", geom="crossbar", fill="grey", alpha=0.3)+
geom_point(data = SOdata[SOdata[[y]] > high,], position=position_jitter(w=0.1, h=0),col="green", alpha=0.5)+
geom_point(data = SOdata[SOdata[[y]] < low,], position=position_jitter(w=0.1, h=0),col="red", alpha=0.5)+
stat_summary(fun.y=median, geom="point",shape=18 ,size=4, col="orange")
}

for (i in names(SOdata)[-c(1,7)]) {
p<- magic(i)
ggsave(paste("magig_plot_",i,".png",sep=""), plot=p, height=3.5, width=5.5)
}

© Stack Overflow or respective owner

Related posts about r

    Related posts about ggplot2