用ggplot2绘制lme4的结果 [英] Plotting results of lme4 with ggplot2

查看:202
本文介绍了用ggplot2绘制lme4的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用lme4创建线性混合效果模型

I used lme4 for a linear mixed-effects model

lme.m1 <- lmer(I1 ~ P1 + Period * Actor + (1 | Actor), data=Q)

有8个Actor,我有三个周期。
现在我想绘制(使用ggplot2)8个不同方面的不同线条(这似乎并不困难)。但是,我不知道如何为同一个面上的每个演员绘制三条不同的曲线(因为三个不同的时期)。我使用了 http://www.sagepub.com/long/chapters中的以下代码/rcode/82689_10rc.txt

There are 8 Actors and I have three Periods. Now I would like to plot (using ggplot2) the different lines in 8 different facets (which does not seem to be difficult). However, I do not know how to draw three different lines (because of the three different periods) for each actor in the same facet. I used the following code from http://www.sagepub.com/long/chapters/rcode/82689_10rc.txt:

plot.m1 <- data.frame(lme.m1@frame, fitted.re = fitted(lme.m1))
head(plot.m1)
fixed.m1 <- data.frame(fixef(lme.m1))
fixed.m1

g1 <-  ggplot(plot.m1, aes(x = P1, y = I1)) + geom_point()
## Facet.
g2 <- g1 + facet_wrap(~ Actor, nrow = 2)
## Individual fitted curve.
g3 <- g2 + geom_line(aes(y = fitted.re), linetype = 2) + scale_x_continuous(breaks = 0:3)
## Group fitted curve.
g4 <- g3 + geom_abline(intercept = fixed.m1[1,1], slope = fixed.m1[2,1]) + theme_bw()
print(g4)

如何在三个时间段内绘制三条不同的线?

How can I draw three different lines for the three periods?

以下是数据:

    Date Period Actor Group    P1    I1
49   1Q1     P1    Ar     D  0.55  1.00
50   1Q2     P1    Ar     D  0.28  0.68
51   1Q3     P2    Ar     D -0.11  0.67
52   1Q4     P2    Ar     D  0.56  0.00
53   2Q1     P2    Ar     D  0.68  0.83
54   2Q2     P2    Ar     D  0.20  0.73
55   2Q3     P2    Ar     D  0.36  0.50
56   2Q4     P2    Ar     D  0.11  0.23
57   3Q1     P2    Ar     D  0.38  0.67
58   3Q2     P3    Ar     D  0.54  0.54
59   3Q3     P3    Ar     D  0.41  0.56
60   3Q4     P3    Ar     D  0.39  0.49
61   4Q1     P3    Ar     D  0.42  0.62
62   4Q2     P3    Ar     D  0.37  0.37
63   4Q3     P3    Ar     D  0.37  0.70
64   4Q4     P3    Ar     D  0.46  0.67
116  1Q1     P1    Bu     M  0.49  0.61
117  1Q2     P1    Bu     M  0.33  0.57
118  1Q3     P2    Bu     M  0.24  0.50
119  1Q4     P2    Bu     M  0.23  0.41
120  2Q1     P2    Bu     M  0.42  0.61
121  2Q2     P2    Bu     M  0.28  0.51
122  2Q3     P2    Bu     M  0.34  0.70
123  2Q4     P2    Bu     M  0.20  0.54
124  3Q1     P2    Bu     M  0.30  0.55
125  3Q2     P3    Bu     M  0.39  0.59
126  3Q3     P3    Bu     M  0.30  0.44
127  3Q4     P3    Bu     M  0.35  0.62
128  4Q1     P3    Bu     M  0.22  0.44    
129  4Q2     P3    Bu     M  0.37  0.59
130  4Q3     P3    Bu     M  0.45  0.63
131  4Q4     P3    Bu     M  0.36  0.54
183  1Q1     P1    Ch     H  0.48  0.50
184  1Q2     P1    Ch     H  0.51  0.70
185  1Q3     P2    Ch     H  0.29  0.28
186  1Q4     P2    Ch     H  0.18  0.23
187  2Q1     P2    Ch     H  0.33  0.50
188  2Q2     P2    Ch     H  0.43  0.45
189  2Q3     P2    Ch     H  0.29  0.39
190  2Q4     P2    Ch     H  0.30  0.29
191  3Q1     P2    Ch     H  0.23  0.17
192  3Q2     P3    Ch     H  0.29  0.37
193  3Q3     P3    Ch     H  0.17  0.52
194  3Q4     P3    Ch     H  0.28  0.57
195  4Q1     P3    Ch     H  0.18  0.52
196  4Q2     P3    Ch     H  0.29  0.48
197  4Q3     P3    Ch     H  0.24  0.29
198  4Q4     P3    Ch     H  0.26  0.32
250  1Q1     P1    Po     D  0.41  0.46
251  1Q2     P1    Po     D  0.42  0.61
252  1Q3     P2    Po     D  0.40  0.58
253  1Q4     P2    Po     D  0.39  0.51
254  2Q1     P2    Po     D  0.42  0.57
255  2Q2     P2    Po     D  0.41  0.60
256  2Q3     P2    Po     D  0.31  0.44
257  2Q4     P2    Po     D  0.28  0.49
258  3Q1     P2    Po     D  0.32  0.50
259  3Q2     P3    Po     D  0.36  0.52
260  3Q3     P3    Po     D  0.35  0.55
261  3Q4     P3    Po     D  0.43  0.58
262  4Q1     P3    Po     D  0.40  0.54
263  4Q2     P3    Po     D  0.37  0.54
264  4Q3     P3    Po     D  0.29  0.52
265  4Q4     P3    Po     D  0.43  0.54
317  1Q1     P1    Ri     M  0.65 -0.33
318  1Q2     P1    Ri     M  0.51  0.83
319  1Q3     P2    Ri     M  0.45  0.73
320  1Q4     P2    Ri     M  0.34  0.54
321  2Q1     P2    Ri     M  0.29  0.26
322  2Q2     P2    Ri     M  0.39  0.47
323  2Q3     P2    Ri     M  0.16  0.24
324  2Q4     P2    Ri     M  0.27  0.29
325  3Q1     P2    Ri     M  0.28  0.52
326  3Q2     P3    Ri     M  0.45  0.67
327  3Q3     P3    Ri     M  0.30  0.39
328  3Q4     P3    Ri     M  0.18  0.48
329  4Q1     P3    Ri     M  0.30  0.46
330  4Q2     P3    Ri     M  0.36  0.56
331  4Q3     P3    Ri     M  0.31  0.56
332  4Q4     P3    Ri     M -0.18  0.33
384  1Q1     P1    Ru     H  0.40  0.59
385  1Q2     P1    Ru     H  0.34  0.47
386  1Q3     P2    Ru     H  0.33  0.38
387  1Q4     P2    Ru     H  0.10  0.29
388  2Q1     P2    Ru     H  0.25  0.41
389  2Q2     P2    Ru     H  0.34  0.45
390  2Q3     P2    Ru     H  0.23  0.47
391  2Q4     P2    Ru     H  0.28  0.46
392  3Q1     P2    Ru     H  0.20  0.40
393  3Q2     P3    Ru     H  0.29  0.41
394  3Q3     P3    Ru     H  0.27  0.46
395  3Q4     P3    Ru     H  0.29  0.48
396  4Q1     P3    Ru     H  0.26  0.48
397  4Q2     P3    Ru     H  0.32  0.53
398  4Q3     P3    Ru     H  0.34  0.47
399  4Q4     P3    Ru     H  0.33  0.62
451  1Q1     P1    Te     H  0.11  0.14
452  1Q2     P1    Te     H  0.56  0.71
453  1Q3     P2    Te     H  0.00  1.00
454  1Q4     P2    Te     H    NA    NA
455  2Q1     P2    Te     H  0.08 -0.33
456  2Q2     P2    Te     H  0.20  0.22
457  2Q3     P2    Te     H    NA    NA
458  2Q4     P2    Te     H  0.16  0.16
459  3Q1     P2    Te     H -0.07 -0.09
460  3Q2     P3    Te     H  0.33  0.10
461  3Q3     P3    Te     H    NA    NA
462  3Q4     P3    Te     H    NA    NA
463  4Q1     P3    Te     H  0.09  0.04
464  4Q2     P3    Te     H  0.43  0.30
465  4Q3     P3    Te     H  0.69  0.57
466  4Q4     P3    Te     H    NA    NA
518  1Q1     P1    Wo     H    NA    NA
519  1Q2     P1    Wo     H  0.52  0.80
520  1Q3     P2    Wo     H  0.28  0.29
521  1Q4     P2    Wo     H  0.12  0.35
522  2Q1     P2    Wo     H  0.18  0.13
523  2Q2     P2    Wo     H  0.28  0.37
524  2Q3     P2    Wo     H  0.30  0.39
525  2Q4     P2    Wo     H  0.25  0.36
526  3Q1     P2    Wo     H  0.18  0.34
527  3Q2     P3    Wo     H  0.27  0.32
528  3Q3     P3    Wo     H  0.23  0.45
529  3Q4     P3    Wo     H  0.02  0.26
530  4Q1     P3    Wo     H  0.00  0.41
531  4Q2     P3    Wo     H  0.13  0.28
532  4Q3     P3    Wo     H  0.00  0.30
533  4Q4     P3    Wo     H    NA    NA


推荐答案

看起来您需要为 geom_line 调用添加一个 color geom geom >

It looks like you need to add a color geom to the geom_line call:

g3 <- g2 + geom_line(aes(y=fitted.re, color=Period), linetype=2) + scale_x_continuous(breaks=0:3)

这篇关于用ggplot2绘制lme4的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆