使用绝对颜色映射进行热图

发布于 2025-01-26 05:19:47 字数 695 浏览 4 评论 0原文

我正在尝试创建一个热图,其中细胞的颜色与网格中的其他值不相关,而是与绝对值相关(有点像Excel中的单元格格式,例如,绿色的值在0.20至0.25之间。 )。我一直在玩R软件包超热,但我仍然不清楚如何提供这样的映射。关于如何完成的任何建议?最好使用过热,但可以提示如何通过其他软件包进行操作。

更新: 例如,使用MTCARS数据集,我们

superheat(mtcars,
          X.text = as.matrix(mtcars),
          legend.breaks = c(min(mtcars), 0, max(mtcars)))

在数据框中添加了一个新行,我们在原始行中获得了完全相同的值的不同颜色:

mtcars.new <- rbind(mtcars, 
                    extrarow = c(-100, 6, 20, 10, 6, 2, 10, 1, 1, 6, 6))

superheat(mtcars.new,
          X.text = as.matrix(mtcars.new),
          legend.breaks = c(min(mtcars.new), 0, max(mtcars.new)))

我想拥有的是,颜色不取决于所示的值在数据框中,当为不同的数据集创建地图时,可以将它们直接相互比较。

I am trying to create a heat map where the colors of the cells are not relative to the other values in the grid, but are linked to absolute values (a bit like the cell formatting in excel, e.g., green for values between 0.20 and 0.25). I have been playing around with the R package superheat but it is still unclear to me how to provide such a mapping. Any recommendations on how this can be done? Preferably using superheat but hints on how to do it via other packages are fine as well.

UPDATE:
For instance, using the mtcars dataset, we get

superheat(mtcars,
          X.text = as.matrix(mtcars),
          legend.breaks = c(min(mtcars), 0, max(mtcars)))

Adding a new row to the dataframe, we get different colors for the exact same values in the original rows:

mtcars.new <- rbind(mtcars, 
                    extrarow = c(-100, 6, 20, 10, 6, 2, 10, 1, 1, 6, 6))

superheat(mtcars.new,
          X.text = as.matrix(mtcars.new),
          legend.breaks = c(min(mtcars.new), 0, max(mtcars.new)))

What I would like to have, is that the colors do not depend on the values present in the dataframe, such that when maps are created for different datasets, they can be directly compared to each other.

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

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

发布评论

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

评论(1

叹倦 2025-02-02 05:19:47

这是一个示例,说明如果您创建一个“ grpcolor”变量,取决于您的变量值。然后,您可以使用此变量值匹配GGPLOT的颜色。 (如果需要,请肯定地更改因素水平)

### 1- Initiating data-frame
set.seed(1)
dfHeatmap <- data.frame(group1=rep(1:4, times=4), group2=rep(1:4, each=4), value=sample(1:100, 16))

### 2- Selecting colors depending on variable values
dfHeatmap$grpColor <- ifelse(dfHeatmap$value<30, "low", 
                 ifelse(dfHeatmap$value<60, "medium", "high"))

### 3- Heatmap with fill=grpColor option in geom_tile
ggplot(data=dfHeatmap, aes(x=as.factor(group1), y=group2)) + 
  geom_tile(aes(fill=grpColor)) + 
  scale_fill_manual("legend", values = c("#25714f", "#1ea467", "#00e57d")) + 
  xlab("group1") + 
  ylab("group2") + 
  geom_text(aes(label=value)) + 
  theme(legend.position="none", 
        line = element_blank())

”在此处输入图像描述

Here is an example on how you could do if you create a "grpColor" variable depending on your variable value. You could then match colors from ggplot with this variable value. (do not hesite to change factors levels if needed)

### 1- Initiating data-frame
set.seed(1)
dfHeatmap <- data.frame(group1=rep(1:4, times=4), group2=rep(1:4, each=4), value=sample(1:100, 16))

### 2- Selecting colors depending on variable values
dfHeatmap$grpColor <- ifelse(dfHeatmap$value<30, "low", 
                 ifelse(dfHeatmap$value<60, "medium", "high"))

### 3- Heatmap with fill=grpColor option in geom_tile
ggplot(data=dfHeatmap, aes(x=as.factor(group1), y=group2)) + 
  geom_tile(aes(fill=grpColor)) + 
  scale_fill_manual("legend", values = c("#25714f", "#1ea467", "#00e57d")) + 
  xlab("group1") + 
  ylab("group2") + 
  geom_text(aes(label=value)) + 
  theme(legend.position="none", 
        line = element_blank())

enter image description here

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