R语言学习笔记之热图绘制


640?wx_fmt=gif

作者简介

taoyan:R语言中文社区特约作家,伪码农,R语言爱好者,爱开源。

个人博客: https://ytlogos.github.io/

640?wx_fmt=gif

640?wx_fmt=jpeg

简介

本文将绘制静态与交互式热图,需要使用到以下R包和函数:
heatmap():用于绘制简单热图的函数
heatmap.2():绘制增强热图的函数
d3heatmap:用于绘制交互式热图的R包
ComplexHeatmap:用于绘制、注释和排列复杂热图的R&bioconductor包(非常适用于基因组数据分析)

数据准备

使用R内置数据集mtcars

   df <- as.matrix((scale(mtcars)))#归一化、矩阵化


使用基本函数绘制简单简单热图

主要是函数heatmap(x, scale="row")

x:数据矩阵

scale:表示不同方向,可选值有:row, columa, none

Default plotheatmap(df, scale = “none”)

640?wx_fmt=png

Use custom colors

col <- colorRampPalette(c("red", "white", "blue"))(256)

heatmap(df, scale = "none", col=col)

 640?wx_fmt=png

#Use RColorBrewer color palette names

library(RColorBrewer)col <- colorRampPalette(brewer.pal(10, "RdYlBu"))(256)#自设置调色板dim(df)#查看行列数

## [1] 32 11   

heatmap(df, scale = "none", col=col, RowSideColors = rep(c("blue", "pink"), each=16),

ColSideColors = c(rep("purple", 5), rep("orange", 6)))

#参数RowSideColors和ColSideColors用于分别注释行和列颜色等,可help(heatmap)详情

640?wx_fmt=png

增强热图

函数heatmap.2()
在热图绘制方面提供许多扩展,此函数包装在gplots包里。

library(gplots)heatmap.2(df, scale = "none", col=bluered(100),

trace = "none", density.info = "none")#还有其他参数可参考help(heatmap.2())

 640?wx_fmt=png  

交互式热图绘制

d3heatmap包可用于生成交互式热图绘制,可通过以下代码生成: 

if (!require("devtools"))

install.packages("devtools")

devtools::install_github("rstudio/d3heatmap")

   

函数d3heatmap()用于创建交互式热图,有以下功能:

1、将鼠标放在感兴趣热图单元格上以查看行列名称及相应值

2、可选择区域进行缩放

library(d3heatmap)d3heatmap(df, colors = "RdBu", k_row = 4, k_col = 2)

   

k_row、k_col分别指定用于对行列中树形图分支进行着色所需组数。进一步信息可help(d3heatmap())获取。


使用dendextend包增强热图

软件包dendextend可以用于增强其他软件包的功能

library(dendextend)# order for rows

Rowv <- mtcars %>% scale %>% dist %>%

hclust %>% as.dendrogram %>%

set("branches_k_color", k = 3) %>%

set("branches_lwd", 1.2) %>% ladderize# Order for columns#

We must transpose the data

Colv <- mtcars %>% scale %>% t %>% dist %>%

hclust %>% as.dendrogram %>%

set("branches_k_color", k = 2, value = c("orange", "blue")) %>% set("branches_lwd", 1.2) %>% ladderize

增强heatmap()函数

---

heatmap(df, Rowv = Rowv, Colv = Colv, scale = "none")

   640?wx_fmt=png

#增强heatmap.2()函数

heatmap.2(df, scale = "none", col = bluered(100), Rowv = Rowv, Colv = Colv, trace = "none", density.info = "none")

  640?wx_fmt=png

#增强交互式绘图函数

d2heatmap()d3heatmap(scale(mtcars), colors = "RdBu", Rowv = Rowv, Colv = Colv)

   

绘制复杂热图

ComplexHeatmap包是bioconductor包,用于绘制复杂热图,它提供了一个灵活的解决方案来安排和注释多个热图。它还允许可视化来自不同来源的不同数据之间的关联热图。可通过以下代码安装:

if (!require("devtools")) install.packages("devtools")

devtools::install_github("jokergoo/ComplexHeatmap")

   

ComplexHeatmap包的主要功能函数是Heatmap(),格式为:Heatmap(matrix, col, name)

  matrix:矩阵

  col:颜色向量(离散色彩映射)或颜色映射函数(如果矩阵是连续数)

  name:热图名称   

library(ComplexHeatmap)

Heatmap(df, name = "mtcars")

640?wx_fmt=png      

#自设置颜色

  • 17
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值