R中的着色图是多重因素

时间:2017-02-15 14:09:32

标签: r plot

我有以下df数据集:

  Subject Age                School TimeOfTest Score1 Score2
1       1  12 Aletta-Jacobs-College    Morning     78    180
2       2  11 Aletta-Jacobs-College  Afternoon     81    181
3       3  12 Aletta-Jacobs-College    Morning     79    177
4       4  13 Aletta-Jacobs-College  Afternoon     88    170
5       5  13    Marie-Curie-School    Morning     88    182
6       6  12    Marie-Curie-School  Afternoon     82    179

我想在Score1Score2上绘制SchoolTimeOfTest

现在,根据this其他问题的答案,我可以通过一个变量得到颜色:

plot(df$Score1, df$Score2, col=df$School)

Plot colored by school

但是,如果我尝试将这两个因素结合起来,我会得到一个空图。

我尝试了不同的解决方案,但都没有效果:

plot(df$Score1, df$Score2, col=c(df$School, df$TimeOfTest))

或者

colors <- levels(interaction(df$School, df$TimeOfTest))
plot(df$Score1, df$Score2, col=colors)

有什么想法吗?也许没有ggplot2?

1 个答案:

答案 0 :(得分:0)

正如评论中所指出的,以下工作:

colors <- interaction(df$School, df$TimeOfTest)
plot(df$Score1, df$Score2, col=colors)