【问题标题】:Resize tile based on variable in Heatmap根据热图中的变量调整图块大小
【发布时间】:2020-10-14 02:07:01
【问题描述】:

我有一个数据框,我为其构建了一个热图,如下所示:

df <- data.frame("CELL1" = c(0.7,0.7,0.5 ), 
             "CELL2" = c(1, 0.4, 0.3), 
             "CELL3" = c(0.7, 0.73, 0.61))
#set rownames
rownames(df) <- c("GENE1", "GENE2", "GENE3")

#plot
heatmap.2(as.matrix(df), scale = "column",Rowv=TRUE, Colv=TRUE, 
          trace = "none",  distfun = dist,
          hclustfun = hclust, 
          margins=c(20,20))

我想更改此热图,以便每个图块的大小基于另一个数据框中的数字,具有相同的列和行名,但值不同。这样,基因 1-cell1 将有一个大图块,而基因 1、cell2 将有一个较小的图块。如下图:

#Create df
df2 <- data.frame("CELL1" = c(183,2,19 ), 
                 "CELL2" = c(24, 1.8, 11.1), 
                 "CELL3" = c(18.9, 3.3, 22.9))
rownames(df2) <- c("GENE1", "GENE2", "GENE3")
 
> df2
      CELL1 CELL2 CELL3
GENE1   183  24.0  18.9
GENE2     2   1.8   3.3
GENE3    19  11.1  22.9

我该怎么做?这甚至可能吗?任何帮助表示赞赏!

【问题讨论】:

    标签: r dataframe ggplot2 heatmap


    【解决方案1】:

    您的问题是有道理的,但是如果您更改图块的大小,它看起来就不再像热图了,例如:

    # Load libraries
    library(tidyverse)
    # Create dataframes
    df <- data.frame("Gene" = c("GENE1", "GENE2", "GENE3"),
                     "CELL1" = c(0.7, 0.7, 0.5), 
                     "CELL2" = c(1, 0.4, 0.3), 
                     "CELL3" = c(0.7, 0.73, 0.61))
    df2 <- data.frame("Gene" = c("GENE1", "GENE2", "GENE3"),
                      "CELL1" = c(183, 2, 19), 
                      "CELL2" = c(24, 1.8, 11.1), 
                      "CELL3" = c(18.9, 3.3, 22.9))
    # Pivot df1 to 'long' format
    data <- pivot_longer(data = df, cols = c(CELL1, CELL2, CELL3))
    # Pivot df2 to 'long' format and scale values
    data2 <- pivot_longer(data = df2, cols = c(CELL1, CELL2, CELL3)) %>%
      mutate(value = log10(value)/1.5)
    # Plot "data" using "data2" to set the tile sizes
    ggplot(data = data, aes(x = name, y = Gene)) +
      geom_tile(data = data2, aes(fill = value, width = value, height = value)) +
      theme(panel.background = element_blank())
    

    所以我不确定这对你有什么用,但如果这是你想要做的,我会看看是否可以使用heatmap.2,或者我可以将树状图等添加到这个情节中看看它的样子。

    【讨论】:

    • 谢谢!还有一个问题,有没有办法确保瓷砖不重叠(如果宽度足够大,它们似乎会这样做。
    • 我知道确保瓷砖不重叠的唯一方法是缩放瓷砖的大小,例如geom_tile(data = data2, aes(fill = value, height = value / 150, width = value / 150))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2018-01-25
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多