2015-09-13 26 views
1

比方說:定義特定值R中與pheatmap着色

m1<-matrix(rnorm(1000),ncol=100) 

和定義顏色:

cols = colorRampPalette(c("white", "red"))(30) 

我沒有與pheatmap功能集羣產生熱圖:

pheatmap(dist(t(m1)), cluster_rows = F, cluster_cols = F, show_rownames = TRUE, 
color = cols, main = 'Heatmap') 

問題是,我如何定義顏色才能獲得相同的熱圖,但只能使用特定值的像素ue着色(例如小於0.1)。

我試圖設置

cols = ifelse(dist(t(m1))<0.1,'red','black') 

,但沒有奏效。

回答

3

對於一個簡單的二進制的配色方案,你可以使用breaks參數:

library(pheatmap) 

set.seed(1) 
m1<-matrix(c(rnorm(1000)), ncol=100) 

pheatmap(dist(t(m1)), 
     cluster_rows = F, 
     cluster_cols = F, 
     show_rownames = TRUE, 
     color = c("red", "black"), 
     breaks = c(0, 3, 9), # distances 0 to 3 are red, 3 to 9 black 
     main = 'Heatmap') 

它看起來像這樣:

enter image description here

如果你喜歡的顏色漸變,這是可以做到的如下:

m <- matrix(c(rnorm(1000)), ncol=100) 
distmat <- dist(t(m)) 

# Returns a vector of 'num.colors.in.palette'+1 colors. The first 'cutoff.fraction' 
# fraction of the palette interpolates between colors[1] and colors[2], the remainder 
# between colors[3] and colors[4]. 'num.colors.in.palette' must be sufficiently large 
# to get smooth color gradients. 
makeColorRampPalette <- function(colors, cutoff.fraction, num.colors.in.palette) 
{ 
    stopifnot(length(colors) == 4) 
    ramp1 <- colorRampPalette(colors[1:2])(num.colors.in.palette * cutoff.fraction) 
    ramp2 <- colorRampPalette(colors[3:4])(num.colors.in.palette * (1 - cutoff.fraction)) 
    return(c(ramp1, ramp2)) 
} 

cutoff.distance <- 3 
cols <- makeColorRampPalette(c("white", "red", # distances 0 to 3 colored from white to red 
           "green", "black"), # distances 3 to max(distmat) colored from green to black 
          cutoff.distance/max(distmat), 
          100) 

pheatmap(distmat, 
     cluster_rows = F, 
     cluster_cols = F, 
     show_rownames = TRUE, 
     color = cols, 
     main = 'Heatmap') 

然後看起來裏柯本:

enter image description here

+0

嗨@WhiteViking,我想這和它處理了半個多小時,直到我停止它。我正在與Rstudio在McBook 1,4 GHz Intel Core i5上合作。另外,對我來說,在黑色背景中只有紅點是不錯的。 – Kwnwps

+0

@Kwnwps我已經添加了一個使用二進制配色方案的示例(紅色黑點)。 – WhiteViking

+0

@Kwnwps至於處理時間很長:這個玩具的例子會發生嗎?或者只爲一個更大的數據集 - 如果是這樣的話? – WhiteViking

2

不是你要的,但這裏有一個ggplot的解決方案,可以幫助別人。

set.seed(1)   # for reproducible example 
m1 <- matrix(rnorm(1000),ncol=100) 
d <- dist(t(m1)) 

library(ggplot2) 
library(reshape2) # for melt(...) 
gg.df <- melt(as.matrix(d), varnames=c("row","col")) 

# fill is red for value < 3; black for value >= 3 
ggplot(gg.df, aes(x=factor(col), y=factor(row)))+ 
    geom_tile(aes(fill=ifelse(value<3, "below", "above")), color=NA)+ 
    scale_fill_manual("Threshold",values=c(below="#FF0000", above="#000000"))+ 
    coord_fixed() 

# fill is black for value > 3; gradient white to red for value <= 3 
ggplot(gg.df, aes(x=factor(col), y=factor(row)))+ 
    geom_tile(aes(fill=value), color=NA)+ 
    scale_fill_gradient(low="#FFFFFF", high="#FF0000", limits=c(0,3), na.value="black")+ 
    coord_fixed()