How to pass an lm function in GGPLOT2

62 views
Skip to first unread message

Vikram Rawat

unread,
Sep 29, 2017, 6:23:57 AM9/29/17
to ggplot2
the abline geom takes a slope and intercept
geom_smooth creates it's own lm function

If I want to pass an LM function in a ggplot2 how would I do that

say in this example 

=======================================================================================
=======================================================================================
library(tidyverse)


model<- lm(mpg~hp,mtcars)

mtcars %>% 
    ggplot()+
    geom_point(aes(mpg,hp))
=======================================================================================
=======================================================================================

how to pass model to gggplot object

Roman Luštrik

unread,
Sep 29, 2017, 6:29:45 AM9/29/17
to Vikram Rawat, ggplot2
See examples in geom_smooth.


Cheers,
Roman

--
--
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+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Ista Zahn

unread,
Sep 29, 2017, 12:37:03 PM9/29/17
to Vikram Rawat, ggplot2
Please keep the list copied.

On Fri, Sep 29, 2017 at 10:10 AM, Vikram Rawat <vikram...@gmail.com> wrote:
> I tried but this does not work.

Right, I forgot that formula needs to be in terms of x and y. Ugh.

My point is if I want to try regression
> analysis. I will do so by building a model. Either lm or gam or speedlm but
> I will build my own model and compare them with other one.
>
> Now when I want to pass it to base plot I can simply do that with abline
> function butI can't do that straight away in ggplot2.
>
> What's the alternative method to pass the model directly. Without any
> changes.

I don't really understand the question. My guess is that you want something like

model<- lm(mpg~hp,mtcars)
mtcars %>%
ggplot(aes(hp, mpg))+
geom_point() +
geom_abline(intercept = coef(model)[1], slope = coef(model)[2])

or perhaps

mtcars %>%
ggplot(aes(hp, mpg))+
geom_point() +
geom_line(aes(y = fitted(model)))

>
> My question is can we do it?? If so then how?
>
> Please do answer it if you know...
>
> 29-09-2017 6:55 pm को "Vikram Rawat" <vikram...@gmail.com> ने लिखा:
>
>> Cool, thanks!
>>
>> 29-09-2017 5:59 pm को "Ista Zahn" <ista...@gmail.com> ने लिखा:
>>>
>>> One option is something like
>>>
>>> geom_smooth(method = lm, formula = formula(model))
>>>
>>> This does however re-fit the model.
>>> --
>>> --
>>> 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
>>>
>>> ---
>>> You received this message because you are subscribed to the Google Groups
>>> "ggplot2" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to ggplot2+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages