我相当确定我在某处看到了这个问题的解决方案,但由于我无法找到它,这就是我的问题。
我有一些由多个变量标识的时间序列数据,我希望能够使用多个变量来绘制图表并区分颜色ggplot2
.
样本数据:
date <- c("2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC",
"2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC",
"2016-06-01 UTC", "2016-04-01 UTC")
temp <- c(80.24018, 85.88911, 104.23125, 85.13571, 91.21129, 104.88333, 97.81116,
107.40484, 121.03958, 87.91830)
id <- c("A","A","A","A","A","B","B","B","B","B")
location <- c("N","S","S","N","N","S","N","S","N","S")
df <- data.frame(date,temp,id,location)
我在绘图方面的尝试
library(ggplot2)
ggplot(df) +
geom_line(aes(x=date,y=temp,colour=factor(location), group=interaction(location,id)))
使用此代码仅按位置着色。我想根据位置和 ID 对线条进行着色。