Remi.b - 9 months ago 29
R Question

# Statistical Model Representation with ggplot2

## I will ask my question with a study case and then I'll make my question more general.

Let's first import some libraries and create some data:

``````require(visreg)
require(ggplot2)
y = c(rnorm(40,10,1), rnorm(20,11,1), rnorm(5,12,1))
x=c(rep(1,40), rep(2,20), rep(3,5))
dt=data.frame(x=x, y=y)
``````

and run a linear regression of
`y`
on
`x`
and graph the data and the model with ggplot2

``````m1 = lm(y~x, data=dt)
ggplot(dt, aes(x,y)) + geom_point() + geom_smooth(formula = y~x, method="anova", data=dt)
``````

Now I would like to consider my
`x`
variable as a nominal variable. So I slightly change my data and run the following model.

``````y = c(rnorm(40,10,1), rnorm(20,11,1), rnorm(5,12,1))
x=factor(c(rep(1,40), rep(2,20), rep(3,5))) # this line has changed!
dt=data.frame(x=x, y=y)
m2 = lm(y~x, data=dt)
``````

How can I plot this model
`m2`
with ggplot2? And more globally how can I directly tell ggplot to consider the object
`m2`
in order to create representation of the model?

What I aim to do is the kind of things that can be done using the
`visreg`
package

``````visreg(m2)
``````

So, is there any visreg-like solution for ggplot? something like

``````ggplot(..,aes(..)) + super_geom_smooth(model = m2)
``````

This is not much different from @rnso's idea. `geom_jitter()` adds more flavour. I also change the colour of median bar. Hope this helps you!
``````ggplot(data = m2\$model, aes(x = x, y = y)) +