Plot 95%CI with lines, not shading ()

4,610 views
Skip to first unread message

Ben

unread,
May 2, 2011, 11:22:59 PM5/2/11
to ggplot2
Hi there. Love ggplot2 and thanks for putting it out there for us.

i get a nice y~x plot with regression line and shaded 95% Confidence
interval with this (via Deducer):

ggplot() +
+ geom_smooth(aes(x = dlco,y = Sat),data=p,colour = '#000000',alpha =
0.3,method = 'lm',formula = 'y ~ x') +
+ geom_point(aes(x = dlco,y = Sat),data=p)

My question is - is it possible to draw the confidence intervals with
a line, and no shading?

i have done this with the R basic graphics, but it involved creating
additional vectors and using predict() function.

i can only presume that ggplot is doing something similar under the
hood and then returning a layer for the shading - so i want to ask if
i can add a layer with dashed lines instead.

thanks for any help...

ben

Dennis Murphy

unread,
May 3, 2011, 12:43:01 AM5/3/11
to Ben, ggplot2
Hi:

How about something like this?

x <- 1:10
dd <- data.frame(x = x, y = 0.8 + 0.5 * x + rnorm(10, s = 1.5))

m <- lm(y ~ x, data = dd)
pp <- data.frame(x = x, predict(m, interval = 'confidence')

g <- ggplot(dd, aes(x = x, y = y)) + geom_point(size = 2.5) +
geom_smooth(method = 'lm', size = 1, se = FALSE)
g + geom_smooth(data = pp, aes(y = lwr), color = 'dodgerblue') +
geom_smooth(data = pp, aes(y = upr), color = 'dodgerblue')

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
>

Ben

unread,
May 3, 2011, 8:36:02 AM5/3/11
to ggplot2
thanks denis
yes this is how i manually coded it before, i was just asking if
ggplot can plot this with a built-in. if not, i will do it like that.

(have 10 such plots to do for a publication. sigh ...)

ben

On May 3, 12:43 am, Dennis Murphy <djmu...@gmail.com> wrote:
> Hi:
>
> How about something like this?
>
> x <- 1:10
> dd <- data.frame(x = x, y = 0.8 + 0.5 * x + rnorm(10, s = 1.5))
>
> m <- lm(y ~ x, data = dd)
> pp <- data.frame(x = x, predict(m, interval = 'confidence')
>
> g <- ggplot(dd, aes(x = x, y = y)) + geom_point(size = 2.5) +
>        geom_smooth(method = 'lm', size = 1, se = FALSE)
> g + geom_smooth(data = pp, aes(y = lwr), color = 'dodgerblue') +
>     geom_smooth(data = pp, aes(y = upr), color = 'dodgerblue')
>
> HTH,
> Dennis
>

ONKELINX, Thierry

unread,
May 3, 2011, 9:02:20 AM5/3/11
to Ben, ggplot2
Here is another solution.

x <- 1:10
dd <- data.frame(x = x, y = 0.8 + 0.5 * x + rnorm(10, s = 1.5))

ggplot(dd, aes(x = x, y = y)) + geom_point() + stat_smooth(method = "lm", se = FALSE) + stat_smooth(method = "lm", colour = "red", geom = "ribbon", fill = NA)

#some tweaking to get rid to the vertical lines of the ribbon. The trick: expand the range with scale() then zoom in with coord()
ggplot(dd, aes(x = x, y = y)) + geom_point() + stat_smooth(method = "lm", se = FALSE) + stat_smooth(method = "lm", colour = "red", geom = "ribbon", fill = NA, fullrange = TRUE) + scale_x_continuous(limits = diff(range(dd$x)) * c(-0.2, 0.2) + range(dd$x)) + coord_cartesian(xlim = range(pretty(dd$x)))

Best regards,

Thierry

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

> -----Oorspronkelijk bericht-----
> Van: ggp...@googlegroups.com
> [mailto:ggp...@googlegroups.com] Namens Ben
> Verzonden: dinsdag 3 mei 2011 14:36
> Aan: ggplot2
> Onderwerp: Re: Plot 95%CI with lines, not shading ()

> > > + =

Ben

unread,
May 3, 2011, 9:34:23 AM5/3/11
to ggplot2
Thierry

I love your solution!!!

I did a final little tweak to get the chart lined up well on the x-
axis:
+ coord_cartesian(xlim = range(dd$x)+c(-0.5,0.5))

only issue here is that the c(-0.5,0.5) is set manually each time. i
will have a think how to do this in code so that i can be *even
lazier* :-)

i will post here if/when i figure it out.

thanks a 10^6
ben


On May 3, 9:02 am, "ONKELINX, Thierry" <Thierry.ONKEL...@inbo.be>
wrote:
> Here is another solution.
>
> x <- 1:10
> dd <- data.frame(x = x, y = 0.8 + 0.5 * x + rnorm(10, s = 1.5))
>
> ggplot(dd, aes(x = x, y = y)) + geom_point() + stat_smooth(method = "lm", se = FALSE) + stat_smooth(method = "lm", colour = "red", geom = "ribbon", fill = NA)
>
> #some tweaking to get rid to the vertical lines of the ribbon. The trick: expand the range with scale() then zoom in with coord()
> ggplot(dd, aes(x = x, y = y)) + geom_point() + stat_smooth(method = "lm", se = FALSE) + stat_smooth(method = "lm", colour = "red", geom = "ribbon", fill = NA, fullrange = TRUE) + scale_x_continuous(limits = diff(range(dd$x)) * c(-0.2, 0.2) + range(dd$x)) + coord_cartesian(xlim = range(pretty(dd$x)))
>
> Best regards,
>
> Thierry
>
> ----------------------------------------------------------------------------
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek
> team Biometrie & Kwaliteitszorg
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
>
> Research Institute for Nature and Forest
> team Biometrics & Quality Assurance
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
>
> tel. + 32 54/436 185
> Thierry.Onkel...@inbo.bewww.inbo.be

Ben

unread,
May 3, 2011, 11:19:23 AM5/3/11
to ggplot2
So here's my lazy solution:

xrange<- function(x, margin){
r<-range(x, na.rm=TRUE)
d<-diff(r)
m<-c(d*-margin, d*margin)
return (r+m)
}

where x is the x-data to be plotted, and margin is a decimal of how
much 'margin' we want around the x-data so that we dont get points
lying on the boundaries of the plot area. eg 0.05 is a 5% margin above
and below

then we modify Thierry's command:

ggplot(dd, aes(x = x, y = y)) + geom_point() + stat_smooth(method =
"lm", se = FALSE) + stat_smooth(method = "lm", colour = "red", geom =
"ribbon", fill = NA, fullrange = TRUE) + scale_x_continuous(limits =
xrange(x, 0.2) + coord_cartesian(xlim = xrange(x,0.05))

hey presto exactly what i need
just posting it here so that someone else can use benefit

On May 3, 9:02 am, "ONKELINX, Thierry" <Thierry.ONKEL...@inbo.be>
wrote:
> Here is another solution.
>
> x <- 1:10
> dd <- data.frame(x = x, y = 0.8 + 0.5 * x + rnorm(10, s = 1.5))
>
> ggplot(dd, aes(x = x, y = y)) + geom_point() + stat_smooth(method = "lm", se = FALSE) + stat_smooth(method = "lm", colour = "red", geom = "ribbon", fill = NA)
>
> #some tweaking to get rid to the vertical lines of the ribbon. The trick: expand the range with scale() then zoom in with coord()
> ggplot(dd, aes(x = x, y = y)) + geom_point() + stat_smooth(method = "lm", se = FALSE) + stat_smooth(method = "lm", colour = "red", geom = "ribbon", fill = NA, fullrange = TRUE) + scale_x_continuous(limits = diff(range(dd$x)) * c(-0.2, 0.2) + range(dd$x)) + coord_cartesian(xlim = range(pretty(dd$x)))
>
> Best regards,
>
> Thierry
>
> ----------------------------------------------------------------------------
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek
> team Biometrie & Kwaliteitszorg
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
>
> Research Institute for Nature and Forest
> team Biometrics & Quality Assurance
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
>
> tel. + 32 54/436 185
> Thierry.Onkel...@inbo.bewww.inbo.be
Reply all
Reply to author
Forward
0 new messages