ggplot2:如何为由不同颜色的线连接的点指定多种填充颜色 [英] ggplot2: How to specify multiple fill colors for points that are connected by lines of different colors

查看:1700
本文介绍了ggplot2:如何为由不同颜色的线连接的点指定多种填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ggplot2 的新用户。我想创建一个线图,在它们上面的点填充不同的颜色比线(见下面的图)。

假设我正在使用的数据集是下面的一个:

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

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

But I do not get what I want.I hope someone can point me to the right direction. Thanks!

推荐答案

scale_fill_manual()仅当您已设置 fill = 时,才能使用scale_shape_manual() scale_colour_manual() aes()中的 shape = color =

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

要改变颜色只需为点添加 color = group 里面 geom_point ()调用。

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

这篇关于ggplot2:如何为由不同颜色的线连接的点指定多种填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆