• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    教学以及管理操作教程

    教学以及管理操作教程

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

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      教学以及管理操作教程

      教学以及管理操作教程

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

      未分类

      • 首页
      • 博客
      • 未分类
      • pheatmap|暴雨暂歇,“热图”来袭!!!

      pheatmap|暴雨暂歇,“热图”来袭!!!

      • 发布者 一览
      • 分类 未分类
      • 日期 2020年3月8日
      • 评论 0评论

      参考:https://mp.weixin.qq.com/s?__biz=MzIyNDI1MzgzOQ==&mid=2650394128&idx=1&sn=5cb8b1a16077abc451a20fde62e6d347&chksm=f01cacf0c76b25e60f4f54cf72608bed1e5ab7a07c3d7c6fb695f29215d0ca103077af94bdfa&scene=21#wechat_redirect

        热图可以聚合大量的数据,并可以用一种渐进色来优雅地表现,可以很直观地展现数据的疏密程度或频率高低。

          本文利用R语言 pheatmap 包从头开始绘制各种漂亮的热图。参数像积木,拼凑出你最喜欢的热图即可,如下图:

           基因和样本都可以单独聚类,排序,聚类再分组,行列注释,配色调整,调整聚类线以及单元格的宽度和高度均可实现。

      载入数据,R包

      #R包
      library(pheatmap)
      # 构建测试数据 
      set.seed(1234)
      test = matrix(rnorm(200), 20, 10)
      test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3 
      test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2 
      test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4 
      colnames(test) = paste("Test", 1:10, sep = "")
      rownames(test) = paste("Gene", 1:20, sep = "")
      head(test[,1:6])

      绘制热图

      绘制默认热图

      pheatmap(test)

      基本参数

      # scale = “row”参数对行进行归一化
      # clustering_method参数设定不同聚类方法,默认为”complete”,可以设定为’ward’, ‘ward.D’, ‘ward.D2’, ‘single’, ‘complete’, ‘average’, ‘mcquitty’, ‘median’ or ‘centroid’

      pheatmap(test,scale = "row", clustering_method = "average")

      #表示行聚类使用皮尔森相关系数聚类,默认为欧氏距离”euclidean”

      pheatmap(test, scale = "row", clustering_distance_rows = "correlation")

      #行 列是否聚类,cluster_row ,cluster_col

      pheatmap(test, cluster_row = FALSE,cluster_col = TRUE)

      # treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50

      pheatmap(test, treeheight_row = 30, treeheight_col = 50)

      # 设定cell 的大小

      pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 10)

      设定 text

      热图中展示数值

      # display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,#number_color参数设置数值字体的颜色

      pheatmap(test, display_numbers = TRUE,number_color = "blue")

      # 设定数值的显示格式

      pheatmap(test, display_numbers = TRUE, number_format = "%.1e")

      #设定条件式展示

      pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

      设置 legend 

      设定legend展示的值
      #legend_breaks参数设定图例显示范围,legend_labels参数添加图例标签

      pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1"))

      #去掉legend

      pheatmap(test, legend = FALSE)

      设定 color

      自定义颜色

      #colorRampPalette 

      pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

      # border_color参数设定每个热图格子的边框色

      # border=TRIUE/FALSE参数是否要边框线

      pheatmap(test, border_color = "red", border=TRUE)

      设定 annotations

      # 生成行 列的注释

      annotation_col = data.frame( CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5 )
      rownames(annotation_col) = paste("Test", 1:10, sep = "")
      annotation_row = data.frame( GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))
      rownames(annotation_row) = paste("Gene", 1:20, sep = "")

      #添加列的注释

      pheatmap(test, annotation_col = annotation_col)

      #添加行 列的注释

      #angle_col 改变列标签的角度

      pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")

      # 根据聚类结果,自定义注释分组及颜色

      ann_colors = list( Time = c("white", "firebrick"), CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"), GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E") )
      pheatmap(test, annotation_col = annotation_col,annotation_row=annotation_row, annotation_colors = ann_colors, main = "Title")

      设定 gap

      #根据聚类结果,设定行gap

      pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))

      #根据聚类结果,设定列gap

      
      pheatmap(test,annotation_col = annotation_col, cluster_rows = FALSE,cutree_col = 2)

      #展示行或者列的label

      labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b")
      pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)

      热图汇总

      
      pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap")

      输出结果

      
      A = pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap") #记录热图的行排序
      
      order_row = A$tree_row$order 
       #记录热图的列排序
      order_col = A$tree_col$order   
      # 按照热图的顺序,重新排原始数据
      result = data.frame(test[order_row,order_col])   
      # 将行名加到表格数据中
      result = data.frame(rownames(result),result,check.names =F)  
      colnames(result)[1] = "geneid" 
      #result结果按照热图中的顺序
      write.table(result,file="reorder.txt",row.names=FALSE,quote = FALSE,sep='\t')  

      R的当前工作目录下即可查看热图的结果。

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

      • 分享:
      作者头像
      一览

      上一篇文章

      ggalluvial|炫酷桑基图(Sankey),你也可以秀
      2020年3月8日

      下一篇文章

      ggplot2-plotly|让你的火山图“活”过来
      2020年3月8日

      你可能也喜欢

      2-1675088548
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      30 1月, 2023
      9-1675131201
      如何快速批量修改 Git 提交记录中的用户信息
      26 1月, 2023
      5-1660909989
      scanpy官方教程2022|03-scanpy包核心绘图功能
      19 8月, 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年
      在线支付 激活码

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