When mapping colour to lines in ggplot2, e.g.:
x = data.frame(x = 1:6, y = c(0,0,10,10,0,0))
ggplot(x, aes(x, y, col=y)) + geom_line(size=5)
.. the lines colours are mapped to the first data point of each line segment. Is there any easy way to get ggplot to calculate the mean value of both points instead (ie. so the sloping lines are both scaled to the colour for 5)?


temp <- mutate(x, foo = (lead(y, default = 0) + y) / 2); ggplot(temp, aes(x = x, y = y, col = foo)) + geom_line(size=5)would do the same job. If you have more complicated situation, I think it is worth adding more description in your question. - jazzurro