R中使用原始值绘制热力图

4

我有这个数据框:

df <- data.frame(PatientID = c("3454","345","5","348","567","79"),
                 clas1 = c(1, 0, 5, NA, NA, 4),
                 clas2 = c(4, 1, 0, 3, 1, 0),
                 clas3 = c(1, NA, 0, 5, 5, 5), stringsAsFactors = F)

我想创建一个热力图,x轴上是患者ID,y轴上是clas1、clas2和clas3。热力图中表示的值将是每个“clas”的原始值。这里我发布了我想要的绘图。

enter image description here

很抱歉,因为我没有更多的颜色来代表这个,但这只是一个例子,任何颜色都可以使用。

重要的是,我想区分零和NAs,所以理想情况下,NAs有自己的颜色或出现在白色(空)中。

希望这足够清楚易懂。

如果有任何问题,请随时问。

非常感谢!


我看到你已经选择了一个“被选中”的答案,但是我添加了另一个答案,展示了几个很棒的选项(在我看来,它们比直接使用ggplot2代码更好。这就是为什么这些包存在的原因)。 - Tal Galili
4个回答

4

以下是使用基本R选项的“heatmap”的示例:

heatmap(t(`row.names<-`(as.matrix(df[-1]), df$PatientID)))

# Which is like
# x <- as.matrix(df[-1]
# row.names(x) <- df$PatientID
# heatmap(t(x))

enter image description here


谢谢Thomas,和下面那个一样... :( 它没有给我错误,但也没有生成图表。 - Lili
@AnilGoyal 它为矩阵提供行名称。我也给你点了赞,那是一个不错的图表。 - ThomasIsCoding

4
df <- data.frame(PatientID = c("3454","345","5","348","567","79"),
                 clas1 = c(1, 0, 5, NA, NA, 4),
                 clas2 = c(4, 1, 0, 3, 1, 0),
                 clas3 = c(1, NA, 0, 5, 5, 5), stringsAsFactors = F)
library(tidyverse)
df %>% pivot_longer(!PatientID) %>%
  ggplot(aes(x= PatientID, y = name, fill = value)) +
  geom_tile()

本示例由 reprex 包 (v2.0.0) 在2021年5月25日创建。


谢谢Anil,不幸的是这并没有起作用(它也没有给我任何错误,但它没有生成图表)。 - Lili
@Lili,这就是为什么我在reprex上展示的原因。这意味着错误出现在其他地方而不是代码中。首先尝试使用样本数据。 - AnilGoyal
2
不确定发生了什么,但是开关一下现在它就工作了!漂亮的图表,谢谢!!! - Lili

3
这里还有另一种选择:

在此输入图片描述

df <- data.frame(PatientID = c("3454","345","5","348","567","79"),
                 clas1 = c(1, 0, 5, NA, NA, 4),
                 clas2 = c(4, 1, 0, 3, 1, 0),
                 clas3 = c(1, NA, 0, 5, 5, 5), stringsAsFactors = F)


# named vector for heatmap
cols <-  c("0" = "white",
           "1" = "green", 
           "2" = "orange", 
           "3" = "yellow", 
           "4" = "pink",
           "5" = "black",
           "99" = "grey")
labels_legend <- c("0" = "0",
                   "1" = "1", 
                   "2" = "2", 
                   "3" = "3", 
                   "4" = "4", 
                   "5" = "5",
                   "99" = "NA")

df1 <- df %>% 
  pivot_longer(
    cols = starts_with("clas"),
    names_to = "names",
    values_to = "values"
  ) %>% 
  mutate(PatientID = factor(PatientID, levels = c("3454", "345", "5", "348", "567", "79")))

ggplot(
  df1, 
  aes(factor(PatientID),  factor(names))) +
  geom_tile(aes(fill= factor(values))) +
  # geom_text(aes(label = values), size = 5, color = "black") + # text in tiles
  scale_fill_manual(
    values = cols, 
    breaks = c("0", "1", "2", "3", "4", "5", "99"),
    labels = labels_legend,
    aesthetics = c("colour", "fill"),
    drop = FALSE
  ) +
  scale_y_discrete(limits=rev) +
  coord_equal() +
  theme(line = element_blank(),
        title = element_blank()) +
  theme(legend.direction = "horizontal", legend.position = "bottom")

3

准备数据

我将提供4个选项,其中每个选项都需要为行指定名称并删除id列。即:

df <- data.frame(PatientID = c("3454","345","5","348","567","79"),
                 clas1 = c(1, 0, 5, NA, NA, 4),
                 clas2 = c(4, 1, 0, 3, 1, 0),
                 clas3 = c(1, NA, 0, 5, 5, 5), stringsAsFactors = F)
rownames(df) <- df$PatientID
df$PatientID <- NULL
df

输出为:
> df
     clas1 clas2 clas3
3454     1     4     1
345      0     1    NA
5        5     0     0
348     NA     3     5
567     NA     1     5
79       4     0     5

基础R

使用基础R(良好的输出):

heatmap(as.matrix(df))

enter image description here

gplots

使用 gplots(有点丑,但是可以控制更多参数):

library(gplots)
heatmap.2(as.matrix(df))

enter image description here

heatmaply

使用heatmaply,您可以使用更好的默认设置来使用树状图(它还以更“最佳化”的方式组织它们)。

您可以在此处了解有关该软件包的更多信息

静态

使用heatmaply生成静态热图(更好的默认设置,我认为)

library(heatmaply)
ggheatmap(df)

enter image description here

现在有了彩色的树状图。
library(heatmaply)
ggheatmap(df, k_row = 3, k_col = 2)

enter image description here

没有树状图:
library(heatmaply)
ggheatmap(df, dendrogram = F)

enter image description here

互动式

使用heatmaply创建的互动式热图(悬停工具提示和缩放功能 - 它是互动式的!):

library(heatmaply)
heatmaply(df)

你可以使用交互式的heatmaply版本来完成静态ggheatmap能够完成的所有操作。

enter image description here


1
很棒的答案,提供了许多选项!已点赞! - ThomasIsCoding
嗨 Tal,一个快速的问题。我该怎么定义颜色比例范围?我想比较不同的热图,所以我需要所有的颜色比例从0到100 - 谢谢! - Lili
嘿@Lili - 这取决于您决定使用哪个热图选项。请就此编写一个新问题,涉及您关心的具体实现(以及一个简单的自包含示例)。谢谢 :) - Tal Galili
非常感谢! :)(基本上我正在表示百分比,并且我想比较两个不同的热图,其中一个从0%到100%,另一个从0%到60%。因此,我希望两者的比例都表示颜色从0到100%) - Lili

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接