Search code examples
rdata-visualizationlegendchord-diagramcirclize

Put labels inside the legend color bar in circlize chord diagram


I've been following the circular visualization in R tutorial.

I try to recreate the figure in Chapter 16 A complex example of Chord diagram. Particularly, I am asking on how to make the labelled legend for 'Chromatin States', i.e. to label each box in the color legend with numbers.

The text refers to the legend section ("Legends can be added according to instructions discussed in Section 4"). However, labelling inside individual legend grids is not explicitly described there.

enter image description here

Below is the codes to the data:

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"))

And here's the codes to the legends:

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") 

And, this is as far as I can get:

enter image description here

I can't manage to find the right function to make the labelled legends. Does someone know how to make the legend number 1-15 for 'Chromatin States'?

Thank you very much in advance.


Solution

  • From ?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.

    Thus, one possibilty may be to use type = "points instead of the default "grid"; create the labels as pch = as.character(<the-desired-numbers>). As background, use your state_col vector. Small example:

    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)
    

    enter image description here