5
Suppose the following data:
set.seed(1)
y<-rnorm(101)
x<-seq(from=0, to=100,by=1)
I want to make a Plot with a line that has different color for negative values.
To make a chart of points just command below:
plot(x,y,col=ifelse(y>0,"blue","red"))
However, switching to a line chart does not work.
plot(x,y,col=ifelse(y>0,"blue","red"),type="l")
If I try to do with the ggplot2
also not working. It assigns the line segment the color of the previous stitch.
library(ggplot2)
df<-data.frame(x,y)
df$cat<-y>0
ggplot(df,aes(x,y,color=cat)) + geom_path(aes(group=1))
How to make R correctly assign red color to negative values and blue color to positive values in line Plot?