• 主页
  • 课程

    关于课程

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

    教学以及管理操作教程

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

      关于课程

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

      教学以及管理操作教程

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

      老俊俊的生信笔记

      • 首页
      • 博客
      • 老俊俊的生信笔记
      • pheatmap展示自定义标签

      pheatmap展示自定义标签

      • 发布者 weinfoadmin
      • 分类 老俊俊的生信笔记
      • 日期 2021年10月1日
      测试开头

      在简书 土豆学生信 分享的内容看到这篇论文 简书的链接是 https://www.jianshu.com/p/bbf9cb13b41a

      论文是pheatmap展示自定义标签

      论文对应的代码是公开的 https://github.com/ajwilk/2020_Wilk_COVID

      pheatmap展示自定义标签
      image.png

      在学习他这个代码的时候发现其中自定义了一个函数可以操作热图的文字标签,可以让热图上只显示我们感兴趣的文字标签。

      我在运行这个代码的时候遇到了报错,没有把代码完全运行完,但是已经获得和NK.markers这个表达量文件,部分内容如下

      pheatmap展示自定义标签
      image.png

      我们用这个表达量文件先做一个简单的热图

      读入数据
      df<-read.csv("NM/NK_markers_1.csv",header=T,row.names = 1)
      head(df)
      最简单的热图
      library(pheatmap)
      pdf(file = "NM/hp-1.pdf",width = 4,height = 10)
      pheatmap(df,fontsize = 3)
      dev.off()
      pheatmap展示自定义标签
      image.png

      我们可以看到上图右侧所有的基因名都显示出来了,如果我们想只显示自己感兴趣的,那该如何实现呢?可以用开头提到的自定义函数

      add.flag <- function(pheatmap,
                           kept.labels,
                           repel.degree) {
        
        # repel.degree = number within [0, 1], which controls how much 
        #                space to allocate for repelling labels.
        ## repel.degree = 0: spread out labels over existing range of kept labels
        ## repel.degree = 1: spread out labels over the full y-axis
        
        heatmap <- pheatmap$gtable
        
        new.label <- heatmap$grobs[[which(heatmap$layout$name == "row_names")]] 
        
        # keep only labels in kept.labels, replace the rest with ""
        new.label$label <- ifelse(new.label$label %in% kept.labels, 
                                  new.label$label, "")
        
        # calculate evenly spaced out y-axis positions
        repelled.y <- function(d, d.select, k = repel.degree){
          # d = vector of distances for labels
          # d.select = vector of T/F for which labels are significant
          
          # recursive function to get current label positions
          # (note the unit is "npc" for all components of each distance)
          strip.npc <- function(dd){
            if(!"unit.arithmetic" %in% class(dd)) {
              return(as.numeric(dd))
            }
            
            d1 <- strip.npc(dd$arg1)
            d2 <- strip.npc(dd$arg2)
            fn <- dd$fname
            return(lazyeval::lazy_eval(paste(d1, fn, d2)))
          }
          
          full.range <- sapply(seq_along(d), function(i) strip.npc(d[i]))
          selected.range <- sapply(seq_along(d[d.select]), function(i) strip.npc(d[d.select][i]))
          
          return(unit(seq(from = max(selected.range) + k*(max(full.range) - max(selected.range)),
                          to = min(selected.range) - k*(min(selected.range) - min(full.range)), 
                          length.out = sum(d.select)), 
                      "npc"))
        }
        new.y.positions <- repelled.y(new.label$y,
                                      d.select = new.label$label != "")
        new.flag <- segmentsGrob(x0 = new.label$x,
                                 x1 = new.label$x + unit(0.15, "npc"),
                                 y0 = new.label$y[new.label$label != ""],
                                 y1 = new.y.positions)
        
        # shift position for selected labels
        new.label$x <- new.label$x + unit(0.2, "npc")
        new.label$y[new.label$label != ""] <- new.y.positions
        
        # add flag to heatmap
        heatmap <- gtable::gtable_add_grob(x = heatmap,
                                           grobs = new.flag,
                                           t = 4, 
                                           l = 4
        )
        
        # replace label positions in heatmap
        heatmap$grobs[[which(heatmap$layout$name == "row_names")]] <- new.label
        
        # plot result
        grid.newpage()
        grid.draw(heatmap)
        
        # return a copy of the heatmap invisibly
        invisible(heatmap)
      }

      将以上函数放到文本文件里,通过source()加载这个函数

      source("useful_R_function/add_flag.r")

      选择感兴趣的基因名,我这里就随机选取几个了

      gene_name<-sample(rownames(df),10)

      画图

      source("useful_R_function/add_flag.r")
      library(grid)
      gene_name<-sample(rownames(df),10)
      p1<-pheatmap(df)
      add.flag(p1,
               kept.labels = gene_name,
               repel.degree = 0.2)

      结果就变成了如下pheatmap展示自定义标签

      接下来是简单的美化

      代码

      source("useful_R_function/add_flag.r")
      df<-read.csv("NM/NK_markers_1.csv",header=T,row.names = 1)
      head(df)
      library(pheatmap)
      library(grid)
      gene_name<-sample(rownames(df),10)
      paletteLength <- 100
      mycolor<-colorRampPalette(c("blue","white","red"))(100)
      mycolor
      myBreaks <- unique(c(seq(min(df), 0, length.out=ceiling(paletteLength/2) + 1), 
                           seq(max(df)/paletteLength, max(df),
                               length.out=floor(paletteLength/2))))
      p1<-pheatmap(df,color = mycolor,breaks = myBreaks)
      pdf(file = "NM/hp-2.pdf",width = 4,height = 8)
      add.flag(p1,
               kept.labels = gene_name,
               repel.degree = 0.2)
      dev.off()
      pheatmap展示自定义标签
      image.png

      这个图和开头提到的论文里的Figure3f就有几分相似了,但是还没有添加分组信息


      需要用到示例数据的可以在文末留言,记得点赞和点击在看!

      测试结尾

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

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      怎么在 UCSC 官网下载基因组和注释文件?
      2021年10月1日

      下一篇文章

      circRNAs 定量之 CIRIquant 软件
      2021年10月2日

      你可能也喜欢

      8-1651542331
      跟着Nature学绘图(2) 箱线图-累积分布曲线图
      2 5月, 2022
      9-1651542322
      Julia 笔记之字符串
      2 5月, 2022
      0-1651542343
      Julia 笔记之数学运算和初等函数
      1 5月, 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年
      在线支付 激活码

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