簡體   English   中英

帶有 R“circlize”的圓形熱圖:繪制區域和行標簽

[英]Circular heatmap with R "circlize": Plot area and row labels

我正在嘗試使用 R“circlize”包繪制帶有樹狀圖的單個圓形聚集熱圖。 可從此pastebin 鏈接獲得兩列表達式數據

我正在使用以下代碼進行繪圖,主要是從circlize 手冊中提取的

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. 我不知道如何在圖中放置行和列標簽

任何人都可以幫助解決這兩個問題嗎?

謝謝

您可以更改每個軌道的 y 限制以匹配繪圖內容,這樣圖形就不會超出繪圖區域。 您可以使用 circlize 手冊中的條形圖示例添加行標簽; 您可以通過在外部創建僅包含標簽的額外軌道來添加列標簽。 這個你也可以粗略地從手冊中復制系統發育樹的例子。 它們都在同一章第 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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM