I have written a small function that plots multiple glm's as forest plots with ggplot. Each model shares the same predictors, but have different dependent variables.
The function requires at least all glm-objects as parameter. Then a data frame is created which is used for the ggplot-procedure.
In my example, I have following data frame with the predictors "sex", "age" (alter3gr), socio-economic status ("ses") and experience ("f0103"). After "transforming" the glm's into a data frame, the results look like this example:
OR lower upper p pa shape grp xpos
sex1 1.3253832 1.0735041 1.6372096 "1.33 **" s 3 1 7
alter3gr1 1.0544569 0.8078543 1.3747014 "1.05" ns 1 1 6
alter3gr2 0.7042160 0.5372706 0.9212466 "0.7 *" s 2 1 5
ses3_neu21 1.3270242 1.0274121 1.7129088 "1.33 *" s 2 1 4
ses3_neu22 2.0043975 1.4394807 2.8009534 "2 ***" s 4 1 3
f01031 1.5953467 1.2783964 1.9944690 "1.6 ***" s 4 1 2
f01032 2.3780514 1.7175161 3.3307287 "2.38 ***" s 4 1 1
sex11 0.9841822 0.7684196 1.2605188 "0.98" ns 1 2 7
alter3gr11 1.1778530 0.8731799 1.5964175 "1.18" ns 1 2 6
alter3gr21 0.7633293 0.5513314 1.0588159 "0.76" ns 1 2 5
ses3_neu211 0.9536030 0.7048865 1.3010905 "0.95" ns 1 2 4
ses3_neu221 1.1891460 0.8171171 1.7327086 "1.19" ns 1 2 3
f010311 1.4651668 1.1290179 1.9002631 "1.47 **" s 3 2 2
f010321 1.7943022 1.2683576 2.5200254 "1.79 ***" s 4 2 1
sex12 1.1614532 0.9089319 1.4852303 "1.16" ns 1 3 7
alter3gr12 1.1143240 0.8228899 1.5159289 "1.11" ns 1 3 6
alter3gr22 1.0179194 0.7411147 1.4032116 "1.02" ns 1 3 5
ses3_neu212 1.2271544 0.9002163 1.6913440 "1.23" ns 1 3 4
ses3_neu222 1.6178685 1.1085687 2.3713724 "1.62 *" s 2 3 3
f010312 1.5175505 1.1722055 1.9637617 "1.52 **" s 3 3 2
f010322 2.0459773 1.4624682 2.8472016 "2.05 ***" s 4 3 1
sex13 0.6712958 0.4647907 0.9638335 "0.67 *" s 2 4 7
alter3gr13 1.2343442 0.7809696 1.9911347 "1.23" ns 1 4 6
alter3gr23 1.1335450 0.7068902 1.8517144 "1.13" ns 1 4 5
ses3_neu213 1.1521867 0.7230082 1.9049441 "1.15" ns 1 4 4
ses3_neu223 1.8294885 1.0694548 3.1988430 "1.83 *" s 2 4 3
f010313 1.2280278 0.8363800 1.7952537 "1.23" ns 1 4 2
f010323 1.8262125 1.1282033 2.8955135 "1.83 *" s 2 4 1
'data.frame': 42 obs. of 8 variables:
$ OR : num 1.325 1.054 0.704 1.327 2.004 ...
$ lower: num 1.074 0.808 0.537 1.027 1.439 ...
$ upper: num 1.637 1.375 0.921 1.713 2.801 ...
$ p : Factor w/ 40 levels "0.7 *","0.89",..: 5 3 1 4 7 6 8 12 13 10 ...
$ pa : Factor w/ 2 levels "ns","s": 2 1 2 2 2 2 2 1 1 1 ...
$ shape: chr "3" "1" "2" "2" ...
$ grp : Factor w/ 6 levels "1","2","3","4",..: 1 1 1 1 1 1 1 2 2 2 ...
$ xpos : Factor w/ 8 levels "1","2","3","4",..: 7 6 5 4 3 2 1 7 6 5 ...
The different models are identified by the "grp" column, the predictors by the "xpos" column.
This is how I draw the ggplot:
plotHeader <- ggplot(finalodds, aes(y=OR, x=xpos, alpha=pa, colour=grp))+
geom_point(position=position_dodge(-modelPlotSpace)) +
geom_errorbar(aes(ymin=lower, ymax=upper), position=position_dodge(-modelPlotSpace)) +
geom_text(aes(label=p, y=upper), position=position_dodge(width=-modelPlotSpace), hjust=-0.1) +
scale_x_discrete(labels=axisLabels.y) +
scale_y_log10(limits=c(lower_lim, upper_lim), breaks=ticks, labels=ticks) +
coord_flip()
However, in the plot, the order of the models change for almost every x-position (predictor). Does anybody know why? I'd like to have the same order of my OR-values for each x-position...
In case you like to reproduce any examples, you can download the R-script sjPlotOddsMultiple.R here. In the script header is an example. If you run that example with sjp.glmm(fitOR1, fitOR2, fitOR3)
everything looks fine. However, if you change the order of parameters to sjp.glmm(fitOR1, fitOR3, fitOR2)
, the problem occurs.
Thanks in advance Daniel
sju.wordwrap
is not defined. – tonytonov Jan 22 '14 at 8:15plotHeader <-
call of unnecessary details, which refer to undefined data. This would be much easier than sourcing your script, which is quite huge. – tonytonov Jan 22 '14 at 8:23scales
package, you may want to addrequire(scales)
to the script since that's not obvious. – tonytonov Jan 22 '14 at 8:51plotHeader <- ggplot(finalodds, aes(y=OR, x=xpos, colour=grp, alpha=pa))
(order within aes) and the order will be preserved (no idea why though). If that fixes your issue, I'll post this as an answer. – tonytonov Jan 22 '14 at 9:26