gpt4 book ai didi

R ggplot2 - 调整连续颜色条渐变上的中断之间的距离

转载 作者:行者123 更新时间:2023-12-05 00:51:41 46 4
gpt4 key购买 nike

我在 ggplot2 中创建了一个热图使用 geom_tile并使用 scale_fill_gradientn 映射值.颜色条被分成两个渐变。一个梯度映射值从 0 到 0.05,另一个梯度映射值从 0.05 到 1。热图看起来像预期的那样,但颜色条不是预期的。我希望红黄色渐变 (0-0.05) 与黑棕色渐变 (0.05-1) 具有相同的高度。

HEATMAP

QUESTION: How do I modify the colourbar so the value 0.05 is in the middle/center of the bar making the height of the two distinct color gradients identical?

In other words, imagine that the 0.05 tick mark is a slider and you can move it to the middle of the bar expanding/compressing the respective gradients.



类似的问题已在下面的两篇文章中讨论过,但没有确定的解决方案,或者至少不是我可以轻松理解和回答我的问题的解决方案:
  • Similar question 1
  • Similar question 2

  • 非常感谢大家阅读我的问题并思考可能的解决方案。到目前为止,除了在 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))

    最佳答案

    更改 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))

    enter image description here

    附言用新图像编辑

    关于R ggplot2 - 调整连续颜色条渐变上的中断之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43838929/

    46 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com