Remi.b 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:

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

enter image description here

Now I would like to consider my
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
with ggplot2? And more globally how can I directly tell ggplot to consider the object
in order to create representation of the model?

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


enter image description here

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", = function(x){return(c(y=median(x), ymin=median(x), ymax=median(x)))})

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download