【问题标题】:ggplot2: How to specify multiple fill colors for points that are connected by lines of different colorsggplot2:如何为由不同颜色的线连接的点指定多种填充颜色
【发布时间】:2013-03-12 13:49:25
【问题描述】:

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

我尝试了以下代码:

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

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

【问题讨论】:

    标签: r colors plot ggplot2


    【解决方案1】:

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

    要为点更改颜色,您应该在 geom_point() 调用中添加 colour=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"))
    

    【讨论】:

    • 如果情节已经创建,我想改变它的调色板怎么办?
    猜你喜欢
    • 2014-03-02
    • 2016-08-23
    • 2018-12-12
    • 1970-01-01
    • 2016-09-13
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多