Hi,
geom_ and stat_smooth fail to produce a plot if they are used on a dataset in which only one of the facetted plots would be empty.
Maybe I can explain this better with an example.
Let's create a plot with facets and stat_geom on the diamonds dataset:
>>> qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm')
The plot is created and it is nice, it has 8 different smaller plots.
The problem is that any algorithm used by stat_smooth produces an error if it is applied to a dataset with too few points, and when you apply it to faceted data this is more likely to happen.
Let's add a new data to diamonds, adding a new value for 'clarity' and with no price:
>>> newdata <- data.frame(carat=0.4, cut='Ideal', color='E', clarity='NEW', depth=33.2, table=33, price=NA, x=2.1, y=2.2, z=2.2)
>>> d2 <- rbind(diamonds, newdata)
Now, if you apply the same function to d2, you will get an error and no plot is shown at all:
>>> qplot(data=d2, carat, price, facets=~clarity) + stat_smooth(method='lm')
Errore in `[<-.data.frame`(`*tmp*`, var, value = list(`NA` = NULL)) :
i valori missing non sono ammissibili cone indici in assegnazioni di data frame
Inoltre: Warning message:
Removed 1 rows containing missing values (stat_smooth).
This behaviour is fine because I am trying to apply a smoothing/spline function to a dataset with too few points; however, I would like to have a plot in which all the facets in which the smoothing function can be calculated are shown, and for those on which the smoothing function returns an error, just plot them without the smoothing line.
Is it possible to do this with ggplot2? How can I do it? Maybe by rewriting the lm or loess function and returning no value in case the input data is too short?
--
Giovanni Dall'Olio, phd student
Department of Biologia Evolutiva at CEXS-UPF (Barcelona, Spain)
My blog on bioinformatics:
http://bioinfoblog.it