如何创建facet heatmap ggplot2

时间:2015-11-29 20:07:45

标签: r ggplot2

我有大量的基因组数据如下:

chr   leftPos   Sample1    AnotherSample  EtcSample
1     4324       434          43           33
1     5353       63           34           532
1     6632       543          3544         23
2     1443       25           345          543
2     7644       74           26           324
2     8886       23           9            23
3     1287       643          45           23
3     5443       93           23           77
3     7668       33           45           33

我想创建一个由染色体组织的热图,其中x轴为样本,y轴为leftPos。我认为这在facet_wrap图像(由染色体组织)中看起来很好但这意味着我必须在ggplots中使用热图,我知道这不是一件事所以我必须使用geom_tiles()。

所以我尝试了谷歌搜索,但我坚持如何首先每个染色体做一个热图,然后每个样本做瓷砖。所有示例似乎只使用两列。

1 个答案:

答案 0 :(得分:1)

df <- data.frame(chr=c(1,1,1,2,2,2,3,3,3),
                 leftPos=c(4324, 5353, 6632, 1443, 7644, 8886, 1287, 5443, 7668),
                 Sample1=c(434,63,543,25,74,23,643,93,33),
                 AnotherSample=c(43,34,3544,345,26,9,45,23,45),
                 EtcSample=c(33,532,23,543,324,23,23,77,33))

以长格式重塑数据。

df.l <- reshape(df, 
                varying = c("Sample1", "AnotherSample", "EtcSample"),
                idvar="chr",
                v.names = "value",
                timevar = "sample",
                times=c("Sample1", "AnotherSample", "EtcSample"),
                new.row.names=c(1:(3*nrow(df))),
                direction = "long")

> df.l 
   chr leftPos        sample value
1    1    4324       Sample1   434
2    1    5353       Sample1    63
3    1    6632       Sample1   543
4    2    1443       Sample1    25
5    2    7644       Sample1    74
...
12   1    6632 AnotherSample  3544
13   2    1443 AnotherSample   345
14   2    7644 AnotherSample    26
15   2    8886 AnotherSample     9
16   3    1287 AnotherSample    45
...
23   2    7644     EtcSample   324
24   2    8886     EtcSample    23
25   3    1287     EtcSample    23
26   3    5443     EtcSample    77
27   3    7668     EtcSample    33

为了根据您的数据进行展示,我将leftPos转换为系数。

library(ggplot2)
df.l$leftPos <- factor(df.l$leftPos)
ggplot(df.l, aes(sample, leftPos)) + geom_tile(aes(fill = value)) + 
scale_fill_gradient(low = "white", high = "red") + facet_wrap(~chr)+ 
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

enter image description here