2016-10-27 62 views
1

我想用一個變量(RPKM)對背景進行着色,因爲大多數值範圍從1到40,最大值是800,最終圖片幾乎是藍色的,使得無法區分近似值,如2和3.在pheatmap,我可以通過使用爲1到40指定更多顏色的中斷來解決此問題,並使用相同顏色使值大於100。我試圖用scale_fill_gradientn,scale_color_brewer做同樣的事情,但沒有成功,有人能幫助我嗎?如何分解ggplot2中連續變量的背景顏色?

\ 1。我的數據是這樣的:

head(data3, n=14) 
Gene_H Index  RPKM Usage Species Dif_index 
1 BORCS5  1  NA 0.9300  H   1 
2 BORCS5  1 4.663070 0.4200  R   1 
3 BORCS5  2  NA 1.0000  H  NA 
4 BORCS5  2 4.663070 1.0000  R  NA 
5 BORCS5  3  NA 1.0000  H  NA 
6 BORCS5  3 4.663070 0.8700  R  NA 
7 BORCS5  4  NA 1.0000  H  NA 
8 BORCS5  4 4.663070 1.0000  R  NA 
9 ALKBH3  1 0.000000 1.0000  H   1 
10 ALKBH3  1 5.330331 0.1400  R   1 
11 ALKBH3  2 0.000000 1.0000  H  NA 
12 ALKBH3  2 5.330331 1.0000  R  NA 
13 ALKBH3  3 0.000000 1.0000  H  NA 
14 ALKBH3  3 5.330331 1.0000  R  NA 

\ 2。我的代碼是:

ggplot(data3)+geom_point(aes(x=Index, y=Usage))+ylim(0,1)+ 
    geom_point(aes(x=Dif_index, y=Usage), color="red")+facet_wrap(Gene_H~Species, ncol=2)+ 
    theme(strip.text.x = element_blank(), axis.text.y=element_blank(), panel.grid.major=element_blank(), 
     panel.grid.minor=element_blank(), panel.margin=unit(0.1, "lines"))+ 
    geom_rect(aes(fill=RPKM), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) 

\ 3。然後我得到: enter image description here

\ 4。我曾試圖與cutscale_fill_brewer,但它輸出的錯誤,我沒有解決

geom_rect(aes(fill=cut(RPKM, c(seq(0,40,by=0.5),seq(41,800,by=20)))), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)+ 
    scale_fill_brewer(type="seq", palette="YlGn") 

Warning messages: 
1: In RColorBrewer::brewer.pal(n, pal) : 
    n too large, allowed maximum for palette YlGn is 9 
Returning the palette you asked for with that many colors 

2: Removed 5 rows containing missing values (geom_point). 
3: Removed 122 rows containing missing values (geom_point). 
4: In RColorBrewer::brewer.pal(n, pal) : 
    n too large, allowed maximum for palette YlGn is 9 
Returning the palette you asked for with that many colors 

\ 5。隨着scale_color_discrete,它會將顏色分爲不同的種類,但我希望顏色改變漸變。

geom_rect(aes(fill=cut(RPKM, c(seq(0,40,by=0.5),seq(41,800,by=20)))), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)+ 
    scale_color_discrete() 

enter image description here

+0

'scale_fill_brewer'是離散的規模,嘗試'scale_fill_distiller'代替(不'cut'所以你RPKM是連續的)。 – Gregor

回答

1

scale_fill_brewer適用於離散比例尺,對於基於相同調色板的連續比例尺,您可以使用scale_fill_distiller。以下是一個示例(與color而不是fill - 爲您的用例切換回fill)與您的數據相同的0到50比例。

x = seq(0, 50, by = 2) 
dd = data.frame(x = x, y = x) 

gridExtra::grid.arrange(g + scale_color_distiller(palette = "RdYlGn"), 
      g + scale_color_distiller(palette = "PiYG"), 
      g + scale_color_distiller(palette = "YlGn")) 

enter image description here

您可以使用RColorBrewer::display.brewer.all()看到所有的RColorBrewer調色板選項。

另一種選擇是,因爲您的數據似乎集中在0附近,因此將對該比例進行對數或平方根變換。平方根將更自然,因爲您的數據包含0,但這有助於分散較低的顏色並壓縮較高的顏色。只需將trans = "sqrt"添加到任何scale_fill函數。對於更極端的轉換(可能需要您的數據上升到800),你可以log(RMKP + 1),這是用trans = "log1p"實現的。

這裏是同一地塊從上述但trans = "sqrt"添加到秤:

enter image description here

+0

不錯,你知道'trans'記錄在哪裏嗎?正在尋找類似的東西,假設它肯定存在,但無法找到它。 – BrodieG

+0

它在'?continuous_scale'中 - 適用於任何比例(不僅僅是顏色)。但是我真的知道它[來自Hadley評論這個古老的問題](http://stackoverflow.com/q/8069837/903061)。 – Gregor

0

您可以登錄色階:

set.seed(1) 
dat <- cbind(
    expand.grid(x=1:10, y=1:10), 
    z=sample(c(rep(1:40, length.out=99), 800)) 
) 
exp10 <- function(x) 10^x 
p <- ggplot(dat, aes(x=x, y=y, fill=log10(z))) + geom_tile() 
p + scale_fill_continuous(name="z", labels=exp10) 

enter image description here

,還可以使用一個更好的色標:

library(viridis) 
p + scale_fill_gradientn(name="z", labels=exp10, colours=viridis(256)) 

enter image description here

0

@BrodieG @Gregor

您好,感謝您的回覆!

使用日誌真的有助於解決這個問題時,有一些非常重要的價值。我想知道我是否可以通過將連續值更改爲離散值來解決問題,正如我在上面的4和5中所述。實際上,我認爲用4,除了輸出顏色不足的錯誤(除了輸出圖片後)外,我也會得到滿意的結果。谷歌後,有人建議使用「colorRampPalette(brewer.pal(9,」YlGn「))(101)」,但我不知道在哪裏添加和失敗。 5,與4相同,只是顏色沒有逐漸變化。

enter image description here

+0

我最後通過添加一個變量col = colorRampPalette(c(「whilte」,「red」))(101)來解決它,然後結合cut和scale_fill_manual,即gg + geom_rect(aes(fill = cut(RPKM ,c(seq(0,40,by = 0.5),seq(41,800,by = 20)))),xmin = -Inf,xmax = Inf,ymin = -Inf,ymax = Inf)+ scale_fill_manual(values = col ) – lam138138