【问题标题】:Circular heatmap with R "circlize": Plot area and row labels带有 R“circlize”的圆形热图:绘制区域和行标签
【发布时间】:2020-03-22 09:32:00
【问题描述】:

我正在尝试使用 R“circlize”包绘制带有树状图的单个圆形聚集热图。两列表达式数据可从此pastebin link

我正在使用以下代码进行绘图,大部分来自circlize manual

dataURL <- "https://pastebin.com/raw/whLr21ZA"
expData.df<- read.table(dataURL,header=TRUE,sep="\t",stringsAsFactors=FALSE)
expData.mat <- as.matrix(expData.df[-c(1)])
rownames(expData.mat) <- expData.df$Mol
useT.mat <- t(expData.mat)
mat_list = list(a = useT.mat)
dend_list = list(a = as.dendrogram(hclust(dist(t(mat_list[["a"]])))) )
col_fun = colorRamp2(breaks= c(-13.288,-5.265,-6.674,-2.544,4.694,5.000), colors = c("blue4","lightblue","yellow","orange","orangered", "red"),transparency = .4)
factors = rep(letters[1], times = c( dim(useT.mat)[2] ))

circos.par(cell.padding = c(0, 0, 0, 0), gap.degree = 15)
circos.initialize(factors, xlim =cbind(c(0), table(factors)))

circos.track(ylim = c(0, 1), bg.border = NA, panel.fun = function(x, y) {
    sector.index = CELL_META$sector.index
    m = mat_list[[sector.index]]
    dend = dend_list[[sector.index]]

    m2 = m[, order.dendrogram(dend)]
    col_mat = col_fun(m2)
    nr = nrow(m2)
    nc = ncol(m2)
    for(i in 1:nr) {
        circos.rect(1:nc - 1, rep(nr - i, nc), 
            1:nc, rep(nr - i + 1, nc), 
            border = col_mat[i, ], col = col_mat[i, ])
    }
})
#Dendrogram
max_height = max(sapply(dend_list, function(x) attr(x, "height")))
circos.track(ylim = c(0, max_height), bg.border = NA, track.height = 0.3, 
    panel.fun = function(x, y) {

        sector.index = get.cell.meta.data("sector.index")
        dend = dend_list[[sector.index]]
        circos.dendrogram(dend, max_height = max_height)
})
circos.clear()

我在绘制数据时遇到了两个问题:

  1. 部分图形超出了顶部和底部的绘图区域。

  2. 图中不知道怎么放行列标签

谁能帮忙解决这两个问题?

谢谢

【问题讨论】:

    标签: r heatmap circlize


    【解决方案1】:

    您可以更改每个轨迹的 y 限制以匹配绘图内容,这样图形就不会超出绘图区域。您可以使用 circlize 手册中的 barplots 示例添加行标签;您可以通过在外部创建仅包含标签的额外轨道来添加列标签。这你也可以从手册中大致复制系统发育树的例子。他们都在同一章,第5章。 使用您提供的代码进行一些修改,示例如下:

    dataURL <- "https://pastebin.com/raw/whLr21ZA"
    expData.df<- read.table(dataURL,header=TRUE,sep="\t",stringsAsFactors=FALSE)
    expData.mat <- as.matrix(expData.df[-c(1)])
    rownames(expData.mat) <- expData.df$Mol
    useT.mat <- t(expData.mat)
    mat_list = useT.mat #changed to fit 1 heatmap
    dend_list = as.dendrogram(hclust(dist(t(mat_list)))) #changed to calculate for 1 matrix
    col_fun = colorRamp2(breaks= c(-13.288,-5.265,-6.674,-2.544,4.694,5.000), 
    colors = c("blue4","lightblue","yellow","orange","orangered", "red")) 
    #removed transparency so easier to save in EPS, very optional
    #excluded 'factor' as there is only 1 heatmap
    
    circos.par("start.degree" = 90,cell.padding = c(0, 0, 0, 0), gap.degree = 15) 
    circos.initialize("a", xlim =c(0,292)) #changed to 1 heatmap setting
    #adding new track for column label
    circos.track(ylim = c(0, 1), bg.border = NA, track.height = 0.05, 
    panel.fun = function(x, y) {
        for(i in seq_len(ncol(useT.mat))) {
            circos.text(i-0.5, 0, colnames(useT.mat)[order.dendrogram(dend_list)][i], adj = c(0, 0.5), 
                facing = "clockwise", niceFacing = TRUE,
             cex = 0.5)                
        }
    })
    
    circos.track(ylim = c(0, 2), bg.border = NA, panel.fun = function(x, y) {
    m = mat_list 
    dend = dend_list
    #changed variable for 1 heatmap setting
    m2 = m[, order.dendrogram(dend)]
    col_mat = col_fun(m2)
    nr = nrow(m2)
    nc = ncol(m2)
    for(i in 1:nr) {
        circos.rect(1:nc - 1, rep(nr - i, nc), 
            1:nc, rep(nr - i + 1, nc), 
            border = col_mat[i, ], col = col_mat[i, ])
    }
    #adding row label
    circos.text(rep(1, 2), 1:2, 
            rownames(useT.mat), 
            facing = "downward", adj = c(1.45, 1.1), cex = 0.7) 
    
    })
    #Dendrogram
    max_height = attr(dend_list, "height") #changed for 1 dendrogram setting
    circos.track(ylim = c(0, max_height), bg.border = NA, track.height = 0.3, 
    panel.fun = function(x, y) {
        dend = dend_list
        circos.dendrogram(dend, max_height = max_height)
    })
    circos.clear()
    
    #adding legend key
    library(ComplexHeatmap)
    lgd_links = Legend(at=c(-15,-5,0,5),col_fun = col_fun, 
    title_position = "topleft", title = "Value", direction = "horizontal")
    draw(lgd_links, x = unit(1, "npc") - unit(2, "mm"), y = unit(4, "mm"), 
    just = c("right", "bottom"))
    

    (示例图链接:https://drive.google.com/file/d/1doq7jBhuUn7g795gqFjYjux90vZIyHA8/view

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多