I was wondering if someone could help me with something. I have a mixed effects model fitted with lme4 and I have extracted the random effects to plot in a dotplot. I have followed the very helpful code pasted below and have got the plot shown. However, I would like to be able to tidy up the x-axis scales a bit to make it look a bit tidier. Ideally I want axis tick labels at -0.1, 0.1 and 0.3. I have tried modifying the function with scale_x_continous in various places but as I am not entirely sure how the whole code works together I don't quite know where this would go or if this would even work. I would be very grateful for any assitance, thank you.
## re = object of class ranef.mer
ggCaterpillar <- function(re, QQ=TRUE, likeDotplot=TRUE) {
require(ggplot2)
f <- function(x) {
pv <- attr(x, "postVar")
cols <- 1:(dim(pv)[1])
se <- unlist(lapply(cols, function(i) sqrt(pv[i, i, ])))
ord <- unlist(lapply(x, order)) + rep((0:(ncol(x) - 1)) * nrow(x), each=nrow(x))
pDf <- data.frame(y=unlist(x)[ord],
ci=1.96*se[ord],
nQQ=rep(qnorm(ppoints(nrow(x))), ncol(x)),
ID=factor(rep(rownames(x), ncol(x))[ord], levels=rownames(x)[ord]),
ind=gl(ncol(x), nrow(x), labels=names(x)))
if(QQ) { ## normal QQ-plot
p <- ggplot(pDf, aes(nQQ, y)) + scale_x_continous(limits=c(-0.6,0.7), breaks=seq(-0.6,0.7, by=0.1), labels=c("-0.6","-0.5","-0.4","0.3","0.2","0.1","0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7"))
p <- p + facet_wrap(~ ind, scales="free")
p <- p + xlab("Standard normal quantiles") + ylab("Random effect quantiles")
} else { ## caterpillar dotplot
p <- ggplot(pDf, aes(ID, y)) + coord_flip()
if(likeDotplot) { ## imitate dotplot() -> same scales for random effects
p <- p + facet_wrap(~ ind)
} else { ## different scales for random effects
p <- p + facet_grid(ind ~ ., scales="free_y")
}
p <- p + xlab("Levels") + ylab("Random effects")
}
p <- p + theme(legend.position="none")
p <- p + geom_hline(yintercept=0)
p <- p + geom_errorbar(aes(ymin=y-ci, ymax=y+ci), width=0, colour="black")
p <- p + geom_point(aes(size=1.2), colour="blue")
return(p)
#plotting the dotplot of my model
ggCaterpillar(ranef(model, condVar=TRUE), QQ=FALSE)