【问题标题】:Parts Missing From Plot, That then Reappear and Overwrite The Entire Plot When Saved? (R, Heatmap.2)情节中缺少的部分,然后重新出现并在保存时覆盖整个情节? (R,热图.2)
【发布时间】:2019-09-05 12:01:01
【问题描述】:

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

然后当我运行dev.off() 命令时,然后使用 Key 和 Title 覆盖原始图形?

例如,我将制作这样的情节:

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

您在上面看到的是一个非常扭曲的 Key 和我的“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()

我非常感谢您对此问题的任何见解。

【问题讨论】:

    标签: r matrix plot device heatmap


    【解决方案1】:

    我相信这是因为我有不止 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 这样的黑客卡住。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      相关资源
      最近更新 更多