• 主页
  • 课程

    关于课程

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

    同等学历教学

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

      关于课程

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

      同等学历教学

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

      未分类

      • 首页
      • 博客
      • 未分类
      • ggcor 的环形热图

      ggcor 的环形热图

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

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


      点击上方关注我们




      今天学习一下 ggcor 如何绘制环形热图,ggcor 是一款强大的绘制 相关性热图 的 R 包,网上教程很多,大家感兴趣搜索即可。

      1、构造数据

      这里还是使用之前的方法:

      # 构造热图数据

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

      # 生成25个名字
      my_name <- lapply(1:25, add_name) %>% Reduce('rbind',.)
      # 生成25行随机矩阵
      mat <- matrix(rnorm(125),ncol = 5)
      # 转为数据框
      mat <- as.data.frame(mat)
      # 增加行名
      rownames(mat) <- my_name
      # 增加列名
      colnames(mat)[1:5] <- paste('S',1:5,sep = '')
      # 查看内容
      head(mat,3)

                    S1         S2         S3         S4          S5
      DADZX -0.8865726  0.2218578 -2.3218800 -1.4207947 -2.30872404
      RSXQH -1.1686315 -1.1954912 -0.3923487 -0.6683483 -0.24387706
      SYBZO -0.9267627 -1.0018120  0.8926237 -1.4758484  0.06554279

      2、一般样式热图

      我们需要使用 cor_tbl 函数把数据转换成 ggcor 可以识别的格式,cluster 参数可以指定为 col、row、all、none、TRUE、FALSE,来显示聚类的方式:

      # 转换格式

      # 行列不聚类
      da <- cor_tbl(mat,cluster = F)
      p1 <- quickcor(da,circular = F,
               # 基因名位置
               axis.y.position = 'right') +
        # 单元格边框线颜色
        geom_colour(colour = 'black') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC'))

      # 行列聚类
      da <- cor_tbl(mat,cluster = T)
      p2 <- quickcor(da,circular = F,
                     # 基因名位置
                     axis.y.position = 'right') +
        # 单元格边框线颜色
        geom_colour(colour = 'black') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC'))

      # 拼图
      p1 | p2

      我们看看转换格式的数据长啥样:

      # 查看转换格式数据
      head(da,3)

      # A tibble: 3 x 5
        .row.names .col.names      r .row.id .col.id
        <chr>      <chr>       <dbl>   <int>   <int>
      1 BIFVU      S1         -0.341      25       1
      2 PPYEX      S1         -0.559      24       1
      3 IRYIV      S1          0.394      23       1

      可以看到基因的表达值在 r 这一列,基因名在 .row.names 列,样本名在 .col.names 列,感觉就像是做了一个 宽数据转长数据 的操作。

      我们还可以添加 聚类树图 ,使用 anno_col_tree 和 anno_row_tree 函数,注意,添加聚类树时在转换格式函数里的 cluster 参数要对应:

      # 添加聚类树

      p3 <- quickcor(da,circular = F,
                     # 基因名位置
                     axis.y.position = 'right') +
        # 单元格边框线颜色
        geom_colour(colour = 'black') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC')) +
        # 列聚类树,height为树高度
        anno_col_tree(height = 0.05) +
        # 行聚类树,pos为树的位置
        anno_row_tree(pos = 'left')

      p3

      还可以给聚类树不同的 分枝添加颜色 :

      # 给树添加颜色
      p4 <- quickcor(da,circular = F,
                     # 基因名位置
                     axis.y.position = 'right') +
        # 单元格边框线颜色
        geom_colour(colour = 'black') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC')) +
        # 列聚类树,height为树高度
        anno_col_tree(height = 0.05,
                      # 树分支颜色
                      bcols = c('#FB9300','#0A81AB')) +
        # 行聚类树,pos为树的位置
        anno_row_tree(pos = 'left',
                      # 树分支颜色
                      bcols = rainbow(5))

      p4

      添加 行列注释 :

      # 添加行列注释
      p5 <- quickcor(da,circular = F,
                     # 基因名位置
                     axis.y.position = 'right') +
        # 单元格边框线颜色
        geom_colour(colour = 'black') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC')) +
        # 列注释
        anno_hc_bar(k = 2,fill = c('#FB9300','#0A81AB'),
                    pos = 'top') +
        # 列聚类树,height为树高度
        anno_col_tree(height = 0.05,
                      # 树分支颜色
                      bcols = c('#0A81AB','#FB9300')) +
        # 行聚类树,pos为树的位置
        anno_row_tree(pos = 'left',
                      # 树分支颜色
                      bcols = rainbow(5)) +
        # 行注释
        anno_hc_bar(k = 5,fill = rainbow(5),
                    pos = 'left')
      p5

      添加多层注释:

      # 添加多层注释
      library(circlize)
      p6 <- quickcor(da,circular = F,
                     # 基因名位置
                     axis.y.position = 'left') +
        # 单元格边框线颜色
        geom_colour(colour = 'black') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC')) +
        # 列注释
        anno_hc_bar(k = 5,fill = rand_color(5),pos = 'bottom') +
        anno_hc_bar(k = 5,fill = rand_color(5),pos = 'top') +
        anno_hc_bar(k = 3,fill = rand_color(3),pos = 'bottom') +
        anno_row_tree(bcols = rainbow(5),pos = 'left') +
        # 行注释
        anno_hc_bar(k = 5,fill = rainbow(5),pos = 'right') +
        anno_hc_bar(k = 8,fill = rand_color(8),pos = 'right',width = 0.7) +
        anno_hc_bar(k = 20,fill = rand_color(20),pos = 'right',width = 0.7)

      p6

      没有图例,弄着玩就行,要绘制热图最好用 pheatmap 和 complexheatmap 专业的包吧。

      3、环形热图

      有了上面一些基础,我们试着画一下环形热图:

      # 环形热图
      da <- cor_tbl(mat,cluster = T)

      p7 <- quickcor(da,circular = T,cluster = T,
               open = 45, # 缺口大小
               # 内圈外圈比例
               outer = 0.5,inner = 1.5) +
        # 单元格边框线颜色
        geom_colour(colour = 'black') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC')) +
        # 添加聚类树
        anno_col_tree() +
        anno_row_tree() +
        # 基因名
        set_p_yaxis() +
        # 样本名
        set_p_xaxis()

      给树和标签添加一些颜色:

      p8 <- quickcor(da,circular = T,cluster = T,grid.colour = 'white',
                     open = 45, # 缺口大小
                     # 内圈外圈比例
                     outer = 0.2,inner = 2) +
        # 单元格边框线颜色
        geom_colour(colour = 'white') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC')) +
        # 添加聚类树
        anno_col_tree(bcols = rand_color(5)) +
        anno_row_tree(bcols = rand_color(10)) +
        # 基因名
        set_p_yaxis(bcols = rand_color(10)) +
        # 样本名
        set_p_xaxis(bcols = rand_color(5))

      p7 + p8

      同样也可给环形热图添加注释:

      # 环形热图行列注释
      quickcor(da,circular = T,cluster = T,grid.colour = 'white',
               open = 45, # 缺口大小
               # 内圈外圈比例
               outer = 0.5,inner = 1) +
        # 单元格边框线颜色
        geom_colour(colour = 'white') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
        # 更改图例名称
        guides(fill = guide_colorbar(title = 'log2FC')) +
        # 列注释
        anno_hc_bar(k = 2,fill = rand_color(2),pos = 'top',height = 0.3) +
        anno_hc_bar(k = 3,fill = rand_color(3),pos = 'top',height = 0.5) +
        anno_hc_bar(k = 5,fill = rand_color(5),pos = 'top',height = 0.2) +
        # 添加聚类树
        anno_col_tree(bcols = rand_color(5),height = 0.15) +
        anno_hc_bar(k = 15,fill = rand_color(15),pos = 'left',width = 0.5) +
        anno_hc_bar(k = 10,fill = rand_color(10),pos = 'left',width = 0.5) +
        anno_row_tree(bcols = rand_color(8)) +

        # 样本名
        set_p_xaxis(bcols = rand_color(5)) +
        # 基因名
        set_p_yaxis()

      还使用 anno_row_heat 可以在外圈添加热图:

      # 添加热图
      p9 <- quickcor(da,circular = T,cluster = T,grid.colour = 'white',
                     open = 45, # 缺口大小
                     # 内圈外圈比例
                     outer = 2,inner = 1) +
        # 单元格边框线颜色
        geom_colour(colour = 'white') +
        # 自定义填充颜色
        scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red',name = 'log2FC') +
        # 更改图例名称
        # guides(fill = guide_colorbar(title = 'log2FC')) +
        # 列注释
        anno_hc_bar(k = 2,fill = rand_color(2),pos = 'top',height = 0.3) +
        anno_hc_bar(k = 3,fill = rand_color(3),pos = 'top',height = 0.5) +
        anno_hc_bar(k = 5,fill = rand_color(5),pos = 'top',height = 0.2) +
        # 添加聚类树
        anno_col_tree(bcols = rand_color(5),height = 0.15) +
        anno_hc_bar(k = 15,fill = rand_color(15),pos = 'left',width = 0.5) +
        anno_hc_bar(k = 10,fill = rand_color(10),pos = 'left',width = 0.5) +
        anno_row_tree(bcols = rand_color(8)) +

        # 样本名
        set_p_xaxis(bcols = rand_color(5))

      p9 + anno_row_heat(da,aes(fill = r),width = 0.8,
                         geom = 'tile') +
        scale_fill_gradient2(low = 'green',mid = 'white',high = 'red',name = 'expression') +
        # 基因名
        set_p_yaxis()

      添加点:

      # 添加点
      p9 + anno_row_heat(da,aes(color = r),width = 0.8,
                         geom = 'point') +
        scale_color_gradient2(low = '#F9B208',mid = 'white',high = '#F55C47',name = 'expression') +
        # 基因名
        set_p_yaxis()


      欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群 哦。

      群二维码:


      老俊俊微信:




      知识星球:



      所以今天你学习了吗?

      欢迎小伙伴留言评论!

      点击我留言!

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

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

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



       往期回顾 




      ◀你看过 NCBI 的基因组和注释文件吗?

      ◀ComplexHeatmap 之 Legends 续(二)

      ◀ComplexHeatmap 之 Legends 续(一)

      ◀ComplexHeatmap 之 Legends

      ◀ComplexHeatmap 之 Heatmap List 续(二)

      ◀ComplexHeatmap 之 Heatmap List 续(一)

      ◀ComplexHeatmap 之 Heatmap List

      ◀ComplexHeatmap 之 Heatmap Annotations 续(三)

      ◀ComplexHeatmap 之 Heatmap Annotations 续(二)

      ◀ComplexHeatmap 之 Heatmap Annotations 续(一)

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

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      把 corrplot 颜色条改成文献里那样?
      2021年9月10日

      下一篇文章

      ggplot2 | 图例(Ⅰ):图例函数、主题函数中的图例参数
      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年
      在线支付 激活码

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