I'm plotting a time-series where I map the color by a factor variable. The problem I have is that the different factor levels are located in discrete time windows throughout the data so for a given factor, the end of one window is being connected by a line to the beginning of another window. This line cuts through a different factor that is being plotted between the two windows. I've changed the geom_line()
to geom_point()
which is okay, but I'd prefer to have the lines. Here's code to create a sample data frame.
#Create dataframe
df <- data.frame(cbind(
t= c(1361347202,1361347212,1361347222,1361347232,1361347242,1361347252,1361347262),
y = runif(7,1,5),
l =c(1,1,1,2,2,1,1)))
df$l = as.factor(df$l)
And here's the plot command,
ggplot(df, aes(x = t, y=y, colour = factor(l)))+geom_line()
I'd like the reddish line to stop at the 3rd point and then start again at the 6th point. Also, I don't think it matters but the x-values are actually POSIX variables - I've just converted them to numeric values for this question. Thanks