我有一个数据表,每个名称有2个名称和3种类型。
dt = CJ(as.Date(c('2000-01-01','2000-01-02','2000-01-03','2000-01-04')), c('A','B'), c('t1','t2','t3'))
setnames(dt, c('date','name','type'))
dt[, type := paste0(name, type)]
dt[, value := rnorm(n = .N) + (name == 'A')]
date name type value
1: 2000-01-01 A At1 0.3872948
2: 2000-01-01 A At2 -0.6147802
3: 2000-01-01 A At3 2.2317864
4: 2000-01-01 B Bt1 0.4979870
5: 2000-01-01 B Bt2 -1.7946112
6: 2000-01-01 B Bt3 0.7247306
7: 2000-01-02 A At1 0.3148835
8: 2000-01-02 A At2 0.1124225
9: 2000-01-02 A At3 1.1758116
10: 2000-01-02 B Bt1 -0.7068185
11: 2000-01-02 B Bt2 -1.6968477
12: 2000-01-02 B Bt3 0.4575144
....
按类型绘制,但按名称绘制颜色,因此我们得到3条红线和3条蓝线
ggplot(dt) + geom_line(aes(x = date, y = value, group = type, col = name))
现在我想添加两行代表每个名字的方法
mdt = dt[, .(value = mean(value)), by = 'name,date']
ggplot(dt) + geom_line(aes(x = date, y = value, group = type, col = name)) +
geom_line(data = mdt, aes(x = date, y = value, col = name), size = 1.5)
对于各行和平均线,这将使用完全相同的2种颜色。如何为平均线指定不同的颜色?