2017-04-20 57 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

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