R 热图着色取决于高于或低于零的值

发布于 2024-12-17 02:36:51 字数 498 浏览 6 评论 0原文

我正在尝试在 R 中制作热图,其中我的数据范围从 -5 到 +50。我希望 0 的值是白色,0 到 -5 之间的值从白色到蓝色渐变着色,0 到 +50 之间的值从白色到红色着色。理想情况下,我还希望使用相同的缩放比例,即 -5 时的蓝色暗度与 +5 时的红色暗度相同。

我有热图的工作代码,整个数据集的渐变从蓝色到红色,白色着色点接近数据的平均值。

hv <- heatmap.2(data_matrix,
    scale="none",
    Rowv=NA, 
    Colv=NA, 
    col = rev(brewer.pal(11,"RdBu")), 
    margins=c(5,5), 
    cexRow=0.5, cexCol=1.0,
    ylab= "Mutations",
    main = "heatmap", 
    key=TRUE,keysize=1.5, trace="none")

任何建议将不胜感激。

干杯!

I am trying to make a heatmap in R where my data ranges from -5 to +50. I would like values of 0 to be white, the values between 0 and -5 to be coloured on a gradient from white to blue and the values between 0 and +50 to be coloured from white to red. Ideally, I'd also like the same scaling to be used, i.e. the darkness of blue at -5 to be the same as the darkness of the red at +5.

I have working code for heatmap with the gradient for the whole data set from blue to red with white colouring points close to the mean of the data.

hv <- heatmap.2(data_matrix,
    scale="none",
    Rowv=NA, 
    Colv=NA, 
    col = rev(brewer.pal(11,"RdBu")), 
    margins=c(5,5), 
    cexRow=0.5, cexCol=1.0,
    ylab= "Mutations",
    main = "heatmap", 
    key=TRUE,keysize=1.5, trace="none")

Any suggestions would be greatly appreciated.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2024-12-24 02:36:51

您需要将这些添加到您的函数中。

中断 = c(5/6* -5,5/6* -4,5/6* -3,5/6* -2,5/6* -1,0,5/6* 1,50/6 *1,50/6*2,50/6*3,50/6*4,50/6*5)

Breaks=breaks

这指定您在热图中使用的中断。
您可以尝试稍微捏造一下数字,并且可以添加更多颜色和中断,中断的数量应该是颜色数 + 1。

给出:

library(RColorBrewer) 
library(gplots)
data_matrix <- cbind(c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)))

breaks = c(5/6* -5, 5/6* -4, 5/6* -3, 5/6* -2, 5/6* -1, 0 ,5/6* 1, 50/6*1, 50/6*2, 50/6*3, 50/6*4, 50/6*5)
hv <- heatmap.2(data_matrix, 
    scale="none",
    Rowv=NA,
    Colv=NA,
    col = rev(brewer.pal(11,"RdBu")),
    margins=c(5,5),
    cexRow=0.5, cexCol=1.0, 
    breaks=breaks,
    ylab= "Mutations",
    main = "heatmap",
    key=TRUE,keysize=1.5, trace="none")

You need to add these to you function.

breaks = c(5/6* -5,5/6* -4,5/6* -3,5/6* -2,5/6* -1,0,5/6* 1,50/6*1,50/6*2,50/6*3,50/6*4,50/6*5)

breaks=breaks

This specifies the breaks you use in the heatmap.
You might try to fudge the numbers a bit, and mayby add a few more colors and breaks, the number of breaks should be number of colors + 1.

Giving this:

library(RColorBrewer) 
library(gplots)
data_matrix <- cbind(c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)))

breaks = c(5/6* -5, 5/6* -4, 5/6* -3, 5/6* -2, 5/6* -1, 0 ,5/6* 1, 50/6*1, 50/6*2, 50/6*3, 50/6*4, 50/6*5)
hv <- heatmap.2(data_matrix, 
    scale="none",
    Rowv=NA,
    Colv=NA,
    col = rev(brewer.pal(11,"RdBu")),
    margins=c(5,5),
    cexRow=0.5, cexCol=1.0, 
    breaks=breaks,
    ylab= "Mutations",
    main = "heatmap",
    key=TRUE,keysize=1.5, trace="none")
风筝有风,海豚有海 2024-12-24 02:36:51

如果您的数据从 -5 到 +50,正负值具有相同的渐变将使负颜色看起来几乎全是白色,

washedout

相反,完整的色调版本是这样的,

fullhue

If your data goes from -5 to +50, having the same gradient for positive and negative values will make the negative colours look pretty much all white,

washedout

In contrast, the full hue version goes like this,

fullhue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文