ggplot2-按行排列的热图 [英] ggplot2 - Heatmap Table by Row

查看:291
本文介绍了ggplot2-按行排列的热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个热图表,这很容易,但是我希望将渐变颜色限制在单行内,而不是在整个data.frame内.结果应该是每一行都应同时具有最大和最小行.基本上,我可以在网上找到所有热图示例,然后根据表格中的所有值创建渐变色标.

I am trying to make a heatmap table, which is fairly easy, but I am wanting the gradient color to be confined within a single row, not over the entire data.frame. The result should be that each row should have the row max and min both present. Basically all examples of heatmaps I can find online create the gradient color scale from all values in the table.

这是一个示例数据框:

df <- data.frame('var' = paste0('var',1:3),
             'group1' = c(1,500,3),
             'group2' = c(2,300,1),
             'group3' = c(3,100,2),
             'group4' = c(4,50,4),
             'group5' = c(5,10,5))

这是我要寻找的结果:

And here is the result of what I am looking for:

推荐答案

如何按行缩放数据(除以行的平均值). ggplot2的示例:

What about scaling your data by row (divide it by the average of the row). Example with ggplot2:

df <- melt(df)
df <- data.table(df)
df[,value_rescaled:=value/mean(value),by=.(var)]


ggplot(data = df, aes(x = variable, y = var)) +
  geom_tile(aes(fill = value_rescaled))+
  scale_fill_gradient2(low = "#4682B4", mid = "#FFFFFF", high = "#FF0000", midpoint = 1, space = "Lab",
                      na.value = "grey50", guide = "colourbar")+
  geom_text(aes(label=value))+
  theme(legend.position="none")

这篇关于ggplot2-按行排列的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆