2
votes

I want to create a graph of geom_line() coloured by a variable (Var1) then plot geom_point() with shapes according to a different variable (Var2) with the same colours as geom_line().

After reading a lot about this but not being able to find anything that I could interpret as being the same issue I have attempted the following:

 ggplot(data, aes(X, Y)) +
 geom_line(aes(color = Var1)) +
 geom_point(data = subset(data, Var2 != 0), aes(shape = Var2, colour = Var1), size = 3) + 
 scale_color_manual(values=c("#7CAE00", "#00BFC4", "#000000", "#C77CFF")) +
 scale_x_continuous(breaks=seq(0,30,5)) +
 theme_bw()

enter image description here

Which results in the above. The issue with this graph is that the second legend has both IDs are circles when one is a circle and one is a triangle. I would ideally like it to just be a coloured line with no shapes at all.

I've also tried this:

 ggplot(data, aes(X, Y)) +
 geom_line(aes(color = Var1)) +
 geom_point(data = subset(data, Var2 != 0), aes(shape = Var2), size = 3) + 
 scale_color_manual(values=c("#7CAE00", "#00BFC4", "#000000", "#C77CFF")) +
 scale_x_continuous(breaks=seq(0,30,5)) +
 theme_bw()  

enter image description here

This issue with this graph is that the shapes are not filled in by colour in the graph.

This is my data.

dput(data)
structure(list(X = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L), Y = c(1L, 1L, 1L, 2L, 4L, 13L, 18L, 19L, 
21L, 24L, 34L, 43L, 70L, 90L, 129L, 169L, 1L, 3L, 3L, 3L, 3L, 
4L, 21L, 79L, 157L, 229L, 323L, 470L, 655L, 889L, 1128L, 1701L, 
2036L, 2502L, 3089L, 3858L, 4636L, 5883L, 7375L, 9172L, 10149L
), Var1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("", 
"ID1", "ID2"), class = "factor"), Var2 = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 4L, 2L, 2L), .Label = c("", "0", "Point1", "Point2"
), class = "factor")), row.names = c(NA, -41L), class = "data.frame")
1

1 Answers

2
votes

How about this

ggplot(data, aes(X, Y))+
geom_line(aes(color = Var1)) +
  geom_point(data = subset(data, Var2 != 0), aes(shape = Var2, color=Var1), size = 3) + 
  scale_color_manual(values=c("#7CAE00", "#00BFC4", "#000000", "#C77CFF")) +
  scale_x_continuous(breaks=seq(0,30,5)) +
  theme_bw()+
  guides(colour = guide_legend(override.aes = list(shape = NA)))