Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

r - 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 plot that has points on them where the points are filled with different colors than the lines (see the plot below). enter image description here Suppose the dataset I am working with is the one below:

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 do not get what I want.I hope someone can point me to the right direction. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

To change colour just for the points you should add colour=group inside geom_point() call.

  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...