2017-02-16 39 views
0

是否可以通過多個低和高定義參數製作顏色梯度?通過2個以上定義的顏色的梯度連續縮放填充或顏色

比方說,在這樣的數據:

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 

設置低和高參數僅供導致線性漸變色,但我希望有更多的組合顏色漸變,允許用戶從白色變成藍色,然後黑色的說。

感謝您的關注和答覆。

+1

'scale_fill_gradientn'可能是你在找什麼。 –

回答

2

你可以試試:

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

enter image description here

1
ggplot(data.frame(x=seq(along=x), y=rnorm(100, mean = 1000, sd = 500)), aes(x=x, y=y, colour=y)) + 
geom_point() + 
scale_colour_gradientn(values=c(0, 1000, 2000, 3000), 
colours=c("green", "blue", "red", "yellow"), 
rescaler=function(x, ...) x, oob=identity) 

給這個情節

plot

1

gplotscolorpanel功能非常適合這個。它產生的n種顏色的光譜(例如,50)2顏色之間或3(中間色是可選的):

library("gplots") 
scale_fill_continuous <- colorpanel(50, "blue", "red") #2 colours 
scale_fill_continuous <- colorpanel(50, "blue", "white", "red") #3 colours 

也有各種不同的預設功能,例如,rainbow(50)greenred(50)(MID =黑),或bluered(50)(mid = white)。

對於給出的示例:

#Generate a colour spectrum 
library("gplots") 
scale_fill_continuous <- colorpanel(300, "green", "purple", "yellow") 
#Generate data 
set.seed(5) 
data <- rnorm(100, mean = 1000, sd = 500) 
mapping <- ceiling(data/100) #round to index in colour scale 
coloured_data <- scale_fill_continuous[mapping] #map to colour sale