2016-06-19 95 views
0

我想使用包viridisRColorBrewer的包裝中的調色板繪製一個圖表,但我想設置變量分佈中的哪個點應該被視爲中間點顏色漸變。在viridis或RColorBrewer中設置顏色範圍的參考值

例如,在下面的圖中,R取0.5作爲顏色範圍的中點。我如何設置不同的值,例如0.25?

library(ggplot2) 
library(RColorBrewer) 

# data 
set.seed(1) 
df <- expand.grid(x = 0:5, y = 0:5) 
df$z <- runif(nrow(df)) 

# plot 
ggplot(df, aes(x, y, fill = z)) + geom_raster() + 
    scale_fill_distiller(palette="RdBu", guide = "colorbar") 

enter image description here

# plot using viridis 
library(viridis) 

ggplot(df, aes(x, y, fill = z)) + geom_raster() + 
    scale_fill_viridis() 

enter image description here

編輯:我一直在尋找與此類似這裏的解決方案,因爲我的變量的分佈是不對稱的。但是,我想保持從深藍到深紅的整個顏色範圍,這似乎是不可能的。

ggplot(df, aes(x, y, fill = z)) + geom_raster() + 
    scale_fill_gradient2(low = "#2166ac", mid = "#f7f7f7", high = "#b2182b", midpoint = 0.2, 
         space = "Lab", na.value = "grey50", guide = "colourbar") 

enter image description here

+0

@Henrik我編輯了更多信息的問題。 –

回答

-1

也許嘗試設置

space = scale(c(-zlim, 0, zlim)) 

哪裏zlim是數據的極值。

相關問題