[英]How to create facet heatmap 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軸,leftPos沿Y軸。 我認為這在facet_wrap圖像(按染色體組織)中看起來不錯,但這意味着我必須在ggplots中使用熱圖,而且我知道這不是問題,因此必須使用geom_tiles()。
因此,我嘗試在整個地方進行谷歌搜索,但是我一直堅持如何首先對每個染色體進行熱圖繪制,然后對每個樣本進行平鋪。 所有示例似乎都只使用兩列。
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
轉換為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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.