0
我想改變線段的顏色是否大於它之前的點。我在這裏做錯了什麼?顏色ggplot線的基礎上增加或減少
例子:
from [1,4] to [2,5] would be green because the y value is increasing.
from [1,4] to [2,1] would be red because the y value is decreasing.
我的代碼:
set.seed(42)
df<-NULL
df$x<-rnorm(10,0,10)
df$y<-rnorm(10,0,10)
df$colors<-cbind(lapply(1:length(df$x),function(i){
ifelse(df$x[i]>df$x[i-1],"#CC6666","#9999CC")
}))
df<-data.frame(df)
ggplot()+
geom_line(aes(x=df$x,y=df$y),size=1.5,colour=df$color)+
scale_color_manual(values=df$color)
這很好,但即時通訊組的困惑。如果值較大,您可以安排變量,然後進行變異以創建新列「1」。如果值較小,則只是自動分配一個不同的組? – Rilcon42
該組只是在那裏,因爲默認情況下ggplot與嘗試根據美學(如果定義)組合,所以我們只使用「1」作爲粘合劑,以確保它們保持在同一行。這個安排就是這樣,我們正在做相對於x軸順序的適當的「i + 1」comparrisons – Nate