Ggplot2: How to specify multiple fill colors for points that are connected by lines of different colors

I am new to ggplot2 . I would like to create a line graph that has points on them, where the points are filled with different colors than the lines (see. The graph below). enter image description here Suppose the dataset I'm working with is the following:

 set.seed(100) data<-data.frame(dv=c(rnorm(30), rnorm(30, mean=1), rnorm(30, mean=2)), iv=rep(1:30, 3), group=rep(letters[1:3], each=30)) 

I tried the following code:

 p<-ggplot(data, aes(x=iv, y=dv, group=group, pch=group)) + geom_line() + geom_point() p + scale_color_manual(values=rep("black",3))+ scale_shape(c(19,20,21)) + scale_fill_manual(values=c("blue", "red","gray")) p + scale_shape(c(19,20,21)) + scale_fill_manual(values=c("blue", "red","gray")) 

But I don’t understand what I want. Hope someone can point me in the right direction. Thanks!

+4
colors r plot ggplot2
source share
1 answer

scale_fill_manual() , scale_shape_manual() and scale_colour_manual() can only be used if you set fill= , shape= or colour= inside aes() .

To change color only for points you must add colour=group inside geom_point() .

  ggplot(data, aes(x=iv, y=dv, group=group,shape=group)) + geom_line() + geom_point(aes(colour=group)) + scale_shape_manual(values=c(19,20,21))+ scale_colour_manual(values=c("blue", "red","gray")) 

enter image description here

+12
source share

All Articles