geom_ribbon not working

1,305 views
Skip to first unread message

Dan Gillie

unread,
Dec 9, 2010, 1:03:54 PM12/9/10
to ggplot2
I am trying to plot a set of dose response data. the graph should
have data points, a 4 parameter logistic curve fit and the confidence
intervals
I have the data points in a data frame. I have the predicted fit
values with upper and lower confidence limits in a separate data
frame.
Using the following code should give me what I want but it does not.

pCurve= ggplot(data= subset(Metric, Cell_Line=="293"),
aes(x=log10(CCH), y=MaxMin))+ theme_bw()
pCurve+geom_point()+geom_line(data=pred, aes(x=log10(x), y=y.hat))
geom_ribbon(data=pred, aes(x= log10(x), ymin=lower, ymax=upper),
fill=alpha("red",0.5) )
If I use all but the last line of code I get teh graph but without the
confidence interval. The confidence interval code works on its own
when I plot it separetly, but not with the rest of the graph.

Any thoughts on why this is and how to fix it?

Dan Gillie

Ista Zahn

unread,
Dec 9, 2010, 1:29:42 PM12/9/10
to Dan Gillie, ggplot2
Hi Dan,
You need a "+" after theme_bw()

-Ista

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Dennis Murphy

unread,
Dec 9, 2010, 9:16:26 PM12/9/10
to Dan Gillie, ggplot2
Hi:

I suspect it's because you didn't define the y variable in geom_ribbon(). Here's an example with lm() [because it's a lot less work to get the confidence limits ;)]:

x <- 1:20
df <- data.frame(x, y = 2 + 0.8 * x + rnorm(20, s = 0.5))
m <- lm(y ~ x, data = df)
pp <- predict(m, interval = 'confidence')
head(pp, 1)
       fit      lwr      upr
1 2.679853 2.151570 3.208136

g <- ggplot(df, aes(x, y)) + theme_bw()
g + geom_point(size = 2.5) + geom_line(aes(y = pp$fit), color = 'red', size = 1) +
     geom_ribbon(data = pp, aes(x = df$x, ymin = lwr, ymax = upr))
# Error: ggplot2 doesn't know how to deal with data of class matrix
class(pp)
# [1] "matrix"    
pp <- as.data.frame(pp)
g + geom_point(size = 2.5) + geom_line(aes(y = pp$fit), color = 'red', size = 1) +
   geom_ribbon(data = pp, aes(x = df$x, ymin = lwr, ymax = upr))
# Error in eval(expr, envir, enclos) : object 'y' not found
g + geom_point(size = 2.5) +
     geom_line(aes(y = pp$fit), color = 'red', size = 1) +
     geom_ribbon(data = pp, aes(x = df$x, y = fit, ymin = lwr, ymax = upr),
                           alpha = 0.2)
# Works

HTH,
Dennis

Reply all
Reply to author
Forward
0 new messages