confidence intervals with geom_smooth()

8,674 views
Skip to first unread message

Shai

unread,
Oct 12, 2010, 3:52:26 PM10/12/10
to ggplot2
Hi!

Maybe this is for beginners but I still cant get that right... How do
I use the geom_smooth to draw the dashed 95% confidence intervals
above and below my trend line? I got to the point where I make the
gray fill disappear, now I only have to add the two dashed borders!

xvalues <- 0:10
yvalues <- c(1,3,2,4,5.5,6,2,9,7,8.2,9)
d <- data.frame(xvalues,yvalues)
p <- ggplot(d, aes(x=xvalues,y=yvalues))
p+geom_point()+geom_smooth(method="lm",fill="NA")


The borders ARE the confidence intervals right?

Thanks,
Shai

Brian Diggs

unread,
Oct 12, 2010, 5:17:48 PM10/12/10
to ggplot2


On Oct 12, 12:52 pm, Shai <shain...@gmail.com> wrote:
> Hi!
>
> Maybe this is for beginners but I still cant get that right... How do
> I use the geom_smooth to draw the dashed 95% confidence intervals
> above and below my trend line? I got to the point where I make the
> gray fill disappear, now I only have to add the two dashed borders!
>
> xvalues <- 0:10
> yvalues <- c(1,3,2,4,5.5,6,2,9,7,8.2,9)
> d <- data.frame(xvalues,yvalues)
> p <- ggplot(d, aes(x=xvalues,y=yvalues))
> p+geom_point()+geom_smooth(method="lm",fill="NA")

geom_smooth does not have a line around it, so there isn't any way to
make it show up. geom_ribbon, on the other hand, does.

p+geom_point()+geom_smooth(method="lm",fill="NA")+
stat_smooth(method="lm",fill=NA,colour="black",linetype=2,geom="ribbon")

You can adjust the color and linetype as you like.

> The borders ARE the confidence intervals right?
>
> Thanks,
> Shai

--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Dennis Murphy

unread,
Oct 13, 2010, 5:39:08 AM10/13/10
to Shai, ggplot2
Hi:

If you don't like the contiguity of the confidence envelopes at the extremes of the x's, here's the brute force approach: compute the linear model, get the upper and lower limits from lm, concatenate to the original data, and use it as input into ggplot():


xvalues <- 0:10
yvalues <- c(1,3,2,4,5.5,6,2,9,7,8.2,9)
d <- data.frame(xvalues,yvalues)

# fit model and get lower/upper 95% confidence limits at the observed x's
m <- lm(yvalues ~ xvalues, data = d)
mp <- predict(m, interval = 'conf')

# bind predictions to original data and plot
d2 <- cbind(d, mp)
p <- ggplot(d2, aes(x = xvalues, y = yvalues))
p + geom_point() + geom_smooth(method = 'lm', se = FALSE) +
    geom_line(aes(y = upr), color = 'red', linetype = 2) +
    geom_line(aes(y = lwr), color = 'red', linetype = 2)

A few extra lines of code, but nothing difficult.

HTH,
Dennis


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

Reply all
Reply to author
Forward
0 new messages