如何制作包含大量数据的热图?

时间:2018-05-09 05:23:28

标签: r plot ggplot2

数据集是不同样本类型的基因表达。我想在y轴上使用Genus制作热图,在x轴上制作样本类型。像this一样。这是我的代码:

library(ggplot2)
data1<-structure(list(Genus = structure(1:15, .Label = c("a", "b", "c", 
                                                  "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"), class = "factor"), 
               ID = structure(c(1L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
                                2L, 3L, 4L, 5L, 6L, 7L), .Label = c("1", "10", "11", "12", 
                                                                    "13", "14", "15", "2", "3", "4", "5", "6", "7", "8", "9"), class = "factor"), 
               Exposed = c(4.45664848317392, 3.47352433833294, 1.06264726491645, 
                           2.19005092629232, 2.26533363806084, 3.31810337258503, 0.624824835686013, 
                           1.69351499876939, 0.831915790913627, 3.34144014748745, 1.04171383427456, 
                           2.29188611265272, 3.61610786640085, 0.26214003097266, 4.43111610598862
               ), Recovered = c(1.42280009156093, 2.54399934201501, 2.02104107988998, 
                                2.04466922697611, 2.28061385918409, 0.993064268259332, 1.05062131932937, 
                                1.1557382948231, 2.36977807246149, 0.546837221598253, 2.07467105076648, 
                                0.675126203102991, 1.78709530178457, 1.71838806266896, 0.769032474607229
               ), Immune = c(0.0398402754217386, 0.33513950847555, 0.30276765988674, 
                             0.191129956976511, 0.0744170512771234, 0.484796752454713, 
                             0.0741345254937187, 0.183232698123902, 0.472231584484689, 
                             0.45325757318642, 0.0141741185216233, 0.122682133689523, 
                             0.189284125575796, 0.00848434411454946, 0.351375629426911
               )), .Names = c("Genus", "ID", "Exposed", "Recovered", "Immune"
               ), row.names = c(NA, -15L), class = "data.frame")

plot<-ggplot(data1,aes(x=Exposed,Recovered,Immune,y=Genus))+heatmap()

为什么heatmap()不起作用?

2 个答案:

答案 0 :(得分:2)

正如您所指出的,它将有助于您首先将数据帧转换为长格式。这是一种使用geom_tile()的方法。这是你正在寻找的吗?

由于我们使用geom_tile()而没有任何其他选项,因此绘图默认为蓝色调色板,数据框中较高的值将与较浅的颜色相关联。

您可以根据自己的喜好更改这些选项;我建议看一下:http://ggplot2.tidyverse.org/reference/geom_tile.html

library(reshape2)
library(tidyverse)

id_names <- c("ID", "Genus")
measure_names <- c("Exposed", "Recovered", "Immune")

df <- melt(data1, id.vars = id_names, measure.vars = measure_names) %>%
      rename(SampleType = variable, Expression = value)

heatmap_plot <- ggplot(df, aes(x = SampleType, y = Genus, fill = Expression)) + 
                xlab("Sample Type") +
                geom_tile()

print(heatmap_plot)

enter image description here

答案 1 :(得分:1)

您需要将数据转换为“长”形式,以便Exposed,Recovered和Immune的所有不同值都在一列中,另一列标识样本类型。您希望在SampleType来电中将x列映射到ggplot()。然后你可以这样做:

library(tidyverse)

data1_long = data1 %>%
    gather(SampleType, Value, Exposed, Recovered, Immune)

ggplot(data1_long, aes(x = SampleType, y = Genus, fill = Value)) +
    geom_tile()

结果:

enter image description here