从 R 中的公共值矩阵绘制热图

Plotting a heatmap out of common value matrix in R

我有一个 tab-separated 文件,如下所示:

        C1P C2P C3P C4P C5P
sam1    3 cp    3 cp    3 cp    3 cp    3 cp
sam2    S3c 4 cp    3 cp    3 cp    S3c
sam3    3 cp    3 cp    3 cp    3 cp    3 cp
sam4    3 cp    3 cp    LOH LOH 3 cp
sam5    3 cp    3 cp    3 cp    3 cp    3 cp
sam6    4 cp    4 cp    UPD UPD UPT

现在我想为每个值[条件]分配一种颜色...并让颜色出现在任何有相同值的地方,从而制作一个热图来表示特定条件[列中的值]在每个样本 [行 headers].

现在我正在为每个条件分配数值,然后通过 pheatmap 生成热图。但我一直在寻找更强大的方式来做到这一点。

感谢任何帮助。

这应该让你相当接近:

library(tidyverse)
df %>%
    rownames_to_column("row") %>%
    gather(col, Value, -row) %>%
    mutate(
        row = factor(row, levels = rev(unique(row))),
        Value = factor(Value)) %>%
    ggplot(aes(col, row, fill = Value)) +
    geom_tile() +
    scale_fill_manual(values = c(
        `3 cp` = "yellow",
        `4 cp` = "red",
        LOH = "blue",
        S3c = "lightgreen",
        UPD = "darkgreen",
        UPT = "black")) +
    labs(x = "", y = "") +
    theme_minimal()

解释:

  1. 从宽到长重塑数据。
  2. 使用 geom_tile 绘制热图,其中填充颜色由值的 factor 级别指定。
  3. 剩下的就是美学 "fluff" 以增加与您 link 的图像的相似性。

示例数据

df <- read.table(text =
    "        C1P C2P C3P C4P C5P
sam1    '3 cp'    '3 cp'    '3 cp'    '3 cp'    '3 cp'
sam2    'S3c' '4 cp'    '3 cp'    '3 cp'    'S3c'
sam3    '3 cp'    '3 cp'    '3 cp'    '3 cp'    '3 cp'
sam4    '3 cp'    '3 cp'    LOH LOH '3 cp'
sam5    '3 cp'    '3 cp'    '3 cp'    '3 cp'    '3 cp'
sam6    '4 cp'    '4 cp'    UPD UPD UPT", header = T)