我很确定我在某个地方看到过这个问题的解决方案,但是由于我一直找不到它,所以这是我的问题。
我有一些由多个变量识别的时间序列数据,我希望能够使用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进行着色。
ggplot(df, aes(as.Date(date), temp, color = id, linetype = location)) + geom_path()
?或者ggplot(df, aes(as.Date(date), temp, color = id:location)) + geom_line()
,但是后者更加令人困惑。 - alistaire