通过超过 2 种定义颜色的渐变以连续比例填充或着色

Kry*_*trý 2 r ggplot2

是否可以通过多个定义的低和高参数来制作颜色渐变?

让我们说,在这个数据上:

df <- data.frame(a = 1:100, 
                 b = rnorm(100, mean = 1000, sd = 500))
ggplot(df) +
   geom_point(aes(a, b)) +
   scale_fill_continous(low = "white", high = "black")
         ## as "low" and "high" is all scale_fill_continous() funcion offer
Run Code Online (Sandbox Code Playgroud)

设置低和高参数只会导致线性颜色渐变,但我想要更多组合的颜色渐变,比如说从白色到蓝色然后再到黑色。

感谢您的关注和解答。

Rom*_*man 5

你可以试试:

set.seed(123)
df <- data.frame(a=1:100, b=rnorm(100, mean = 1000, sd = 500))
ggplot(df,aes(x=a, y=b, col=b)) + geom_point() +  
  scale_colour_gradientn(colours = c("green", "blue","blue", "red","red", "yellow"),breaks = c(0,1000,2000,3000), limits=c(0,3000))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明