【R】pheatmap行/列长文本注释(annotation_row/col)后热图的边框(border_color)消失?


用了这么久pheatmap包绘热图,自认为很娴熟了。但今天遇到了一件诡异的事,折腾了我一两个小时,记录备忘。

不加行注释时正常显示

pheatmap(pld,
         cluster_rows = F,
         cluster_cols = F,
         gaps_col = c(5,10,15,20),
         annotation_col = annotaion_col,
         color = colorRampPalette(colors = c("white","red"))(100))

这时一切正常,border_color我根本不用去动它。
image.png

加上行注释后热图边框消失了

pheatmap(pld,
         cluster_rows = F,
         cluster_cols = F,
         gaps_col = c(5,10,15,20),
        # border_color = "grey60",
         annotation_col = annotaion_col,
         annotation_row = annotation_row,
         color = colorRampPalette(colors = c("white","red"))(100))

加上行注释后,边框莫名消失了。要不是因为影响直观比较,我也不会去折腾。
image.png
不管我怎么设置border_color,长宽比输出等操作都不管用。

甚至修改了下源码,把行名放在了左边展示还是不管用。

尝试找原因

列注释能正常显示,行注释时边框消失了。列注释和行注释里代码基本是一样的,在我的数据里,列注释比行注释要短很多,分析是不是文本长度之故?

  annotation_row$test <- c(rep(c("test1","test2"),each=36),"test3")
  pheatmap(pld2,cluster_rows = F,cluster_cols = F,
           gaps_col = c(5,10,15,20),
           annotation_col = annotaion_col[-1],
           annotation_row = annotation_row[3],
           # border_color = "black",
           color = colorRampPalette(colors = c("white","red"))(100))

把长长的行注释改为简短的文本后,边框还真恢复了。说明就是这个原因,可能是文本太长以及样本太多而将边框压缩掉了,但这不能通过图片长宽来解决。那么pheatmap有什么解决之道吗?
image.png

通过调节单元格长宽设置来解决

pheatmap中有cellwidth 和cellheight 两个参数来调节单元格的长宽,通过改变这两个值可以在长文本注释情况下使边框重现。

pheatmap(pld,
         cluster_rows = F,
         cluster_cols = F,
         angle_col = 90,
         annotation_colors = ann_colors,
         gaps_col = c(5,10,15,20),
         #border_color = "grey60",
         annotation_col = annotaion_col,
         annotation_row = annotation_row,
         cellwidth = 11,cellheight = 9,  #调节,解决热图区边框不见的问题
         color = colorRampPalette(colors = c("white","red"))(100))

image.png

这两个参数默认NA,不知具体数值,只有通过不断微调才能达到自己想要的效果。

感谢这个问题,让我找到解决方法:https://geek-qa.imtqy.com/questions/384839/index.html

posted @ 2022-04-17 22:35  生物信息与育种  阅读(1866)  评论(0编辑  收藏  举报