You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在pheatmap主标题的expression中添加多行换行

解决方案:在pheatmap的expression标题中实现多行换行

要在expression()中实现多行标题,不能用普通的\n,得用plotmath语法里的atop()函数,通过**嵌套atop()**可以实现三行甚至更多行的效果,同时保留expression支持的上标、下标、加粗等格式。

修正后的代码

# 构建三行的expression标题:嵌套atop实现多行布局
main <- expression(bold(
  atop(CD4_CD69^"+",
       atop("Reported human signature core methylation genes",
            n[tiles] ~ "= 1999;" ~ n[genes] ~ "= 89")
       )
))   

test <- matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] <- test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] <- test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] <- test[15:20, seq(2, 10, 2)] + 4
colnames(test) <- paste("Test", 1:10, sep="")  
rownames(test) <- c(paste("CGene", 6:10, sep=""), 
                    paste("AGene", 1:5, sep=""), 
                    paste("BGene", 11:15, sep=""), 
                    paste("DGene", 16:20, sep=""))

library(pheatmap)

# 绘制带多行expression标题的热图
p2 <- pheatmap(test, cluster_row=FALSE, main=main)

关键说明

  • atop(a, b):plotmath语法中用于将a放在b上方,实现两行布局;嵌套atop(a, atop(b, c))即可得到三行结构
  • bold():直接包裹整个atop结构,实现整个标题加粗,比嵌套paste更简洁
  • ~:用于在plotmath中添加空格,替代普通空格(普通空格在expression中会被忽略)
  • 完整保留了原需求中的CD4_CD69^"+"上标、n[tiles]/n[genes]下标格式

替代方案:使用stack()函数

如果觉得嵌套atop不够直观,也可以用stack()函数,它支持传入多个元素实现多行堆叠:

main <- expression(bold(
  stack(CD4_CD69^"+",
        "Reported human signature core methylation genes",
        n[tiles] ~ "= 1999;" ~ n[genes] ~ "= 89")
))

内容的提问来源于stack exchange,提问作者Charité Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:27:47