[关闭]
@agpwhy 2021-12-27T02:21:26.000000Z 字数 1874 阅读 397

王胖的生信笔记第31期:TidyHeatmap

面对一个矩阵数据,该怎么体现?可能一个比较合适的方式就是使用热图。

怎么让热图不这么丑呢?其实使用ComplexHeatmap已经可以做到很好看的热图了。不过调整这里面的一些参数可能比较累。今天就介绍一个方便的包TidyHeatmap

安装和介绍

  1. devtools::install_github("stemangiola/tidyHeatmap")

https://github.com/stemangiola/tidyHeatmap

按照官网介绍,这个包优势主要包括以下四个:

​ 指定列名帮助注释;
​ 利用管道符就可自定义分类;
​ 自动调整标签的大小;
​ 使用Brewer和Viridis内置配色方案;

开始

  1. library(tidyHeatmap)
  2. library(tidyverse)
  3. mtcars_tidy =
  4. mtcars %>%
  5. as_tibble(rownames="Car name") %>%
  6. mutate_at(vars(-`Car name`, -hp, -vs), scale) %>%
  7. gather(Property, Value, -`Car name`, -hp, -vs)

就用R语言自带的数据集mtcars。结合tidyverse的运用,是格式转化为tibble(也是数据框,具体的差别可参考帮助文档https://cran.r-project.org/web/packages/tibble/vignettes/tibble.html,总之可以使得展示更加简洁,运行也更顺畅)。总之当你使用as.data.frame()不顺利的时候,就可以试试as.tibble()

gather函数类似于Excel中的数据透视的功能。总之记住一句话:长数据变宽数据是spread(),宽数据变长数据是gather()。这个可以找推文看看,或者有机会我写一下(我理解的不是最透彻,能用但是说不好)。

总之现在手头有了一批数据了。

作图

数据列名有Car Name, Property, Value, hp。

  1. mtcars_heatmap =
  2. mtcars_tidy %>%
  3. heatmap(`Car name`, Property, Value ) %>%
  4. add_tile(hp)

heatmap里只要指定热图的行,列,值即可。最后还能加个够注释模块。

mtcars_heatmap

是不是还差一个vs没有用?

利用vs做分组。

  1. mtcars_heatmap <- mtcars_tidy %>%
  2. group_by(vs) %>%
  3. heatmap(`Car name`, Property, Value ) %>%
  4. add_tile(hp)

mtcars_heatmap2

美观

  1. mtcars_tidy %>%
  2. heatmap(
  3. `Car name`,
  4. Property,
  5. Value,
  6. palette_value = c("red", "white", "blue")
  7. )

经典蓝红配色

mtcars_heatmap3

  1. mtcars_tidy_groupings =
  2. mtcars_tidy |>
  3. mutate(property_group = if_else(Property %in% c("cyl", "disp"), "Engine", "Other"))

mtcars_heatmap4

这上面0和1你看着不明显咋整?

加上配色

  1. mtcars_tidy_groupings |>
  2. group_by(vs, property_group) |>
  3. heatmap(
  4. `Car name`, Property, Value ,
  5. palette_grouping = list(
  6. c("#66C2A5", "#FC8D62"),
  7. c("#b58b4c", "#74a6aa")
  8. )
  9. ) |>
  10. add_tile(hp)

mtcars_heatmap5

去除legend

  1. tidyHeatmap::pasilla |>
  2. group_by(location, type) |>
  3. heatmap(
  4. .column = sample,
  5. .row = symbol,
  6. .value = `count normalised adjusted`,
  7. show_heatmap_legend = FALSE
  8. ) |>
  9. add_tile(condition, show_legend = FALSE) |>
  10. add_tile(activation, show_legend = FALSE)

mtcars_heatmap6

行列的注释可以改格式

  1. pasilla_plus |>
  2. heatmap(
  3. .column = sample,
  4. .row = symbol,
  5. .value = `count normalised adjusted`
  6. ) |>
  7. add_tile(condition) |>
  8. add_point(activation) |>
  9. add_tile(act) |>
  10. add_bar(size) |>
  11. add_line(age)

mtcars_heatmap7

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注