问题
The package visreg allows to plot directly a statistical model which I find very convenient to check if anything gone wrong and to check if we understand correctly what the estimates mean. I would love to combine the functionality of visreg with the incredible flexibility of ggplot. I'd like to be able to directly call the model in a ggplot code line. Is this feasible (eventually by directly modifying the visreg function)?
For example:
require(visreg)
require(ggplot2)
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)
m = lm(y~x, data=dt)
I'd like to be able to directly call the object m in a ggplot function in order to represent my statistical model m. With visreg, it simply is:
visreg(m)
The below code is NOT what I am looking for as it does not directly call the object m
ggplot(dt, aes(x,y)) +
geom_boxplot(aes(group=x), alpha=0.5)+geom_jitter()
回答1:
Just FYI, visreg now supports ggplot2 output:
visreg(m, gg=TRUE)
produces
来源:https://stackoverflow.com/questions/26029608/directly-plot-a-statistical-model-with-ggplots