我有一个包含3个因子(condition,measure和time)的数据库,并希望使用x轴,颜色/组和线型来绘制它们.
作为示例,我的数据如下所示:
DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8)
> DT
condition measure time score
1 control A pre-test 1
2 control A post-test 2
3 control B pre-test 3
4 control B post-test 4
5 experimental A pre-test 5
6 experimental A post-test 6
7 experimental B pre-test 7
8 experimental B post-test 8
Run Code Online (Sandbox Code Playgroud)
我的目标是绘制一个这样的图形:
我试过了:
ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
geom_line() +
geom_point()
Run Code Online (Sandbox Code Playgroud)
但它返回此错误:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
你想用
ggplot(DT, aes(time, score, group = interaction(measure, condition),
color = measure, linetype = condition)) +
geom_line() + geom_point()
Run Code Online (Sandbox Code Playgroud)
因为实际的分组不仅仅是由,measure而且也是由condition.measure单独分组时,我想它要求的是平行四边形而不是线条.
| 归档时间: |
|
| 查看次数: |
1197 次 |
| 最近记录: |