2017-04-20 69 views
0

我是新手,我想德興熱圖。這是我的代碼:倒車默認比例梯度GGPLOT2

ggplot(gd, aes(Qcountry, Q6_1_Q6d), order = TRUE) + 
    geom_tile(aes(fill = prob), colour = "white") + 
    theme_minimal() + 
    labs(y = "Main reason for mobility", x = "Country") + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3)) + 
    scale_fill_gradient(name = "(%)") 

將會產生一個完美的圖表,我的問題是低水平是深藍色,更高的值是淡藍色的,這是不直觀。做最常用的方法是使用rev()。但在我的情況下,我不知道如何。因此,是否有可能扭轉這一默認比例This is the legend

其他的問題,有沒有辦法用一種顏色創建比例漸變。我的意思是,scale_fill_gradient/scale_fill_gradientn需要設置一個較低的色彩和高色彩(low = "", high = ""),我想通過紅色變藍色。

非常感謝您的支持。

+0

的方向關於第二個問題(請只問一次一個問題),你」將需要兩種不同的紅色陰影。像'red''和'scale :: muted('red')'。你可能想看看'viridis'包裝以獲得更好的色彩比例。 – Axeman

回答

0

?scale_colour_gradient顯示默認值low = "#132B43"high = "#56B1F7"

簡單地切換周圍的人:

ggplot(faithfuld, aes(waiting, eruptions)) + 
    geom_raster(aes(fill = density)) + 
    scale_fill_continuous(high = "#132B43", low = "#56B1F7") 

enter image description here

就個人而言,我認爲這是比默認不太直觀。

+0

非常感謝您以簡單的方式改進我的代碼。這工作完美! –

0

你可以從RColorBrewer(link)庫使用的調色板,並指定顏色漸變

library(RColorBrewer) 
library(ggplot2) 

ggplot(df, aes(x, y)) + 
    geom_tile(aes(fill = z, width = w), colour = "grey50") + 
    scale_fill_distiller(palette ="RdBu", direction = -1) # or direction=1 


# data 
    df <- data.frame(x = rep(c(2, 5, 7, 9, 12), 2), 
        y = rep(c(1, 2), each = 5), 
        z = rep(1:5, each = 2), 
        w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)) 

​​

+0

感謝您的回答!完美的作品! –