美文网首页
分享一个好看的环形热图做法

分享一个好看的环形热图做法

作者: 一只烟酒僧 | 来源:发表于2021-01-25 14:29 被阅读0次
#一个基因表达矩阵,行为基因,列为样本,其中样品是根据组别排好序,如control1 control2 treat1 treat2
  mat=gsva_ceRNA_mRNA[rownames(ceRNA_gsva.DE.res),]
  #一个样本分组矩阵,含两列,一列为Var1,记录分组信息,如control组和treat组 第二列为Freq,该分组的样品频数
  anno_df<-as.data.frame(table(factor(str_replace(colnames(mat),"\\.[0-9]",""),levels =unique(str_replace(colnames(mat),"\\.[0-9]","")) )))
  #以下为作图代码
  {
mat<-as.matrix(t(apply(mat,1,scale)))
  rownames(mat)<-rownames(mat)
  colnames(mat)<-colnames(mat)
  col_fun = colorRamp2(breaks = c(-2, 0, 2), colors = c("skyblue", "white", "red"))
  lgd = Legend(title = "expr", col_fun = col_fun)
  mat_sub<-t(mat)
  dend <-as.dendrogram(hclust(dist(t(mat_sub))))
  n=3
  dend <-dend %>% set("branches_k_color", k = n) 
  par(mar=c(7.5,3,1,0))
  plot(dend)
  mat2 = mat_sub[, order.dendrogram(dend)]
  lable1=row.names(mat2);lable1
  lable2=colnames(mat2);lable2
  circos.clear()
  circos.par(canvas.xlim =c(-1.3,1.3),
             canvas.ylim = c(-1.3,1.3),
             cell.padding = c(0,0,0,0), 
             gap.degree =90)
  factors = "a"
  circos.initialize(factors, xlim = c(0, ncol(mat2)))
  nr<-nrow(mat2);nc<-ncol(mat2);lable2=colnames(mat2);col_mat<-colorRamp2(c(-1.5, 0, 1.5), c("skyblue", "white", "red"))(mat2)
  circos.track(ylim = c(0, nr),bg.border = NA,track.height = 0.05*nr,
               panel.fun = function(x, y) {
                 for(i in 1:nr) {
                   circos.rect(xleft = 1:nc - 1, ybottom = rep(nr - i, nc),
                               xright = 1:nc, ytop = rep(nr - i + 1, nc),
                               border = "white",
                               col = col_mat[i,])
                   # circos.text(x = nc,
                   #             y = 26 -i,
                   #             labels = lable1[i],
                   #             facing = "downward", niceFacing = TRUE,
                   #             cex = 0.6,
                   #             adj = c(-0.2, 0))
                 }
               })
  for(i in 1:nc){
    circos.text(x = i-0.4,
                y = nr+0.5,
                labels = lable2[i],
                facing = "clockwise", niceFacing = TRUE,
                cex = 0.6,adj = c(0, 0))
  }
  max_height <-max(attr(dend, "height"))
  circos.track(ylim = c(0, max_height),bg.border = NA,track.height = 0.1, 
               panel.fun = function(x, y){
                 circos.dendrogram(dend = dend,
                                   max_height = max_height)
               })
  anno_df<-anno_df
  anno_df$ytop=Reduce(sum,anno_df$Freq,accumulate = T)
  anno_df$ybottom=anno_df$ytop-anno_df$Freq
  anno_df$color=RColorBrewer::brewer.pal(12,"Set3")[1:length(anno_df$Var1)]
  anno_df$text_loc=(anno_df$ybottom+anno_df$ytop)/2
  
  circos.track(track.index = 1, panel.fun = function(x, y) {
    if(CELL_META$sector.numeric.index == 1) { 
      for (i in 1:(dim(anno_df)[1])) {
        circos.rect(xleft = -2,
                    xright = 0,
                    ybottom = anno_df$ybottom[i],
                    ytop = anno_df$ytop[i],
                    col = anno_df$color[i], border = NA)
        circos.text(x=-1,y=anno_df$text_loc[i],anno_df$Var1[i],cex = 0.6,col = "black",facing = "outside")
      }
      
      
    }
  }, bg.border = NA)
  lgd <- Legend(at = c(-2,-1, 0, 1, 2), col_fun = col_fun, 
                title_position = "topcenter",title = "Z-score")
  draw(lgd, x = unit(0.65, "npc"), y = unit(0.7, "npc"))
}
最后出图的样式

相关文章

网友评论

      本文标题:分享一个好看的环形热图做法

      本文链接:https://www.haomeiwen.com/subject/atzbzktx.html