情节中缺少的部分,然后重新出现并在保存时覆盖整个情节? (R,热图 2)

Parts Missing From Plot, That then Reappear and Overwrite The Entire Plot When Saved? (R, Heatmap.2)

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

当我再运行dev.off()命令时,Key和Title会被用来覆盖原来的图吗?

例如,我会制作这样一个情节:

这远非完美。但是当我 运行 dev.off() 关闭设备时(否则会出现许多其他错误):

你在上面看到的是一个非常扭曲的Key和我的'XYZ'标题。

到底为什么要创建两个文件,第一个是我的矩阵,然后用第二个包含我的翻转键和我的标题的文件覆盖它?我跟不上逻辑。

我已经更新了我的 OS、我的 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() 

此外,如果您不是每次都通过使用 dev.off() 创建 PNG 和 enf 开始,它将无法工作。我相信这也可能导致我的困惑,并且可能在绘制热图之后,一旦 dev.off() 命令为 运行,就会绘制一些元素,导致热图被覆盖。

这(用我的矩阵)创建了这个图像。

我所做的是一种非常巧妙的方法来标记我的块,但在我弄清楚如何让 ComplexHeatmap 正常工作之前,我将被困在 Heatmap.2 中使用这样的技巧。