views:

98

answers:

1

I have some data that I am trying to plot faceted by its Type with a smooth (Loess, LM, whatever) superimposed. Generation code is below:

testFrame <- data.frame(Time=sample(20:60,50,replace=T),Dollars=round(runif(50,0,6)),Type=sample(c("First","Second","Third","Fourth"),50,replace=T,prob=c(.33,.01,.33,.33)))

I have no problem either making a faceted plot, or plotting the smooth, but I cannnot do both. The first three lines of code below work fine. The fourth line is where I have trouble:

qplot(Time,Dollars,data=testFrame,colour=Type)
qplot(Time,Dollars,data=testFrame,colour=Type) + geom_smooth()
qplot(Time,Dollars,data=testFrame) + facet_wrap(~Type)
qplot(Time,Dollars,data=testFrame) + facet_wrap(~Type) + geom_smooth()

It gives the following error:

Error in [<-.data.frame(*tmp*, var, value = list(NA = NULL)) : missing values are not allowed in subscripted assignments of data frames

What am I missing to overlay a smooth in a faceted plot? I could have sworn I had done this before, possibly even with the same data.

A: 

It works for me. Are sure you have the latest version of ggplot2?

hadley
Downloaded ggplot2 within the past 2 weeks. I THINK the error may be caused when one of the Type's only has one record, thus not allowing loess() or lm() to compute properly. Computing testFrame as below illustrates that. testFrame <- data.frame(Time=sample(20:60,50,replace=T),Dollars=round(runif(50,0,6)),Type=c(rep("Second",10),sample(c("First","Third","Fourth"),40,replace=T,prob=c(.33,.34,.33))))Or you could simply up the probability for the "Second" Type to be sampled.
Jared
That data set still works for me.
hadley