I am trying to plot the results of a random coefficient growth model I ran with nlme in R. sjPlot is great for plotting the results, but not so great for customization. Accordingly, I would like to feed my feed the results of my sjPlot to ggplot2. I have managed to get something that is acceptable, but ideally I would like to be able to customize the line type, line color, and line size in both the plot and the legend. In addition, the line types (solid, dashed, and dotted) look to be overlayed on top of a solid line. Is there a better way to do this?
The growth model looks something like the following:
mod<-lme(Outcome ~ 1 + Covariate1 + Covaraite2 +
Linear + I(Linear^2) +
X1 + X2 + X3 + X4 +
(Linear * X1) + (Linear * X2) + (Linear * X3) + (Linear * X4),
random=~1|Event,
correlation=corAR1(form=~1|Event),
data=dat,method="ML",
control=lmeControl(opt="optim"))
summary(mod)
I then ran my sjPlot:
plot<-sjPlot::plot_model(mod,type="pred",
terms=c("Linear [all]","X1"),
axis.title=c("Time","Outcome"),
title="Time by X1",
ci.lvl=NA,
legend.title="")
I then fed the sjPlot to ggplot2 as follows:
plot +
geom_line(aes(linetype = group, color = group), size = 1.5) +
scale_color_manual(values = c("black", "grey", "brown"),
labels = c("Low X1", "Average X1", "High X1")) +
scale_linetype_manual(values = c("solid", "dashed", "dotted")) +
guides(color = guide_legend(override.aes = list(linetype = c("solid", "dashed", "dotted"))),
linetype = FALSE) +
theme(
legend.key = element_rect(fill = NA, color = NA)
)
The result looks like this:
enter image description here
I am curious if what I’ve tried is the best/cleanest way to plot my model and if there is a way to remove what looks like a solid line going through my line types. It feels very clunky.
a_roebuck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.