如何修改由pheatmap包生成的热图的字体族
如何将pheatmap热图的字体族改为Times New Roman
没问题,这其实是个很常见的需求,我给你提供两种亲测有效的方法:
方法1:直接使用pheatmap的fontfamily参数(推荐)
pheatmap函数本身就支持通过fontfamily参数指定所有文本元素(行名、列名、图例文字等)的字体族,这是最直接的方式。只需要在调用pheatmap时加上这个参数即可:
# 生成你的示例数据 d <- data.frame( c(runif(5, min=0, max = 5)), c(runif(5, min=0, max = 5)), c(runif(5, min=0, max = 5)), row.names = c("gene1", "gene2", "gene3", "gene4", "gene5") ) colnames(d) <- c("Day 1", "Day 2", "Day3") # 修改字体为Times New Roman pheatmap(d, fontfamily = "Times New Roman")
方法2:全局设置绘图字体(备选)
如果方法1在你的环境中不生效(比如系统字体名不匹配),可以先通过par()函数全局设置绘图字体,再绘制热图:
# 全局设置字体族 par(family = "Times New Roman") # 绘制热图 pheatmap(d)
注意事项
- Windows/macOS用户:通常直接使用
"Times New Roman"作为字体名就能生效,前提是你的系统已经安装了该字体(这是系统默认字体,一般都有)。 - Linux用户:如果系统没有预装Times New Roman,你需要先安装对应的字体包。比如在Debian/Ubuntu系统中,可以运行以下命令安装兼容字体:
之后可以用sudo apt install fonts-liberation"Liberation Serif"作为字体名替代"Times New Roman",效果几乎一致。 - 如果不确定系统可用的字体名,可以用以下命令查看:
- Windows:
windowsFonts() - Linux:
X11Fonts() - macOS:
quartzFonts()
- Windows:
内容的提问来源于stack exchange,提问作者stephany




