2017-07-06 47 views
0

有几个类似的问题,但他们没有问我在找什么。 我有一个基因表达数据与多个独立变量。我想用R中的热图可视化它。我不能将所有三个变量都包含在热图上。以下是示例代码:在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) 

我可以使用任何软件包。请看下面的图片来自不同的数据集。我希望以类似的方式可视化我的数据。

example_data_visualized

非常感谢提前!

回答

0

我不知道它在你的代码的变量对应于你的图表中的尺寸,但使用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") 

当然,你可以相应地调整标题,标签,颜色等。