Code below plots random effects from a mixed effects model:
mtcarsSub <- mtcars[,c("wt", "drat", "cyl")]
library(lme4)
mtcarsME <- lmer(drat ~ (1|cyl) + wt, data=mtcarsSub)
mtcarsSub$fixed.effect <- predict(mtcarsME)
library(plyr)
l_ply(list(4, 6, 8), function(x) mtcarsSub[[ paste0("random.effect.cyl", x) ]] <<- mtcarsSub$fixed.effect + ranef(mtcarsME)$cyl[as.character(x),])
library(ggplot2)
ggplot(mtcarsSub, aes(wt, drat, color=factor(cyl))) +
geom_point() +
geom_line(aes(wt, fixed.effect), color="black", size=2) +
geom_line(aes(wt, random.effect.cyl4), size=2) +
geom_line(aes(wt, random.effect.cyl6), size=2) +
geom_line(aes(wt, random.effect.cyl8), size=2)

How can I programatically make each random effect line the same colour as the colours displayed for cyl? Therefore, the random effect line for level 4 of cyl should be red, level 6 of cyl should be green and level 8 of cyl should be blue. I dont want to specify color="red" etc in geom_line().