将标签放在 circlize 和弦图中的图例颜色条内

Put labels inside the legend color bar in circlize chord diagram

我一直在关注 circular visualization in R tutorial

我尝试重新创建 Chapter 16 A complex example of Chord diagram 中的图形。特别是,我问的是如何为 'Chromatin States' 制作带标签的图例,即用数字标记颜色图例中的每个框。

文本参考图例部分(“图例可以根据 Section 4 中讨论的说明添加”)。但是,那里没有明确描述单个图例网格内的标签。

下面是数据的代码:

library(circlize)
library(tidyverse)
library(ComplexHeatmap)
library(grid)
library(gridBase)
library(gridExtra)
library(RColorBrewer)

download.file("https://jokergoo.github.io/circlize_book/data/chromatin_transition.RData", destfile = "chromatin_transition.RData")
load("chromatin_transition.RData")
mat[1:4, 1:4]
meth_mat_1[1:4, 1:4]
diag(mat) = 0 
all_states = rownames(mat)
n_states = nrow(mat)
rownames(mat) = paste0("R_", seq_len(n_states))
colnames(mat) = paste0("C_", seq_len(n_states))
dimnames(meth_mat_1) = dimnames(mat)
dimnames(meth_mat_2) = dimnames(mat)
    
state_col = c("TssA" = "#E41A1C",    "TssAFlnk" = "#E41A1C",
              "TxFlnk" = "#E41A1C",  "Tx" = "#E41A1C",
              "TxWk" = "#E41A1C",    "EnhG" = "#E41A1C",
              "Enh" = "#E41A1C",     "ZNF/Rpts" = "#E41A1C",
              "Het" = "#377EB8",     "TssBiv" = "#377EB8",
              "BivFlnk" = "#377EB8", "EnhBiv" = "#377EB8",
              "ReprPC" = "#377EB8",  "ReprPCWk" = "#377EB8",
              "Quies" = "black")

state_col2 = c(state_col, state_col)
names(state_col2) = c(rownames(mat), colnames(mat))

colmat = rep(state_col2[rownames(mat)], n_states)
colmat = rgb(t(col2rgb(colmat)), maxColorValue = 255)

qati = quantile(mat, 0.7)
colmat[mat > qati] = paste0(colmat[mat > qati], "A0")
colmat[mat <= qati] = paste0(colmat[mat <= qati], "20")
dim(colmat) = dim(mat)
col_fun = colorRamp2(c(0.5 - abs_max, 0.5, 0.5 + abs_max), c("blue", "white", "red"))
col_fun2 = colorRamp2(c(-abs_max, 0, abs_max), c("green", "white", "orange"))

这是传说中的代码:

lgd_chr = Legend(title = "Chromatin States", at = names(state_col), 
        legend_gp = gpar(fill = state_col))))
    lgd_mmeth = Legend(title = "Mean Methylation", at = seq(0.1, 0.9, 0.2), col_fun = col_fun)
    lgd_mdmeth = Legend(title = "Mean Difference", col_fun = col_fun2)
        
    h = dev.size()[2]
    circle_size = unit(1, "snpc")
    lgd_list = packLegend(lgd_chr, lgd_mmeth, lgd_mdmeth, max_height = unit(0.9*h, "inch"))
    draw(lgd_list, x = circle_size, just = "right") 

而且,这是我所能得到的:

我找不到合适的函数来制作带标签的图例。有人知道如何制作 'Chromatin States' 的图例编号 1-15 吗?

非常感谢您。

来自 ?ComplexHeatmap::Legend:

pch: Type of points if points are used as legend. Note you can use single-letter as pch, e.g. pch = 'A'

background: Background colors for the grids. It is used when points and lines are the legend graphics.

因此,一种可能是使用 type = "points 而不是默认的 "grid";将标签创建为 pch = as.character(<the-desired-numbers>)。作为 background,使用您的 state_col 向量。小例子:

L1 = Legend(labels = month.name[1:5],
            type = "points", pch = as.character(1:5),
            legend_gp = gpar(col = "white", cex = 0.7),
            background = c("red", "red", "black", "blue", "blue"))
draw(L1)