【问题标题】:R ggplot2 - adjust distance between breaks on a continuous colourbar gradientR ggplot2 - 调整连续颜色条渐变上的中断之间的距离
【发布时间】:2017-05-08 02:32:41
【问题描述】:

我在ggplot2 中使用geom_tile 创建了一个热图,并使用scale_fill_gradientn 映射了这些值。颜色条分为两个渐变。一个渐变映射值从 0 到 0.05,另一个渐变映射值从 0.05 到 1。热图看起来如所愿,但颜色条并不如所愿。我希望红黄色渐变 (0-0.05) 与黑棕色渐变 (0.05-1) 具有相同的高度。

问题:如何修改颜色条,使值 0.05 位于条的中间/中心,使两个不同颜色渐变的高度相同?

换句话说,假设 0.05 刻度线是一个滑块,您可以将其移动到条形图的中间以扩展/压缩相应的渐变。

以下两篇文章讨论了类似的问题,但没有最终的解决方案,或者至少不是我可以轻松理解并回答我的问题的问题:

非常感谢大家阅读我的问题并思考可能的解决方案。到目前为止,除了在 Photoshop 中编辑颜色条之外,我想不出任何有用的东西。彼得



可重现的例子:

TEST <- read.csv ("https://filetea.me/n3wHRhuy0GlS4xvjQDxs95BVA",header=T,row.names=NULL)

library(ggplot2) 
ggplot(TEST, aes(x=Measure, y=SMTSD))+
    geom_tile(aes(fill=Pval),colour="grey50", size=0.1) +
    scale_x_discrete(expand = c(0,0))+
    coord_equal(ratio=1)+

scale_fill_gradientn(colours=c("red","yellow","black","#996633"),
    values=c(0,0.0499,0.05,1),
    na.value="white", guide="colourbar",
    name="P-value",limits=c(0,1),breaks=c(0,0.05,1))+

 guides(fill = guide_colourbar(barheight = 20, direction = "vertical",
        title.position="top", title.hjust = 0.5,title.vjust = 0.5, nbin = 50))

【问题讨论】:

    标签: r ggplot2 heatmap


    【解决方案1】:

    更改 scale_fill_gradient 值和中断将为您提供所需的内容:

    ggplot(TEST, aes(x=Measure, y=SMTSD))+
      geom_tile(aes(fill=Pval^0.2313782),colour="grey50", size=0.1) +
      scale_x_discrete(expand = c(0,0))+
      coord_equal(ratio=1)+
      scale_fill_gradientn(colours=c("red","yellow","black","#996633"),
                           values=c(0,0.0499,0.05,1)^ 0.2313782,
                           na.value="white", guide="colourbar",
                           name="P-value",limits=c(0,1),breaks=c(0,0.05,1)^ 0.2313782, 
                           labels=c(0,0.05,1))+
      guides(fill = guide_colourbar(barheight = 20, direction = "vertical",
                                   title.position="top", title.hjust = 0.5,title.vjust = 0.5, nbin = 50))
    

    附言用新图像编辑

    【讨论】:

    • 用 0.2313782 代替 0.25 会不会不准确
    • 感谢@Adam Quek 的迅速而精彩的回复。能否请你解释一下^.25values=c(0,0.0499,0.05,1)^.25中的用法
    • @zacdav 我想会的。我只是在做一个心理估计,但是,是的,0.05^0.2313782 = 0.5
    • 他将值转换为在 0 和 1 之间缩放,因为您只有一个中断(会改变),他将 0.05 重新调整为 0.5,0.05^x = 0.50。
    • 在你的 geom_tile 中进行同样的处理,即geom_tile(aes(fill=Pval^0.2313782),colour="grey50", size=0.1)
    猜你喜欢
    • 2016-03-13
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多