I want to draw geom_line
with mixed colors used for each line. For example, the lines are drawn in red when the y
value is over 0.5 and in blue otherwise. The following is a nasty sketch:
Clearly, we can draw different line segments separately (2 blue segments and 1 red segments). But I still wonder whether we can achieve this in a more convenient way.
Note, I have tried to add a third variable to indicate the color, and supplied that variable to aes()
. However, it ended up with something like the figure below,
where the segments of the same color is connected.
CodePudding user response:
Specify a single group
so that a single line will be drawn
d = transform(data.frame(x = 1:10, y = c(6:10, 10:6)), col = y > 7)
graphics.off()
ggplot(d, aes(x, y, color = col, group = 1))
geom_line(size = 2)