我有一个如下所示的数据框:
head(length_test)
S.UNIT GENRE PATTERN
1 105 Conversation THAT_EXT
2 61 Conversation THAT_EXT
3 210 Conversation TO_EXT
4 196 Conversation TO_EXT
5 166 Academic Prose TO_EXT
6 152 Academic Prose TO_EXT
> str(length_test)
'data.frame': 7329 obs. of 3 variables:
$ S.UNIT : int 105 61 210 196 166 152 152 152 152 150 ...
$ GENRE : Factor w/ 5 levels "Academic Prose",..: 2 2 2 2 1 1 1 1 2 2 ...
$ PATTERN: Factor w/ 6 levels "THAT_EXT","THAT_EXT_NT",..: 1 1 5 5 5 5 5 5 5 5 ...
我想做的是制作一个这样的情节,但是使用ggplot2:
interaction.plot(GENRE, PATTERN, S.UNIT)
我的问题是,我不能只绘制手段,而是得到这样的东西:
ggplot(data = length_test,
aes(x = GENRE, y = S.UNIT, colour = PATTERN, group=PATTERN)) +
geom_line() +
stat_summary(fun.y=mean, geom="point")
我的问题是无法获得绘制的手段。很可能我的stat_summary错误,但我无法提出解决方案。有任何想法吗?
一个可重复的小例子:
structure(list(S.UNIT = c(42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L), GENRE = structure(c(2L, 2L, 1L, 3L, 3L,
2L, 2L, 5L, 2L, 3L, 1L, 1L, 5L, 5L, 1L, 4L, 5L, 5L, 1L, 5L, 2L,
5L, 5L, 1L, 4L, 3L, 5L, 5L, 1L, 1L, 3L, 2L, 5L, 1L, 2L, 5L, 5L,
1L, 3L, 3L, 1L, 3L, 1L, 2L, 3L, 4L, 3L, 3L, 1L, 3L, 5L, 5L, 5L,
5L, 5L, 5L, 4L, 1L, 1L, 1L, 1L, 4L, 5L, 1L, 5L, 1L, 1L, 2L, 4L,
1L, 1L, 4L, 1L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 5L, 5L, 5L, 5L, 5L, 4L, 5L,
5L), .Label = c("Academic Prose", "Conversation", "News", "Novels",
"Popular Science"), class = "factor"), PATTERN = structure(c(6L,
6L, 6L, 2L, 4L, 4L, 4L, 5L, 6L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L,
5L, 2L, 3L, 3L, 1L, 1L, 3L, 3L, 5L, 5L, 1L, 1L, 1L, 3L, 5L, 5L,
1L, 5L, 1L, 3L, 1L, 1L, 3L, 1L, 3L, 1L, 5L, 1L, 3L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L), .Label = c("THAT_EXT", "THAT_EXT_NT", "THAT_POST",
"THAT_POST_NT", "TO_EXT", "TO_POST"), class = "factor")), .Names = c("S.UNIT",
"GENRE", "PATTERN"), class = "data.frame", row.names = c(NA,
-102L))
答案 0 :(得分:10)
您可以使用stat_summary()
代替geom_line(
)将点与线连接起来。在这种情况下,geom_line()
连接所有点,而不仅仅是平均值。
ggplot(data = length_test,
aes(x = GENRE, y = S.UNIT, colour = PATTERN, group=PATTERN)) +
stat_summary(fun.y=mean, geom="point")+
stat_summary(fun.y=mean, geom="line")