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

How to specific colour for a continuous variable in 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)

感谢您的任何建议。

这会起作用:

 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)