0

I'm trying to connect specific data points in ggplot, and geom_line is not working. I've tried:

ggplot(df, aes(x=x, y=y, color=V1)) +
  geom_point(data = df, aes(x, y)) +
  geom_line(aes(group = V1)) 

Here's my data:

V1    V2   
1a    4    
1b    6    
1c    5
1d    6
1e    6
1f    6    
2a    9    
2b    7    
2c    8    
2d    4
2e    6
2f    5
3a    4    
3b    8    
3c    6 
3d    5
3e    7
3f    6

What I'd like to do is create three separate lines. Each one connecting the V2 point 1c to point 1f (5 to 6), 2c to 2f (8 to 5), and 3c to 3f (6 to 6), but no lines connecting any of the a/d or b/e values in V1. Any suggestions?

1 Answer 1

1

You can filter out the groups you don't want in the geom_line layer, then group by the first character of V1

ggplot(df, aes(x = V1, y = V2, color = substr(V1, 1, 1))) +
  geom_point(data = df) +
  geom_line(data = df[substr(df$V1, 2, 2) %in% c('c', 'f'),],
            aes(group = substr(V1, 1, 1))) 

enter image description here

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.