我需要用一条线创建ggplot时间序列图,其中线的颜色取决于二进制因子变量。当我按因数着色时,代码以某种方式生成了两行:
date = seq(as.Date("2018-01-01"), as.Date("2018-01-20"), by = 1)
data = rnorm(20)
factor = as.factor(sample(0:1,20,replace=T))
df = data.frame(date = date, data = data, factor = factor)
p=ggplot(df, aes(date))
p=p + geom_line(aes(y = data, colour = factor))
print(p)
如何解决此问题?