方向

时间:2016-12-20 12:08:31

标签: r ggplot2

如何向geom_line()添加比例颜色以指示线段向上/向下 - 无需使用重塑,dcast预处理数据并创建另一个变量?

library(data.table)
library(ggplot2)

my.data <- data.table(id = c(1,1,2,2,3,3,4,4), 
                  loc = c("a","b","a","b","a","b","a","b"),
                  value = c(10.3,11.5,12.5,12.1,10.5,9.8,9.7,10.7))

p<-ggplot(my.data ,aes(x=loc,y=value,group=id)) + geom_line()

1 个答案:

答案 0 :(得分:5)

我会做

ggplot(my.data, aes(x=loc,y=value,group=id)) + 
  geom_line(aes(color=ave(value,id,FUN=diff)>=0))