1

I asked this question and it seams ggplot2 currently has a bug with empty data.frames.

Therefore I am trying to check if the dataframe is empty, before I make the plot. But what ever I come up with, it gets really ugly, and doesn't work. So I am asking for your help.

example data:

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))

And the plot

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)
}

The problem is that sometimes in the call to geom_point the subset returns an empty dataframe, which sometimes (!) causes ggplot2 to plot all the data instead of none of the data.

geom_point(data = SOdata[SOdata[[y]] > high,], position=position_jitter(w=0.1, h=0),col="green", alpha=0.5)+

This is kindda of important to me, and I am really stuck trying to find a solution. Any help that will get me started is much appreciated.

Thanks in advance.

1 Answer 1

7

I guess you could replace this

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")
}

with something like

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)
k <- SOdata[[y]] > high
z <- SOdata[[y]] < low
k[is.na(k)]<- FALSE
z[is.na(z)]<- FALSE
p <- ggplot(SOdata, aes_string(x="six", y=y))+
stat_summary(fun.data="iqr", geom="crossbar", fill="grey", alpha=0.3)
if (sum(k) > 0) {p <- p + geom_point(data = SOdata[k,], position=position_jitter(w=0.1, h=0),col="green", alpha=0.5)}
if (sum(z) > 0) {p <- p + geom_point(data = SOdata[z,], position=position_jitter(w=0.1, h=0),col="red", alpha=0.5)}
p + stat_summary(fun.y=median, geom="point",shape=18 ,size=4, col="orange")
}
Sign up to request clarification or add additional context in comments.

1 Comment

wauw. That is brilliant and simple (at least I get it!) - thank you so much. I am especially glad that you handle NA data. I would have fogotten that and ended up with the wrong solution. also the key seams to divide the plotting function up. I had problems because i couldn't embed "if" in the ggplot code. working with "p" is a good solution. Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.