Hello,
I have some normal plot codes to run a graph where certain portions of a particular data series can be highlighted in different colours. I was wondering whether this could be done in ggplot2. This is what I tried:
b<-cumsum(rnorm(1000))
x<-sample(c(1,2), size=1000, replace=TRUE)
y<-sample(c(1,2), size=1000, replace=TRUE)
T<-length(b)
t<-1:(T)
par(mfrow=c(1,2))
plot(t, b, type="n", ax=FALSE, ylab="",xlab="",col=x, ylim=range(b))
title("x")
segments(t,b[t],t+1,b[t+1],col=y)
plot(t, b, type="n", ax=FALSE, ylab="",xlab="",col=x)
title("y")
legend("topleft", legend=c("low","high"), pch=2, col=1:2, merge=FALSE, title="regime", lty=1, bg="white")
segments(t,b[t],t+1,b[t+1],col=x)
library(ggplot2)
tmp <- data.frame(cbind(t,b,x,y))
ggplot(tmp, aes(t))+geom_line(aes(y=b,colour=factor(x)))+labs(x=NULL,y=NULL)
The problem is that I cannot hide those parts of the line that I do not want visible in ggplot, which I could to with the normal plot command.
Am I missing something very obvious?
Regards,
Vimal