# Comparing multiple (g)lm in one graph #rstats

**Strenge Jacke! » R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

It’s been a while since a user of my plotting-functions asked whether it would be possible to compare multiple (generalized) linear models in one graph (see comment). While it is already possible to compare multiple models as table output, I now managed to build a function that plots several (g)lm-objects in a single ggplot-graph.

The following examples are based on a development snapshot of my sjPlot package. You can download the script of the `sjp.glmm`

function here (the latest release of my package probably has to be installed to run the script due to dependencies on other help-functions that are not included in the script). *Please note that this script will not be updated! It will be included in the next update of my package!*

Once you’ve compiled the script, you can run one of the examples provided in the function’s documentation:

# prepare dummy variables for binary logistic regression y1 <- ifelse(swiss$Fertility<median(swiss$Fertility), 0, 1) y2 <- ifelse(swiss$Infant.Mortality<median(swiss$Infant.Mortality), 0, 1) y3 <- ifelse(swiss$Agriculture<median(swiss$Agriculture), 0, 1) # Now fit the models. Note that all models share the same predictors # and only differ in their dependent variable (y1, y2 and y3) fitOR1 <- glm(y1 ~ swiss$Education+swiss$Examination+swiss$Catholic, family=binomial(link="logit")) fitOR2 <- glm(y2 ~ swiss$Education+swiss$Examination+swiss$Catholic, family=binomial(link="logit")) fitOR3 <- glm(y3 ~ swiss$Education+swiss$Examination+swiss$Catholic, family=binomial(link="logit")) # plot multiple models sjp.glmm(fitOR1, fitOR2, fitOR3)

Thanks to the help of a stackoverflow user, I now know that the order of aes-parameters matters in case you have dodged positioning of geoms on a discrete scale. An example: I use following code in my function `ggplot(finalodds, aes(y=OR, x=xpos, colour=grp, alpha=pa))`

to apply different colours to each model and setting an alpha-level for geoms depending on the p-level. If the alpha-aes would appear before the colour-aes, the order of lines representing a model may be different for different x-values (see stackoverflow example).

Another more appealing example (not reproducable, since it relies on data from a current research project):

And finally an example where p-levels are represented by different shapes and non-significant odds have a lower alpha-level:

This function and an equivalent for linear models will be included in the next update of my sjPlot package.

Tagged: ggplot, R, rstats

**leave a comment**for the author, please follow the link and comment on their blog:

**Strenge Jacke! » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.