简体   繁体   English

ggplot-加热调色板

[英]ggplot - heat color palette

i want a color scale like in the picture: 我想要一个像图片中的色标:

在此处输入图片说明

So from top to buttom we have: 因此,从上到下,我们有:

a very bright yellow (white-ish), orange, red, black, blue, light blue to very bright blue (white-ish) 非常亮的黄色(白色),橙色,红色,黑色,蓝色,浅蓝色到非常亮的蓝色(白色)

I want a value of 0 always to be displayed as "black". 我希望始终将0值显示为“黑色”。 The smallest (negative) value should be that "extreme bright blue". 最小(负)值应为“极亮蓝色”。 The biggest value (postive) should be the "extreme bright yelllow". 最大的价值(正面)应该是“极高的黄度”。

Please note that the min and max are not equally distant from the origin 0. 请注意,最小值和最大值与原点0的距离并不相等。

Thats where i am: 那就是我在哪里:

library(ggplot2)
df <- data.frame(xDim = c(0, 1, 2, 0, 1, 2, 0, 1, 2), yDim = c(2, 2, 2, 1, 1, 1, 0, 0, 0), high = c(0, -1, 6, -3, 8, 5, -2, 7, 5))

ggplot(df, aes(xDim, yDim)) +
    geom_raster(aes(fill = high)) +
    scale_fill_gradient2(low = "blue", mid = "black", high = "red", midpoint = 0)

scale_fill_gradientn is most suited for this. scale_fill_gradientn最适合此操作。 You just need to get an appropriate vector of colors, then come up with a good range of values for those colors. 您只需要获取适当的颜色向量,然后为这些颜色提供一个合适的值范围。

In your case, we could choose as colors: 在您的情况下,我们可以选择以下颜色:

col <- c('yellow', 'orange', 'red', 'black', 'blue', 'skyblue', 'white')

and as values: 并作为值:

val <- c(seq(min(df$high), 0, length.out = 4), seq(0, max(df$high), length.out = 4)[-1])

We need to use rescale according to the docs, so we do: 我们需要根据文档使用rescale ,所以我们这样做:

p <- ggplot(df, aes(xDim, yDim)) +
  geom_raster(aes(fill = high)) +
  scale_fill_gradientn(colours = col, values = scales::rescale(val))

cowplot::ggdraw() + cowplot::draw_plot(cowplot::get_legend(p))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM