0

I would like to plot two lines and then color segments of a single line with different colors. Here is a simple code:

temp <- data.frame(x = c(2, 4, 5, 5, 7, 6, 7, 8, 10, 9), y = c(3, 8, 4, 8, 8, 4, 9, 12, 5, 1), line = c('a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b'), color = c('c', 'c', 'd', 'd', 'd', 'c', 'c', 'c', 'd', 'd')) ggplot() + geom_line(aes(x, y, linetype = line, color = ???), data = temp) 

I want to color segments of each line using the color factor. Any help? Thank you in advance!

4
  • just use the column name color in place of ??? Commented Aug 25, 2017 at 3:27
  • Thank you for your reply. I have updated the question to make it more clear. Commented Aug 25, 2017 at 3:30
  • 1
    Each line in your sample data comprises 3 segments between 4 points. With 2 colours, how do you intend to split the colours among the segments? Commented Aug 25, 2017 at 3:42
  • 1
    Okay... after edits your lines now each have 4 segments between 5 points. Still not clear how you want the transition segments colored. Run ggplot(temp, aes(x, y)) + geom_line(aes(linetype = line)) + geom_point(aes(color = color)), we could make the lines between the red points red and the lines between the blue points blue. What color do you want the lines between the red and blue points?? Commented Aug 25, 2017 at 4:42

1 Answer 1

4

Does this graph solve you problem?

ggplot() + geom_line(aes(x, y, group = line, color = color), data = temp)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.