R - ggplot2中奇怪的geoms顺序

时间:2014-01-21 10:47:40

标签: r ggplot2

我写了一个小函数,用ggplot绘制多个glm作为森林图。每个模型共享相同的预测变量,但具有不同的因变量。

该函数至少需要所有glm-objects作为参数。然后创建一个用于ggplot-procedure的数据框。

在我的例子中,我有跟随数据框的预测器“性别”,“年龄”(alter3gr),社会经济状况(“ses”)和经验(“f0103”)。在将glm“转换”为数据帧之后,结果如下所示:

                   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 ...

不同的模型由“grp”列标识,预测变量由“xpos”列标识。

这是我绘制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()

但是,在图中,模型的顺序几乎每个x位置(预测变量)都会发生变化。有人知道为什么吗?我想为每个x位置设置相同的OR值...

Multiple glm's with changing order of ORs

如果您想重现任何示例,可以在此处下载R脚本sjPlotOddsMultiple.R在脚本标题中是一个示例。如果您使用sjp.glmm(fitOR1, fitOR2, fitOR3)运行该示例,一切看起来都很好。但是,如果将参数的顺序更改为sjp.glmm(fitOR1, fitOR3, fitOR2),则会出现问题。

提前致谢 丹尼尔

1 个答案:

答案 0 :(得分:1)

以下是最小例子(finalodds如上所述):

ggplot(finalodds, aes(y=OR, x=xpos, alpha=pa, colour=as.factor(grp))) +
  geom_errorbar(aes(ymin=lower, ymax=upper), position=position_dodge(-0.4), width=0, size=0.8) +
  scale_alpha_manual(values=c(1,1), guide="none")

enter image description here

通过更改alphacolour aes:

来恢复正确的订单
ggplot(finalodds, aes(y=OR, x=xpos, colour=as.factor(grp)), alpha=pa) +
  geom_errorbar(aes(ymin=lower, ymax=upper), position=position_dodge(-0.4), width=0, size=0.8) +
  scale_alpha_manual(values=c(1,1.0), guide="none")

enter image description here