R语言如何绘制交互热图

前言

本篇是R包d3heatmap绘制交互式热图的教程,普通热图的详细解释和普通热图的画法见:https://www.r2omics.cn/docs/gallery/omicsChart/heatmap/

什么是交互热图

动态交互热图最后生一个可以交互的html格式的结果。示例结果见末尾。

绘图前的数据准备

数据同普通的热图,来源一般是搜库结果定量表。包含2个维度的数据,一般情况下,每一行是一个基因,每一列是一个样本。

但需要注意的是,交互式热图不宜数据量过大,否则会很卡。

demo数据可以从这下载:https://www.r2omics.cn/res/demodata/heatmapD3.txt

# A tibble: 50 x 7
   X      Sample1 Sample2 Sample3 Sample4 Sample5 Sample6
   <chr>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 Gene1   1.28    1.40    1.07   0.880    0.829   0.695 
 2 Gene2   0.0195  0.0271  0.0147 0.0442   0.0574  0.0594
 3 Gene3   2.40    3.08    2.26   2.42     2.49    2.44  
 4 Gene4   0.0832  0.143   0.157  0.0833   0.173   0.0685
 5 Gene5   0.0274  0.0102  0.0206 0.0500   0.0732  0.0690
 6 Gene6   0.0154  0.0145  0.0135 0.00894  0.0228  0.0121
 7 Gene7   0.0272  0.0620  0.0839 0.753    0.667   0.692 
 8 Gene8   0.503   0.425   0.470  0.534    0.325   0.482 
 9 Gene9   0.855   1.01    0.832  1.21     0.923   1.06  
10 Gene10  2.37    2.13    1.96   0.963    1.31    1.32  
# i 40 more rows

R语言如何绘制交互热图

# 代码来源:https://www.r2omics.cn/
library(d3heatmap) # 安装方式 devtools::install_github("talgalili/d3heatmap")
library(magrittr)  # 提供管道符

# 读取热图数据文件
df = read.delim("https://www.r2omics.cn/res/demodata/heatmapD3.txt", #文件名称 注意文件路径,格式
                header = T,    # 是否有标题
                sep = "\t",    # 分隔符是Tab键
                row.names = 1, # 指定第一列是行名
                fill=T)        # 是否自动填充,一般选择是

# 绘图
heatmapPlot =  d3heatmap(df, 
          anim_duration = 100, # 缩放动画的时间,ms
          dendrogram = "both", # 聚类
          scale = "row",       # 归一化
          show_grid = T,       # 显示单元格边界
          print.values = T,    # 显示数值
          col = c('#0000ff','#ffffff','#ff0000'),  # 颜色
          key = TRUE,          # 图例 
          key.title = "Legend" # 图例名称
          ) %>%
  hmAxis("x", title = "Xname", location = 'bottom',font.size=10) %>%         # 调整x轴标签
  hmAxis("y", title = "Yname", location = 'right') %>%                       # 调整y轴标签
  hmCells(font.size = 8, color = 'blue') %>%                                 # 调整单元格里的字体
  hmLegend(show = T, title = "Legend", location = "tl",density="histogram")  # 调整图例
heatmapPlot 
# 保存网页格式的结果
# htmlwidgets::saveWidget(heatmapPlot,file = "heatmap.html")