Remi.b - 5 months ago 19

R Question

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`

`x`

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

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

`m2`

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

`visreg`

`visreg(m2)`

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

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

Answer Source

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)) +
geom_boxplot(fill = "gray90") +
geom_jitter() +
theme_bw() +
stat_summary(geom = "crossbar", width = 0.65, fatten = 0, color = "blue",
fun.data = function(x){return(c(y=median(x), ymin=median(x), ymax=median(x)))})
```