如何修复热图图例超出边距?

问题描述

我想增加热图的字体大小,但我发现当图例名称长而图例组名称短时,图例很容易超出边距。例子如下:

test = matrix(rnorm(200),20,10)
test[1:10,seq(1,10,2)] = test[1:10,2)] + 3
test[11:20,seq(2,2)] = test[11:20,2)] + 2
test[15:20,2)] = test[15:20,2)] + 4
colnames(test) = paste("Test",1:10,sep = "")
rownames(test) = paste("Gene",1:20,sep = "")

annotation_col = data.frame(
  C = factor(rep(c("longCT1","longCT2"),5))
)
rownames(annotation_col) = paste("Test",sep = "")
pheatmap::pheatmap(test,annotation_col = annotation_col,cluster_cols = F,scale = "row",fontsize = 20)

可以看到图例名称C较短,而lenengned组名称longCTx较长,设置大字体时,会超出边距:

enter image description here

那么在这种情况下,当我想增加热图的字体大小时如何解决?

解决方法

一个简单的解决方案是在组名的末尾添加一些空格。见下文:

# Add blanks to groups names
annotation_col = data.frame(
  C = factor(rep(c("longCT1    ","longCT2    "),5))
)
rownames(annotation_col) = paste("Test",1:10,sep = "")
pheatmap::pheatmap(test,annotation_col = annotation_col,cluster_cols = F,scale = "row",fontsize = 20)

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...