Plot line in ggplot2 that only connects consecutive data

ggplot2, r

Solution

You have to modify the `group` aesthetic for `geom_path`.

ind <- as.numeric(df$l[-1]) - as.numeric(df$l[-nrow(df)]) != 0
splitAt <- function(x, pos) split(x, cumsum(seq_along(x) %in% (pos+1)))
l1 <- splitAt(as.numeric(df$l), which(ind))
names(l1) <- 1:length(l1)
l2 <- lapply(seq_along(l1), 
             function(y, n, i) {
                                 as.numeric(rep(n[[i]], length(y[[i]]))) 
                               }, y=l1, n=names(l1))
ggplot(df, aes(x = t, y=y, colour = l)) + 
  geom_point() +
  geom_path(aes(group=unlist(l2)))

Here's a brief expalnation. First, we should find grouping indices to use them as `group` aes. I assume that a group consists of several consecutive red or blue points. So, `ind` indicates where line breaks should appear. Then, we should build a grouping variable that looks like (for your example) `c(1, 1, 1, 2, 2, 3, 3)`, which would show what points are connected to each other. I do this in two steps: first split the variable by `ind` and store this in `l1`, then simply replace values in `l1` so that `i`th node in the list contains only values, equal to `i`. The result is stored in `l2` and looks like this:

[[1]]
[2] 1 1 1

[[2]]
[3] 2 2

[[3]]
[4] 3 3

Turn this into a vector by `unlist`ing it and we're done. The difference between my answer and the one provided by @AndreSilva is how we treat the transition from one colour to another. My answer looks more difficult because I have to specify groups in an accurate fashion, and that requires some intermediate steps. Here's his plot for the same data:

Problem

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

Original source

Related problems