ggplot2:如何为由不同颜色的线连接的点指定多种填充颜色

2023-11-24

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))

我尝试了以下代码:

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"))

但我没有得到我想要的。我希望有人能指出我正确的方向。谢谢!


scale_fill_manual(), scale_shape_manual() and scale_colour_manual()仅当您已设置时才可以使用fill=, shape= or colour=在 - 的里面aes().

要仅更改您应该添加的点的颜色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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ggplot2:如何为由不同颜色的线连接的点指定多种填充颜色 的相关文章

随机推荐