Draw lines from observation points to fitted regression line

35 views
Skip to first unread message

Manuel Spínola

unread,
Jun 24, 2012, 4:43:25 PM6/24/12
to ggplot2
Dear list members,

Is it posible to draw lines form the observation points to a fitted regression line?

c <- ggplot(mtcars, aes(qsec, wt)) 
c + stat_smooth(method = "lm") + geom_point()

Best,

Manuel

--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

Ben Bolker

unread,
Jun 24, 2012, 5:16:44 PM6/24/12
to Manuel Spínola, ggplot2
On 12-06-24 04:43 PM, Manuel Sp�nola wrote:
> Dear list members,
>
> Is it posible to draw lines form the observation points to a fitted
> regression line?
>
> c <- ggplot(mtcars, aes(qsec, wt))
> c + stat_smooth(method = "lm") + geom_point()
>
> Best,
>
> Manuel
>

I can't see how to do it without running lm() outside of the ggplot
call, but it's pretty easy:

library(ggplot2)
c <- ggplot(mtcars, aes(qsec, wt))
lm1 <- lm(wt~qsec,data=mtcars)
c1 <- c + stat_smooth(method = "lm") + geom_point() +
geom_segment(data=fortify(lm1),
aes(yend=.fitted,xend=qsec))


> --
> *Manuel Sp�nola, Ph.D.*
> Instituto Internacional en Conservaci�n y Manejo de Vida Silvestre
> Universidad Nacional
> Apartado 1350-3000
> Heredia
> COSTA RICA
> mspi...@una.ac.cr <mailto:mspi...@una.ac.cr>
> mspin...@gmail.com <mailto:mspin...@gmail.com>
> Tel�fono: (506) 2277-3598
> Fax: (506) 2237-7036
> Personal website: Lobito de r�o
> <https://sites.google.com/site/lobitoderio/>
> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
>
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2


Manuel Spínola

unread,
Jun 24, 2012, 6:14:58 PM6/24/12
to Ben Bolker, ggplot2
Thank you very much Ben.

That was what I was looking for.

Manuel

2012/6/24 Ben Bolker <bbo...@gmail.com>
On 12-06-24 04:43 PM, Manuel Spínola wrote:
> Dear list members,
>
> Is it posible to draw lines form the observation points to a fitted
> regression line?
>
> c <- ggplot(mtcars, aes(qsec, wt))
> c + stat_smooth(method = "lm") + geom_point()
>
> Best,
>
> Manuel
>

 I can't see how to do it without running lm() outside of the ggplot
call, but it's pretty easy:

library(ggplot2)
c <- ggplot(mtcars, aes(qsec, wt))
lm1 <- lm(wt~qsec,data=mtcars)
c1 <- c + stat_smooth(method = "lm") + geom_point() +
   geom_segment(data=fortify(lm1),
                aes(yend=.fitted,xend=qsec))


> --
> *Manuel Spínola, Ph.D.*
> Instituto Internacional en Conservación y Manejo de Vida Silvestre

> Universidad Nacional
> Apartado 1350-3000
> Heredia
> COSTA RICA
> Teléfono: (506) 2277-3598
> Fax: (506) 2237-7036
> Personal website: Lobito de río
> <https://sites.google.com/site/lobitoderio/>
> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
>
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2





--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS
Reply all
Reply to author
Forward
0 new messages