Shanshan Chen

Visualzing Marginal Effects after Regression Modeling wiht ggplot

library(ggplot2)
library(nlme)

Simpliest way but can be difficult to control style

library(sjPlot)
Model = lme(Y ~ X_Var1*Group + X_Var2, random = ~1|ID, data =Data)
plot_model(Model, terms=c("X_Var1","X_Var2","Group"),
           axis_title = c("X Name", "Y Name"), 
           colors = c("color1", "color2", "color3"), line.size=1, 
           legend.title = "Repeat")

Using the ggeffects package, somewhat flexible

library(ggeffects)

fitted <- ggpredict(Model,terms = c("X_Var1","X_Var2","Group"))
plot(fittied, ci.style= "errorbar", colors= c("color1","color2","color3"))
          +labs(x= "X Name", y = "Y Name", color = "Repeat")

From the scratch using ggplot, most flexible

 newData <- expand.grid(Var1 = seq(min(Data$Var1),max(Data$Var1),1), ID = unique(Data$ID))
 fittedNew = predict(model_nlme, level=0, newdata = newData)
 ggplot(Data, aes(x=Var1, y=Outcome, colour=ID)) 
        +geom_point(size=3)
        +geom_line(data=Data,aes(y=fitted))
        +geom_line(data=newData, aes(x=Var1, y=fittedNew), size=2, colour="color"))
        +theme_bw(base_size=22) +xlab("Var1 Name ") + ylab("Outcome Name")