根据热图中的变量调整磁贴大小
Resize tile based on variable in Heatmap
我有一个数据框,我为其构建了一个热图,如下所示:
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
我该怎么做?这可能吗?感谢您的帮助!
您的问题很有道理,但是如果您更改图块的大小,它看起来就不再像热图了,例如:
# 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
,或者我可以添加树状图等到这个情节,看看它的样子。
我有一个数据框,我为其构建了一个热图,如下所示:
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
我该怎么做?这可能吗?感谢您的帮助!
您的问题很有道理,但是如果您更改图块的大小,它看起来就不再像热图了,例如:
# 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
,或者我可以添加树状图等到这个情节,看看它的样子。