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

时间:2023-02-09 16:50:50

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). ggplot2:如何为由不同颜色的线连接的点指定多种填充颜色 Suppose the dataset I am working with is the one below:

我是ggplot2的新手。我想创建一个在其上有点的线图,其中点用不同的颜色填充点(参见下图)。假设我正在使用的数据集如下:

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!

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

1 个解决方案

#1


15  

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

只有在aes()中设置了fill =,shape =或color =时,才能使用scale_fill_manual(),scale_shape_manual()和scale_colour_manual()。

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

要仅为点更改颜色,您应该在geom_point()调用中添加color = group。

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

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

#1


15  

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

只有在aes()中设置了fill =,shape =或color =时,才能使用scale_fill_manual(),scale_shape_manual()和scale_colour_manual()。

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

要仅为点更改颜色,您应该在geom_point()调用中添加color = group。

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

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