geom_smooth when one facet has a single value

73 views
Skip to first unread message

Ista Zahn

unread,
Dec 21, 2009, 12:42:02 PM12/21/09
to ggplot2
Hi all,
I'm having the following problem:

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

David Kahle

unread,
Dec 21, 2009, 1:17:40 PM12/21/09
to Ista Zahn, ggplot2
Hi Ista -

You can do the work of a stat manually with a little work :

df <- data.frame(
  x = rnorm(20), 
  y = rnorm(20), 
  g = factor(c(rep("A", 2), rep("B", 17), "C"))
)

s <- sort(c(df$x, pretty(extendrange(df$x), n = 101)))

df2 <- data.frame(
  x = rep(s, 3),
  g = rep(factor(c('A','B','C')), each = length(s))
)
df2 <- merge(df2, df, all.x = TRUE)

df2 <- ddply(df2, .(g), 
  function(df){
    df  <- df[order(df$x),]
    mod <- lm(y ~ x, data = df)
    df$pred <- predict(mod, df[, 'x', drop = FALSE])
    df
  }
)

df2$pred[df2$g == 'C'] <- NA

ggplot() + 
  geom_point(aes(x = x, y = y), data = df) +
  geom_path(aes(x = x, y = pred), data = df2) +
  facet_grid(. ~ g)

Is that what you were looking for?

Cheers
david.

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

Ista Zahn

unread,
Dec 21, 2009, 1:43:35 PM12/21/09
to David Kahle, ggplot2
Thanks David, that is what I want. In fact I think this also works:

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

hadley wickham

unread,
Dec 22, 2009, 6:59:16 AM12/22/09
to Ista Zahn, ggplot2
> "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?

It's a bug, and you can track my progress on it here:
http://github.com/hadley/ggplot2/issues/#issue/58

Hadley

--
http://had.co.nz/

Ista Zahn

unread,
Dec 22, 2009, 1:15:27 PM12/22/09
to hadley wickham, ggplot2
Thanks 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

--

Reply all
Reply to author
Forward
0 new messages