R Pheatmap:更改注释颜色并防止弹出图形窗口 [英] R pheatmap: change annotation colors and prevent graphics window from popping up

查看:248
本文介绍了R Pheatmap:更改注释颜色并防止弹出图形窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

紧跟



如您所见,颜色很明显不是我指定的Set1调色板,而是默认的Pheatmap颜色(删除注释和颜色行会得到相同的结果)。





所以我的问题是:我如何在pheatmap中指定注解颜色?



我将Pheatmap输出保存为png文件,图形窗口不断弹出,如何防止这种情况发生?



谢谢!

 > sessionInfo()
R版本3.3.1(2016-06-21)
平台:x86_64-pc-linux-gnu(64位)
运行于:Ubuntu 16.04.1 LTS

语言环境:
[1] LC_CTYPE = en_SG.UTF-8 LC_NUMERIC = C
[3] LC_TIME = en_SG.UTF-8 LC_COLLATE = en_SG.UTF-8
[5] LC_MONETARY = en_SG.UTF-8 LC_MESSAGES = en_SG.UTF-8
[7] LC_PAPER = en_SG.UTF-8 LC_NAME = C
[9] LC_ADDRESS = C LC_TELEPHONE = C
[11] LC_MEASUREMENT = zh_SG.UTF-8 LC_IDENTIFICATION = C

附加基本软件包:
[1]统计图形grDevices utils数据集方法基础

其他附加软件包:
[1] gplots_3.0.1 RColorBrewer_1.1-2 pheatmap_1.0.8

通过名称空间加载(但未附加):
[1] Compiler_3.3.1 colorspace_1 .2-6比例_0.4.0 plyr_1.8.3
[5] tools_3.3.1 gtable_0.2.0 Rcpp_0.12.7 KernSmooth_2.23-15
[9] gdata_2.17.0 grid_3.3.1 caTools_1.17.1 bitops_1.0-6
[13] munsell_0.4.3 gtools_3.5.0


解决方案


  1. 要获得更多类别的更多颜色,您将需要使用其他调色板。有很多连续的调色板,使您可以超越12个调色板(例如使用RColorBrewer)。



  2. 如果您正在自己的代码中运行代码像MWE一样,不应弹出R图形窗口。我运行了您的示例,并在不向控制台打开图形设备的情况下生成了所需的文件。如果您要比较的话,下面是我的 sessioInfo()。我会尝试关闭所有设备( graphics.off()),然后运行您的热图代码,看看是否可以解决问题。我还将检查以确保您在当前正在使用的目录中拥有写权限?



  3. 如@cuttlefish在他的文章中所述要获得自定义的行/彩色,以在颜色列表中显示名称,必须注释注释数据帧的




对于超过12种颜色


  mymat<-matrix(rexp (720,rate = .1),ncol = 12)
colnames(mymat)<-c(rep( treatment_1,3),rep( treatment_2,3),rep( treatment_3,3),rep( treatment_4,3))
rownames(mymat)<-paste( gene,1:dim(mymat)[1],sep = _ )

annotdf<-data.frame(row.names = rownames(mymat),
category = rep(paste0( Category_,seq(12)),each = 5 ))

newCols<-colorRampPalette(grDevices :: rainbow(length(unique(annotdf $ category)))))
mycolors<-newCols(length(unique(anque(annotdf $ ca tegory)))
名称(mycolors)<-唯一(annotdf $ category)
mycolors<-list(类别= mycolors)

pheatmap(mymat,
color = greenred(75),
scale = row,
cluster_rows = FALSE,
cluster_cols = FALSE,
gaps_row = c(5,10,15,20 ,25,30,35,40,45,50,55),
gaps_col = c(3,6,9),
cellheight = 6,
cellwidth = 20,
border_color = NA,
fontsize_row = 6,
main =按类别分组的基因,
文件名= TEST_12cat.png,
注解_row = annotdf,
注解颜色= mycolors


SessionInfo


  R版本3.3.0(2016-05-03)
平台:x86_64-apple -darwin13.4.0(64-bi t)
运行环境:OS X 10.11.6(El Capitan)

语言环境:
[1] en_US.UTF-8 / en_US.UTF-8 / en_US.UTF -8 / C / en_US.UTF-8 / en_US.UTF-8

附加基本软件包:
[1]统计图形grDevices utils数据集方法基本

其他附加软件包:
[1] pheatmap_1.0.8 RColorBrewer_1.1-2 gplots_3.0.1

通过名称空间加载(且未附加):
[1] colorspace_1.3 -0比例_0.4.1 colorRamps_2.3 plyr_1.8.4 tools_3.3.0
[6] gtable_0.2.0 Rcpp_0.12.7 KernSmooth_2.23-15 gdata_2.17.0 grid_3.3.0
[11] caTools_1.17.1 munsell_0。 4.3 bitops_1.0-6 gtools_3.5.0


Following up on this question, I found the pheatmap function (which offers me a lot more control on the stuff that I want to do than heatmap.2).

I have 2 problems though:

1- I cannot change the colors of the annotation (categories)

2- The graphics window keeps popping up even when I am saving the output in a png file

This is my MWE:

library(pheatmap)
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(9, "Set1"))

mymat <- matrix(rexp(600, rate=.1), ncol=12)
colnames(mymat) <- c(rep("treatment_1", 3), rep("treatment_2", 3), rep("treatment_3", 3), rep("treatment_4", 3))
rownames(mymat) <- paste("gene", 1:dim(mymat)[1], sep="_")

annotdf <- data.frame(row.names = paste("gene", 1:dim(mymat)[1], sep="_"), category = c(rep("CATEGORY_1", 10), rep("CATEGORY_2", 10), rep("CATEGORY_3", 10), rep("CATEGORY_4", 10), rep("CATEGORY_5", 10)))

mycolors <- cols(length(unique(annotdf$category)))
names(mycolors) <- unique(annotdf$category)
mycolors <- list(mycolors = mycolors)

pheatmap(mymat,
     color=greenred(75),
     scale="row",
     cluster_rows = FALSE,
     cluster_cols = FALSE,
     gaps_row=c(10,20,30,40),
     gaps_col=c(3,6,9),
     cellheight = 6,
     cellwidth = 20,
     border_color=NA,
     fontsize_row = 6,
     main="Genes grouped by categories",
     filename = "TEST.png",
     annotation_row = annotdf,
     annotation_colors = mycolors
)

Which produces:

As you can see, the colors are clearly NOT the Set1 palette I specify, but the default pheatmap colors (you will get the same results removing the annotation_colors line).

So my question here is: How do I specify annotation_colors in pheatmap??

On the other hand, even when I am saving the pheatmap output in a png file, the graphics window keeps popping up, how do I prevent this from happening?

Thanks!

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_SG.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_SG.UTF-8        LC_COLLATE=en_SG.UTF-8    
 [5] LC_MONETARY=en_SG.UTF-8    LC_MESSAGES=en_SG.UTF-8   
 [7] LC_PAPER=en_SG.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_SG.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gplots_3.0.1       RColorBrewer_1.1-2 pheatmap_1.0.8    

loaded via a namespace (and not attached):
 [1] compiler_3.3.1     colorspace_1.2-6   scales_0.4.0       plyr_1.8.3        
 [5] tools_3.3.1        gtable_0.2.0       Rcpp_0.12.7        KernSmooth_2.23-15
 [9] gdata_2.17.0       grid_3.3.1         caTools_1.17.1     bitops_1.0-6      
[13] munsell_0.4.3      gtools_3.5.0

解决方案

  1. To get more colors for more categories you will need to use a different color palette. There are many continuous palettes that will allow you to go well beyond 12 (like with RColorBrewer).

  2. If you are running the code in your MWE as is, the R graphics window should not be popping up. I ran your example and it produced the desired file with out opening a graphics device to the console. Below is my sessioInfo() if you would like to compare. I would try closing all devices (graphics.off()) and then running your heatmap code to see if that solves the problem. I would also check to make sure you have write privileges in the directory you are currently working in???

  3. As @cuttlefish stated in his comment to get the custom Row/Col colors to display the names in the list of colors has to mach the colnames of your annotation dataframe.

For more than 12 colors

mymat <- matrix(rexp(720, rate=.1), ncol=12)
colnames(mymat) <- c(rep("treatment_1", 3), rep("treatment_2", 3), rep("treatment_3", 3), rep("treatment_4", 3))
rownames(mymat) <- paste("gene", 1:dim(mymat)[1], sep="_")

annotdf <- data.frame(row.names = rownames(mymat), 
                      category = rep(paste0("Category_", seq(12)), each=5) )  

newCols <- colorRampPalette(grDevices::rainbow(length(unique(annotdf$category))))
mycolors <- newCols(length(unique(annotdf$category)))
names(mycolors) <- unique(annotdf$category)
mycolors <- list(category = mycolors)

pheatmap(mymat,
         color=greenred(75),
         scale="row",
         cluster_rows = FALSE,
         cluster_cols = FALSE,
         gaps_row=c(5,10,15,20,25,30,35,40,45,50, 55),
         gaps_col=c(3,6,9),
         cellheight = 6,
         cellwidth = 20,
         border_color=NA,
         fontsize_row = 6,
         main="Genes grouped by categories",
         filename = "TEST_12cat.png",
         annotation_row = annotdf,
         annotation_colors = mycolors
)

SessionInfo

R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] pheatmap_1.0.8     RColorBrewer_1.1-2 gplots_3.0.1      

loaded via a namespace (and not attached):
 [1] colorspace_1.3-0   scales_0.4.1       colorRamps_2.3     plyr_1.8.4         tools_3.3.0       
 [6] gtable_0.2.0       Rcpp_0.12.7        KernSmooth_2.23-15 gdata_2.17.0       grid_3.3.0        
[11] caTools_1.17.1     munsell_0.4.3      bitops_1.0-6       gtools_3.5.0    

这篇关于R Pheatmap:更改注释颜色并防止弹出图形窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆