问题
I used lme4 for a linear mixed-effects model
lme.m1 <- lmer(I1 ~ P1 + Period * Actor + (1 | Actor), data=Q)
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?
Here is the data:
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
回答1:
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)
来源:https://stackoverflow.com/questions/19932251/plotting-results-of-lme4-with-ggplot2