简体   繁体   中英

How to create facet heatmap ggplot2

I have a load of genomic data as follows:

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

I would like to create a heatmap organised by chromosome with sample along the x-axis and leftPos along the Y axis. I think this would look good in a facet_wrap image (organised by chromosome) but this means I have to use heatmaps in ggplots and I understand this isn't a thing so I have to use geom_tiles().

So I tried googling all over the place but I'm stuck with how to firstly do a heatmap per chromosome and secondly do tiles per sample. All the examples seem to just use two columns.

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))

Reshape your data in a long format.

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

For representation purpose based on your data, I converted leftPos into factor.

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())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM