将标签放在 circlize 和弦图中的图例颜色条内
Posted
技术标签:
【中文标题】将标签放在 circlize 和弦图中的图例颜色条内【英文标题】:Put labels inside the legend color bar in circlize chord diagram 【发布时间】:2021-10-04 19:30:30 【问题描述】:我一直在关注circular visualization in R tutorial。
我尝试在Chapter 16 A complex example of Chord diagram 中重新创建该图。特别是,我在询问如何为“染色质状态”制作标记图例,即用数字标记颜色图例中的每个框。
文字指的是图例部分(“可以根据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")
而且,这是我所能得到的:
我无法找到合适的函数来制作带标签的图例。有人知道如何为“染色质状态”制作图例编号 1-15 吗?
非常感谢您。
【问题讨论】:
【参考方案1】:来自?ComplexHeatmap::Legend
:
pch
:点的类型,如果点用作图例。请注意,您可以将单个字母用作pch
,例如pch = 'A'
background
:网格的背景颜色。当点和线是图例图形时使用。
因此,一种可能是使用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)
【讨论】:
以上是关于将标签放在 circlize 和弦图中的图例颜色条内的主要内容,如果未能解决你的问题,请参考以下文章