【问题标题】:R ggplot heatmap manual binning and selecting middle bin colorR ggplot 热图手动分箱和选择中间箱颜色
【发布时间】:2019-10-09 07:17:43
【问题描述】:

我正在尝试生成具有不同颜色(红色、白色、蓝色)但白色处于选定值(或移动比例)的热图,而不是数据的自动拟合/居中。

样本数据集:

set.seed(5)
demo <- data.frame(x = seq(from = -40, to = 40, by = 5), y = 0:5, data = runif(102, min = 0, max = 1))

我生成热图的尝试基于:

library(ggplot2)
library(ggsci)
ggplot(demo) + geom_tile(aes(x = x, y = y, fill = data), color = NA) + scale_fill_gsea()

但是,我想将中心“白色”部分移动到不同的值,例如 0.9,并且我想设置自己的离散箱。

通过阅读更多文章和 stackoverflow 帖子,我的理解是,在使用 scale_fill_manual 之前,我将不得不使用 cut 生成我自己的部分,以便设置我自己的色标。

有没有我想念的更简单的方法?

谢谢。

【问题讨论】:

  • 嗨@kath,谢谢!

标签: r ggplot2 heatmap binning


【解决方案1】:

使用scale_fill_gradient2,您可以为中点定义自定义值:

ggplot(demo) + 
  geom_tile(aes(x = x, y = y, fill = data), color = NA) + 
  scale_fill_gradient2(low = "blue", mid = "white", high = "red", 
                       midpoint = 0.9, 
                       breaks = seq(0, 1, 0.1), 
                       limits = c(0, 1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2018-03-15
    相关资源
    最近更新 更多