在R中生成热图(多个自变量)

在R中生成热图(多个自变量),r,heatmap,R,Heatmap,有几个类似的问题,但他们不是问我在寻找什么。 我有一个包含多个自变量的基因表达数据。我想用R中的热图将其可视化。我无法在热图上同时包含所有三个变量。下面是示例代码: species <- rep(c("st", "rt"), each = 18) life <- rep(c("5d", "15d", "45d"), 2, each = 6) concentration <- rep(c("c1", "c2", "c3"), 6, each = 2) gene <- rep

有几个类似的问题,但他们不是问我在寻找什么。 我有一个包含多个自变量的基因表达数据。我想用R中的热图将其可视化。我无法在热图上同时包含所有三个变量。下面是示例代码:

species <- rep(c("st", "rt"), each = 18)
life <- rep(c("5d", "15d", "45d"), 2, each = 6)
concentration <- rep(c("c1", "c2", "c3"), 6, each = 2)
gene <- rep(c("gene1", "gene2"), 36, each = 1)
response <- runif(36, -4, 4)
data1 <- data.frame(species, life, concentration, gene, response)

species我不确定代码中的哪些变量对应于图表中的哪些维度,但使用
ggplot2
软件包,很容易做到:

library(ggplot2)

ggplot(data1, aes(x = factor(life, levels = c("5d", "15d", "45d")), 
                  y = concentration, 
                  fill = response)) + 
  geom_tile() + 
  facet_wrap(~species + gene, nrow = 1) + 
  scale_fill_gradient(low = "red", high = "green", guide = FALSE) + 
  scale_x_discrete(name = "life")
当然,您可以相应地调整标题、标签、颜色等