Data <- data.frame(x <- rnorm(20), y = rnorm(20), g =
factor(c(rep("A", 2), rep("B", 17), "C")))
ggplot(Data, aes(x=x, y=y)) + facet_grid(. ~ g) + geom_point() +
geom_smooth(method="lm", se=FALSE)
does not work, because the facet defined by g == C only has one value,
so geom_smooth complains that
"Error in `[<-.data.frame`(`*tmp*`, var, value = list(`NA` = NULL)) :
missing values are not allowed in subscripted assignments of data frames"
That makes sense, but I would still like to show the smoother for the
facets that do have more than one value. Is there any way to do that?
Thanks!
-Ista
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
--
You received this message because you are subscribed to the ggplot2 mailing list.
To post to this group, send email to ggp...@googlegroups.com
To unsubscribe from this group, send email to
ggplot2+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ggplot2
set.seed(10)
Data <- data.frame(x = rnorm(20), y = rnorm(20), g = factor(c(rep("A",
2), rep("B", 17), "C")))
tmp <- ddply(Data, .(g), .fun= function(df) length(df$y))
single.values <- Data$g %in% tmp[,1][tmp[,2] == 1]
Data2 <- Data[-(which(single.values == TRUE)),]
ggplot() +
geom_point(aes(x = x, y = y), data = Data) +
geom_smooth(method="lm", se=FALSE, aes(x = x, y = y), data = Data2) +
facet_grid(. ~ g)
and it seems a bit simpler. Thanks for pointing me in the right direction!
-Ista
It's a bug, and you can track my progress on it here:
http://github.com/hadley/ggplot2/issues/#issue/58
Hadley
In case anyone else is having trouble with this, the following
workaround does work with version 0.8.5:
data <- data.frame(
x = rnorm(20),
y = rnorm(20),
g = factor(c(rep("A", 2), rep("B", 17), "C")))
data2 <- Data[-which(Data$g == "C"),] # remove facet with single values
data2 <- Data[-which(Data$g == "A"),] # need to remove this also
because lm smooth seems to need at least three values
ggplot(data, aes(x, y)) +
geom_point() +
geom_smooth(method="lm", se=FALSE, data=data2) + # plot the smoother
using the reduced dataset
facet_grid(. ~ g)
-Ista
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
--