• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      未分类

      • 首页
      • 博客
      • 未分类
      • 环形热图进阶

      环形热图进阶

      • 发布者 weinfoadmin
      • 分类 未分类, 老俊俊的生信笔记
      • 日期 2021年9月10日
      • 评论 0评论

      感谢老俊俊的大力支持。我们会每日跟新,欢迎您关注老俊俊的生信笔记。

      点击上方关注我们






      回顾一下需要搞定的图:

      万恶之源:

      昨天我们基本上可以做出这张图的大部分了,剩下的是 内圈的红色注释 ,今天尝试一下。

      1、构造数据





      我们在 R 里手动构造 200 个基因表达矩阵,锻炼一下你的 R 功底:

      # 加载R包
      library(tidyverse)
      library(reshape2)
      library(ggnewscale)

      # 随机生成5个字母连接的基因名
      sample(LETTERS[1:26],5,replace = T) %>% paste(.,sep = '',collapse = '')

      [1] "XANLV"

      # 写个批量随机生成名字的函数
      add_name <- function(n){
        sample(LETTERS[1:26],5,replace = T) %>% paste(.,sep = '',collapse = '')
      }

      # 生成200个名字
      my_name <- lapply(1:200, add_name) %>% Reduce('rbind',.)

      # 生成200行随机矩阵
      mat <- matrix(rnorm(800),ncol = 4)

      # 转为数据框
      mat <- as.data.frame(mat)

      # 增加行名
      mat$gene_name <- my_name

      # 增加列名
      colnames(mat)[1:4] <- paste('S',1:4,sep = '')

      # 把行名换成数字
      rownames(mat) <- 1:200

      # 查看内容
      head(mat,3)

               S1         S2         S3           S4 gene_name
      1 -1.610566  0.2707304  2.4928545  0.004407928     MRSXW
      2 -1.554155  0.8668774 -0.3788507 -1.169215463     CXFVI
      3  1.441376 -1.0082396 -0.5090956  0.717615026     SOMUN

      # 宽数据转长数据
      cirp <- melt(mat)

      # 查看内容
      head(cirp,3)

        gene_name variable     value
      1     MRSXW       S1 -1.610566
      2     CXFVI       S1 -1.554155
      3     SOMUN       S1  1.441376

      2、简单环形热图





      到这里我们得到了 4 给样本,200 个基因的表达矩阵,然后我们按昨天先绘制一个环形热图开口。

      计算角度:

      # 取一个样本用作标签
      res <- cirp %>% filter(variable == 'S1')

      # 添加文字标签

      # 计算角度
      res$ang <- seq(from = (360/nrow(res)) / 1.5,
                     to = (1.5* (360/nrow(res))) - 360,
                     length.out = nrow(res)) + 80

      # 根据角度确定朝向的方向
      res$hjust <- 0
      res$hjust[which(res$ang < -90)] <- 1
      res$ang[which(res$ang < -90)] <- (180+res$ang)[which(res$ang < -90)]

      绘图:

      # 添加Y轴数值
      cirp$var <- rep(1:200,4)

      # 绘制开口热图
      ggplot(cirp,aes(x = var,y = variable)) +
        geom_tile(aes(fill = value),color = 'white') +
        scale_fill_gradient2(midpoint = 0,
                             low = '#3C8DAD',
                             mid = "white",
                             high = '#FF6767') +
        scale_y_discrete(expand = expansion(mult = c(2,0))) +
        scale_x_discrete(expand = expansion(mult = c(0,0.05))) +
        coord_polar(theta = 'x') +
        theme_void() +
        geom_text(data = res,
                  aes(x = as.numeric(rownames(res)),
                      y = 4.6,
                      label = gene_name, angle = ang, hjust = hjust),
                  size = 2)

      3、添加基因注释





      接下来给基因添加注释来区别哪些基因是哪一类或者突出某一类感兴趣或靶标基因:

      # 添加基因注释
      g_anno <- data.frame(value = c(rep('none',15),rep('target',35),rep('none',10),
                                     rep('target',15),rep('none',20),rep('target',5),
                                     rep('none',50),rep('target',50)))

      # 查看
      table(g_anno)

      g_anno
        none target
          95    105

      先把注释图画出来看看:

      # 注释条形图
      ggplot() +
        # 添加基因注释
        geom_bar(data = g_anno,stat = 'identity',
                 aes(x = 1:200,y = 0.25,fill = value),
                 width = 1,
                 color = NA) +
        scale_fill_manual(name = 'gene annotation',values = c('none' = 'white','target' = '#E8E46E'))

      没啥问题再把四个热图加上看看:

      # 添加热图
      p <- ggplot() +
        # 添加基因注释
        geom_bar(data = g_anno,stat = 'identity',
                 aes(x = 1:200,y = 0.25,fill = value),
                 width = 1,
                 color = NA) +
        scale_fill_manual(name = 'gene annotation',values = c('none' = 'white','target' = '#E8E46E')) +
        # 添加第一个环s1
        # 新增图例
        new_scale("fill") +
        geom_tile(data = cirp[which(cirp$variable == 'S1'),],
                  aes(x = 1:200,y = 1,fill = value),
                  color = 'white') +
        scale_fill_gradient2(midpoint = 0,
                             low = 'green',
                             mid = "black",
                             high = 'red',
                             name = 'ONE') +
        # 添加第一个环s2
        geom_tile(data = cirp[which(cirp$variable == 'S2'),],
                  aes(x = 1:200,y = 2,fill = value),
                  color = 'white') +
        # 添加第一个环s3
        geom_tile(data = cirp[which(cirp$variable == 'S3'),],
                  aes(x = 1:200,y = 3,fill = value),
                  color = 'white') +
        # 添加第一个环s4
        geom_tile(data = cirp[which(cirp$variable == 'S4'),],
                  aes(x = 1:200,y = 4,fill = value),
                  color = 'white') +
        ylim(-3,5)
      p

      看起来没问题,在这里我们要注意以下注释的 y = 0.25 这个参数,用来调整 注释条与热图的相对位置 。

      其次 ylim 参数,越负,环形热图中间就越空,接下来掰弯它,加个基因名:

      # 掰弯,添加基因名
      library(circlize)

      p + coord_polar(theta = 'x') +
        theme_void() +
        geom_text(data = res,
                  aes(x = as.numeric(rownames(res)),
                      y = 4.6,
                      label = gene_name, angle = ang, hjust = hjust),
                  # 给基因名添加颜色
                  color = rand_color(200),
                  size = 2)

      4、添加多层注释





      有时候可能需要对基因添加多个注释,无非是再加一层条形图而已,调换位置就好:

      # 添加多层注释

      # 添加基因注释
      g_anno1 <- data.frame(type = c(rep('important',100),rep('significance',100)))
      g_anno2 <- data.frame(value = c(rep('none',15),rep('target',35),rep('none',10),
                                     rep('target',15),rep('none',20),rep('target',5),
                                     rep('none',50),rep('target',50)))

      # 注释条形图
      p1 <- ggplot() +
        # 添加基因注释1
        geom_bar(data = g_anno1,stat = 'identity',
                 aes(x = 1:200,y = 0.25,fill = type),
                 width = 1,
                 color = NA) +
        # 添加基因注释2
        geom_bar(data = g_anno2,stat = 'identity',
                 aes(x = 1:200,y = -0.25,fill = value),
                 width = 1,
                 color = NA) +
        scale_fill_manual(name = 'gene annotation',
                          values = c('none' = 'white','target' = '#E8E46E',
                                     'important' = '#81B214','significance' = '#E40017'))
      p1

      添加热图检查位置是否正确:

      # 添加热图
      p3 <- p1 + # 添加第一个环s1
        # 新增图例
        new_scale("fill") +
        geom_tile(data = cirp[which(cirp$variable == 'S1'),],
                  aes(x = 1:200,y = 1,fill = value),
                  color = 'white') +
        scale_fill_gradient2(midpoint = 0,
                             low = 'green',
                             mid = "black",
                             high = 'red',
                             name = 'ONE') +
        # 添加第一个环s2
        geom_tile(data = cirp[which(cirp$variable == 'S2'),],
                  aes(x = 1:200,y = 2,fill = value),
                  color = 'white') +
        # 添加第一个环s3
        geom_tile(data = cirp[which(cirp$variable == 'S3'),],
                  aes(x = 1:200,y = 3,fill = value),
                  color = 'white') +
        # 添加第一个环s4
        geom_tile(data = cirp[which(cirp$variable == 'S4'),],
                  aes(x = 1:200,y = 4,fill = value),
                  color = 'white') +
        ylim(-3,5)
      p3

      掰弯整起:

      # 掰弯,开口型
      p3 + coord_polar(theta = 'x') +
        theme_void() + xlim(0,205) +
        geom_text(data = res,
                  aes(x = as.numeric(rownames(res)),
                      y = 4.6,
                      label = gene_name, angle = ang, hjust = hjust),
                  # 给基因名添加颜色
                  color = rand_color(200),
                  size = 2)

      完美。

      5、样本注释





      有时候还需要对样本进行注释,同样的,添加条形图就完事:

      # 注释条形图
      p1 <- ggplot() +
        # 添加基因注释1
        geom_bar(data = g_anno2,stat = 'identity',
                 aes(x = 1:200,y = -0.5,fill = value),
                 width = 1,
                 color = NA) +
        # 添加基因注释2
        geom_bar(data = g_anno1,stat = 'identity',
                 aes(x = 1:200,y = -0.25,fill = type),
                 width = 1,
                 color = NA) +
        scale_fill_manual(name = 'gene annotation',
                          values = c('none' = 'white','target' = '#E8E46E',
                                     'important' = '#81B214','significance' = '#E40017')) +
        # 新增图例
        new_scale("fill") +
        geom_bar(data = s_anno,stat = 'identity',
                 aes(x = -2,y = 1,fill = samp),
                 width = 5,
                 color = NA) +
        scale_fill_manual(name = 'sample annotation',
                          values = c('control' = '#FA26A0','treat' = '#24A19C'))
      p1

      这里需要注意的点还是图层 Y 轴的位置调整 。

      添加热图检查位置是否正确,这里 y 的位置也要稍作修整一下:

      # 添加热图
      p3 <- p1 +
        # 添加第一个环s1
        # 新增图例
        new_scale("fill") +
        geom_tile(data = cirp[which(cirp$variable == 'S1'),],
                  aes(x = 1:200,y = 0.5,fill = value),
                  color = 'white') +
        scale_fill_gradient2(midpoint = 0,
                             low = 'green',
                             mid = "black",
                             high = 'red',
                             name = 'ONE') +
        # 添加第一个环s2
        geom_tile(data = cirp[which(cirp$variable == 'S2'),],
                  aes(x = 1:200,y = 1.5,fill = value),
                  color = 'white') +
        # 添加第一个环s3
        geom_tile(data = cirp[which(cirp$variable == 'S3'),],
                  aes(x = 1:200,y = 2.5,fill = value),
                  color = 'white') +
        # 添加第一个环s4
        geom_tile(data = cirp[which(cirp$variable == 'S4'),],
                  aes(x = 1:200,y = 3.5,fill = value),
                  color = 'white') +
        ylim(-3,5)
      p3

      掰弯整起:

      # 掰弯,开口型
      p3 + coord_polar(theta = 'x') +
        theme_void() + xlim(-5,205) +
        geom_text(data = res,
                  aes(x = as.numeric(rownames(res)),
                      y = 4.3,
                      label = gene_name, angle = ang, hjust = hjust),
                  # 给基因名添加颜色
                  color = rand_color(200),
                  size = 2)

      调整一下参数,优化一下:



      收官!


      代码 我上传到 QQ 群 老俊俊生信交流群 文件夹里。欢迎加入。加我微信我也拉你进 微信群聊 老俊俊生信交流群 哦。

      群二维码:


      老俊俊微信:



      所以今天你学习了吗?

      欢迎小伙伴留言评论!

      点击我留言!

      今天的分享就到这里了,敬请期待下一篇!

      最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!

      如果觉得对您帮助很大,赏杯快乐水喝喝吧!

      推 荐 阅 读




      • circlize 之可视化基因组数据

      • circlize 之 Advanced layout

      • circlize 之 circos.heatmap()

      • circlize 之 Implement high-level circular plots

      • 怎么批量合并 data.frame ?

      • QPCRpro 正式上线!

      • circlize 之 Legends

      • QPCR数据添加 p 值和显著性一次解决!

      • circlize 之 Graphics

      • circlize 之 Introduction

      • circlize 之 Circular layout

      • 鉴定差异翻译效率基因之 deltaTE 下篇

      • 鉴定差异翻译效率基因之 deltaTE 上篇

      • 鉴定差异翻译效率基因之 Riborex

      • purrr 包之 list 处理系列函数

      • purrr 包之 map 系列函数

      • 批量绘制单基因相关性图

      • Y 叔出品:ggfun

      • 神器之 computeMatrix + 绘图

      • Deeptools 神器之 bamCoverage

      • 在线版shiny pheatmap!

      • QPCR数据快速分析和绘图 — by shiny

      • RNA-seq:Salmon 快速定量

      • RNA-seq:Salmon 定量结果差异分析

      • 用R提取代表转录本

      • 画个CNS级别火山图!

      • R Tips :split 函数

      • 什么? R 版 Hisat2

      • R Tips :match 函数

      • conda 安装软件报错

      • MetaProfile on Transcript

      • 提取代表转录本之 gencode

      • 附近含有 m6A 修饰的 Stop Codon 序列提取

      • Introduction of m6A

      • RNA-seq : Hisat2+Stringtie+DESeq2

      • shiny VennDiagram

      • shiny CountToTPM/FPKM

      • 自己模仿画个– m6A distribution on transcript

      • 怎么把 shiny App 做成 exe 文件进行分发

      • shiny-server内网穿透

      • 在线版shiny pheatmap!

      • 用shiny创作在线火山图绘制App

      • circlize 之 Create plotting regions

      • circlize 之 High-level genomic functions

      • R 爬虫之爬取文献信息

      • R 爬虫之爬取公众号图片

      • 跟着 cell 绘制条形堆叠图和分面小提琴图

      • R 爬虫之爬取 NCBI 文献

      • ggplot 分面绘图一网打尽

      • circlize 之 chordDiagram 函数

      • circlize 之 chordDiagram 函数高级用法

      • R 绘制柱形偏差图

      • R 爬虫之爬取文献影响因子

      • R爬虫之 html 简介

      • R 爬虫之爬取 CRAN 官网 R 包信息

      • ZhouLab 星球

      • R爬虫之 html 简介(续)

      • 绘制带连线的柱状堆积图

      • 连线柱状堆积图进阶

      • circlize 绘图小问题解答

      • 批量读取文件一文搞定

      • 保姆级:手把手教你绘制环形基因表达热图

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      奇怪的图例作业
      2021年9月10日

      下一篇文章

      保姆级:手把手教你绘制环形基因表达热图
      2021年9月10日

      你可能也喜欢

      2-1675088548
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      30 1月, 2023
      9-1675131201
      如何快速批量修改 Git 提交记录中的用户信息
      26 1月, 2023
      8-1678501786
      肿瘤细胞通过改变CD8+ T细胞中的丙酮酸利用和琥珀酸信号来调控抗肿瘤免疫应答。
      7 12月, 2022

      留言 取消回复

      要发表评论,您必须先登录。

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?