零件从图丢失,然后在存储时重新出现并覆盖整个图? (R,Heatmap.2) [英] Parts Missing From Plot, That then Reappear and Overwrite The Entire Plot When Saved? (R, Heatmap.2)

查看:128
本文介绍了零件从图丢失,然后在存储时重新出现并覆盖整个图? (R,Heatmap.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用heatmap.2创建图,但是,保存到源文件夹的初始图缺少键和标题.

然后我运行dev.off()命令时,然后使用键"和标题"来覆盖原始图形吗?

例如,我将生成如下图:

这还远非完美.但是然后当我运行dev.off()关闭设备时(否则会发生其他错误):

您在上面看到的是一个非常变形的键和我的"XYZ"标题.

为什么要创建两个文件,首先使用我的矩阵创建一个文件,然后使用包含翻转键和标题的第二个文件覆盖它?我不能遵循逻辑.

我已经更新了操作系统,R,RStudio版本,所有软件包和未安装的RStudio.似乎无济于事.

如果您想尝试复制我的错误,下面是示例矩阵:

structure(c(1, 4, 5, 3, 3, 4, 6, 1, 7, 5, 5, 4, 4, 8, 1, 3, 9, 
2, 2, 9, 3, 1, 3, 4, 4, 5, 5, 5, 1, 4, 4, 3, 3, 3, 9, 1), .Dim = c(6L, 
6L))

这是我用来绘制示例数据的脚本.您需要提供一个SourceDir,并确保将矩阵分配给名称"Matrix".

if (!require("gplots")) {
  install.packages("gplots", dependencies = TRUE)
  library(gplots)
}
if (!require("RColorBrewer")) {
  install.packages("RColorBrewer", dependencies = TRUE)
  library(RColorBrewer)
}

my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 399)


transition
    col_breaks = c(seq(0,1,length=100),    #white 'snow'
                   seq(2,4,length=100), # for yellow
                   seq(5,7,length=100), # for orange 'darkorange'
                   seq(8,9,length=100))    # for red


png(paste(SourceDir, "Heatmap_Test.png"),      
    width = 5*1000,       
    height = 5*1000,
    res = 300,           
    pointsize =15)

heatmap.2(Matrix,
          main =  paste("XYZ"), 
          notecol="black",
          key = "true" ,
          colsep = c(3, 6, 9),
          rowsep = c(3, 6, 9),
          labCol = NULL,
          labRow = NULL,
          sepcolor="white",
          sepwidth=c(0.08,0.08),
          density.info="none",  
          trace="none",         
          margins=c(1,1),     
          col=my_palette,       
          breaks=col_breaks,    
          dendrogram="none",     
          RowSideColors = c(rep("blue", 3), rep("orange", 3)),
          ColSideColors = c(rep("blue", 3), rep("orange", 3)),
          srtCol = 0 ,        
          asp = 1 ,         
          adjCol = c(NA, 0) , 
          adjRow = c(0, NA) , 
          #keysize =  2 ,  
          Colv = FALSE ,      
          Rowv =  FALSE ,    
          key.xlab = paste("Correlation") , 
          cexRow = (1.8) , 
          cexCol = (1.8) , 
          notecex = (1.5) , 
          lmat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) , 
          #par(ColSideColors = c(2,2)),
          lhei = c(1, 1, 3, 1) , 
          lwid = c(1, 1, 3, 1)) 

dev.off()

我非常感谢您对这个问题有任何见识.

解决方案

我认为这是由于我不仅拥有1到4个元素,因为我添加的彩色行被算作必须添加的其他元素.排列在显示矩阵中.

因此:

mat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) , 
lhei = c(1, 1, 3, 1) , 
lwid = c(1, 1, 3, 1))

不再切黄油.经过一番努力,我终于设法使以下布局生效(根据我的实际数据,而不是我的示例数据).

lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) , 
lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
lwid = c(0.4, 0.16, 3, 0.4),

请注意包含元素5和6.

所以我的最终命令如下所示(请注意,还有许多其他更改,但是真正的进步是在我添加了5和6之后发生的):

png(paste(SourceDir, "XYZ.png"),         
    width = 5*1500,        
    height = 5*1500,
    res = 300,            # 300 pixels per inch
    pointsize =30)        

heatmap.2(CombinedMtx,
          main =  paste("XYZ"), # heat map title
          notecol="black",
          key = "true" ,# change font color of cell labels to black
          colsep = c(6, 12, 18),
          labCol = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
          rowsep = c(6, 12, 18),
          labRow = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
          sepcolor="white",
          sepwidth=c(0.08,0.08),
          density.info="none",  
          trace="none",         
          margins=c(1,1),     
          col=my_palette,      
          breaks=col_breaks,   
          dendrogram="none",     
          RowSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
          ColSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
          srtCol = 0 ,        
          asp = 1 ,         
          adjCol = c(1.5, -61.5) , 
          adjRow = c(0, -1.38), 
          offsetRow = (-59.5),
          keysize =  2 ,  
          Colv = FALSE ,     
          Rowv =  FALSE ,    
          key.xlab = NA , 
          key.ylab = NULL ,
          key.title = NA ,
          cexRow = (1.6) , 
          cexCol = (1.6) , 
          notecex = (1.5) , 
          cex.main = (20),
          lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) , 
          #par(ColSideColors = c(2,2)),
          lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
          lwid = c(0.4, 0.16, 3, 0.4),
          symkey = any(0.5 < 0, na.rm=FALSE) || col_breaks,
          key.par=list(mar=c(3.5,0, 1.8,0) )  #tweak specific key paramters
)
dev.off() 

此外,如果您不是每次都通过创建PNG来启动,而每次都不是通过使用dev.off()来启动,那么它将无法正常工作.我相信这也可能加剧了我的困惑,并且可能在绘制热图之后,一旦运行dev.off()命令,就会绘制一些元素,从而导致覆盖热图.

这(和我的矩阵一起)创建了这张图片.

我所做的是一种真正的标记块的游戏方式,但是在我弄清楚如何使ComplexHeatmap正常工作之前,我会被Heatmap.2这样的黑客所困扰.

I'm using heatmap.2 to create a plot, however, the initial plot that is saved to my source folder is missing a key and title.

When I then run the dev.off() command, the Key and the Title are then used to overwrite the original graph?

For instance, I will produce a plot like this:

Which is far from perfect. But then when I run the dev.off() to close the device (otherwise a host of other errors ensue):

What you are looking at above is a very distorted Key and my 'XYZ' title.

Why on earth is it creating two files, firstly the one with my matrix, and then overwriting this with a second file containing my flipping key and my title? I cannot follow the logic.

I've updated my OS, my version of R, RStudio, all my packages and unistalled RStudio. Nothing seems to help.

If you'd like to try and replicate my error here is the example matrix:

structure(c(1, 4, 5, 3, 3, 4, 6, 1, 7, 5, 5, 4, 4, 8, 1, 3, 9, 
2, 2, 9, 3, 1, 3, 4, 4, 5, 5, 5, 1, 4, 4, 3, 3, 3, 9, 1), .Dim = c(6L, 
6L))

And this is the script I'm using to plot my example data. You'll need to provide a SourceDir and make sure you assign the matrix to the name "Matrix".

if (!require("gplots")) {
  install.packages("gplots", dependencies = TRUE)
  library(gplots)
}
if (!require("RColorBrewer")) {
  install.packages("RColorBrewer", dependencies = TRUE)
  library(RColorBrewer)
}

my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 399)


transition
    col_breaks = c(seq(0,1,length=100),    #white 'snow'
                   seq(2,4,length=100), # for yellow
                   seq(5,7,length=100), # for orange 'darkorange'
                   seq(8,9,length=100))    # for red


png(paste(SourceDir, "Heatmap_Test.png"),      
    width = 5*1000,       
    height = 5*1000,
    res = 300,           
    pointsize =15)

heatmap.2(Matrix,
          main =  paste("XYZ"), 
          notecol="black",
          key = "true" ,
          colsep = c(3, 6, 9),
          rowsep = c(3, 6, 9),
          labCol = NULL,
          labRow = NULL,
          sepcolor="white",
          sepwidth=c(0.08,0.08),
          density.info="none",  
          trace="none",         
          margins=c(1,1),     
          col=my_palette,       
          breaks=col_breaks,    
          dendrogram="none",     
          RowSideColors = c(rep("blue", 3), rep("orange", 3)),
          ColSideColors = c(rep("blue", 3), rep("orange", 3)),
          srtCol = 0 ,        
          asp = 1 ,         
          adjCol = c(NA, 0) , 
          adjRow = c(0, NA) , 
          #keysize =  2 ,  
          Colv = FALSE ,      
          Rowv =  FALSE ,    
          key.xlab = paste("Correlation") , 
          cexRow = (1.8) , 
          cexCol = (1.8) , 
          notecex = (1.5) , 
          lmat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) , 
          #par(ColSideColors = c(2,2)),
          lhei = c(1, 1, 3, 1) , 
          lwid = c(1, 1, 3, 1)) 

dev.off()

I'd really appreciate any insight into this problem.

解决方案

I believe this resulted from the fact that I had more than just elements 1 to four, as the coloured rows I had added counted as additional elements that had to be arranged in the display matrix.

As such:

mat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) , 
lhei = c(1, 1, 3, 1) , 
lwid = c(1, 1, 3, 1))

No longer cut the butter. After much ado, I finally managed to get the following layout to work (on my actual data, not my example data).

lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) , 
lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
lwid = c(0.4, 0.16, 3, 0.4),

Notice the inclusion of elements 5 and 6.

So my final command looks like this (note that there will be many other changes but the real progress happened once I added in 5 and 6):

png(paste(SourceDir, "XYZ.png"),         
    width = 5*1500,        
    height = 5*1500,
    res = 300,            # 300 pixels per inch
    pointsize =30)        

heatmap.2(CombinedMtx,
          main =  paste("XYZ"), # heat map title
          notecol="black",
          key = "true" ,# change font color of cell labels to black
          colsep = c(6, 12, 18),
          labCol = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
          rowsep = c(6, 12, 18),
          labRow = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
          sepcolor="white",
          sepwidth=c(0.08,0.08),
          density.info="none",  
          trace="none",         
          margins=c(1,1),     
          col=my_palette,      
          breaks=col_breaks,   
          dendrogram="none",     
          RowSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
          ColSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
          srtCol = 0 ,        
          asp = 1 ,         
          adjCol = c(1.5, -61.5) , 
          adjRow = c(0, -1.38), 
          offsetRow = (-59.5),
          keysize =  2 ,  
          Colv = FALSE ,     
          Rowv =  FALSE ,    
          key.xlab = NA , 
          key.ylab = NULL ,
          key.title = NA ,
          cexRow = (1.6) , 
          cexCol = (1.6) , 
          notecex = (1.5) , 
          cex.main = (20),
          lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) , 
          #par(ColSideColors = c(2,2)),
          lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
          lwid = c(0.4, 0.16, 3, 0.4),
          symkey = any(0.5 < 0, na.rm=FALSE) || col_breaks,
          key.par=list(mar=c(3.5,0, 1.8,0) )  #tweak specific key paramters
)
dev.off() 

Also, if you don't start each time by creating the PNG and enf each time by using dev.off() it won't work. I believe this might also have been contribution to my confusion, and potentially after drawing the heatmap, some elements were being drawn once the dev.off() command was run, causing the heatmap to be overwritten.

This (with my matrix) creates this image.

What I have done is a really gammy way of labelling my blocks but until I can work out how to get ComplexHeatmap working properly I'll be stuck using hacks like this with Heatmap.2.

这篇关于零件从图丢失,然后在存储时重新出现并覆盖整个图? (R,Heatmap.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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