如何在ggplot2中为连续变量指定特定颜色?

时间:2015-02-06 10:14:47

标签: r ggplot2

我想在ggplot2中手动设置连续的颜色。我怎么能这样做?

这是我的示例代码。

library(ggplot2)
set.seed(1)
df <- data.frame(x = runif(100), y = runif(100), z = runif(100))

library(RColorBrewer)
cols <- rev(brewer.pal(11, 'RdYlBu'))

ggplot(df) + 
    geom_point(aes(x, y, colour = z)) +
    scale_colour_manual(values = cols)

感谢您的任何建议。

1 个答案:

答案 0 :(得分:4)

这将有效:

 library(ggplot2)
    set.seed(1)
    df <- data.frame(x = runif(100), y = runif(100), z = runif(100))

    library(RColorBrewer)
    cols <- rev(brewer.pal(11, 'RdYlBu'))

    ggplot(df, aes(x, y, colour = z)) + 
        geom_point()+
        scale_colour_gradientn(colours = cols)