R绘图笔记R语言可视化

Complexheatmap::pheatmap绘图字体设置

2022-05-13  本文已影响0人  myshu

ComplexHeatmap是一个很好用的绘制热图的R包。这篇文章主要介绍Complexheatmap::pheatmap绘图字体设置。
在原函数中没有设置字体的参数。查看有关font的参数比较表如下:
表头为Arguments in pheatmap::pheatmap()Identical settings/arguments in ComplexHeatmap::Heatmap()

参考:https://jokergoo.github.io/2020/05/06/translate-from-pheatmap-to-complexheatmap/
其实我们通过上述比较表可以看出Complexheatmap::pheatmap中一些参数是调用Complexheatmap::Heatmap中的一些参数来调整绘图参数的。
首先,我想到了测试了下直接传入Heatmap的参数到pheatmap,但是其中一些参数如果重新设置会显示冲突。

Error in (function (matrix, col, name, na_col = "grey", color_space = "LAB", :
正式参数"column_names_gp"有多个与之相对应的实际参数

排除了这种方式之外,我测试时发现另一种可行的方法!首先将绘图函数保存到一个变量中,测试代码如下:

library("ComplexHeatmap")
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) = paste("Gene", 1:20, sep = "")

annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)),
  Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")

annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
# Specify colors
ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
p <- pheatmap(test,
        annotation_col = annotation_col, 
        annotation_row = annotation_row,  
        annotation_colors = ann_colors,  
        color = colorRampPalette(c("#0000FF", "#FFFCC8", "#FF0000"))(100), 
        border_color = NA,  
        cellwidth =NA, cellheight = NA, 
        cluster_rows = TRUE,  
        cluster_cols = TRUE,  
        legend = TRUE, 
        main = "jsd diversity distance", 
        fontsize = 10, 
        fontsize_row = 8,  
        fontsize_col = 10, 
        display_numbers = FALSE, number_format = "%.1f", number_color="Black", fontsize_number=4, 
        # display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test))
        angle_col = "90",  # options (0, 45, 90, 270 and 315)
        # show_rownames = TRUE, show_colnames = TRUE
        annotation_legend = TRUE
)

查看p可以看出是一个S4对象,我们可以找下其中可以设置字体的参数,可以根据参数的名称来找

行标题字体设置示例
这里我指定参数的代码如下
p@row_title_param$gp$fontfamily = "serif"  # 行标题

其他的参数也是类似的。

font_family = "serif"
p@column_names_param$gp$fontfamily = font_family  # 列名称
p@row_names_param$gp$fontfamily = font_family   # 行名称
p@row_title_param$gp$fontfamily = font_family  # 行标题p@column_title_param$gp$fontfamily = font_family  # 列标题(图标题)

除了,legend设置的参数在设置完成后可能会有报错,需要指定gpar来设置:

p@matrix_legend_param = gpar(title_gp = gpar(fontfamily = font_family), labels_gp =  gpar(fontfamily = font_family))

最后是行分类,和列分类的字体(包括对应的图例字体)

p@top_annotation@anno_list[[coltype]]@name_param$gp$fontfamily = font_family  # 列分类标题
p@top_annotation@anno_list[[coltype]]@legend_param = gpar(title_gp = gpar(fontfamily = font_family), labels_gp =  gpar(fontfamily = font_family))

p@left_annotation@anno_list[[rowtype]]@name_param$gp$fontfamily = font_family  # 行分类标题
p@left_annotation@anno_list[[rowtype]]@legend_param = gpar(title_gp = gpar(fontfamily = font_family), labels_gp =  gpar(fontfamily = font_family))

最后来show一下对比图吧


修改前:默认字体
修改后:Times字体

参考链接:

上一篇下一篇

猜你喜欢

热点阅读