添加轨道/图层/网格以圈化::chordDiagram 的方法与规范

问题描述 投票:0回答:1

我需要在通过 circlize 制作的和弦图上可视化附加信息。我不知道该怎么做。我尝试过

highlight.section
,但这似乎并不那么简单

让我们使用这些数据。

set.seed(1)

  df <- data.frame(
    Genes = 1:52,
    Description = sample(LETTERS[1:7], size = 52, replace = TRUE),
    value1 = sample(0:1, size = 52, replace = TRUE),
    value2 = sample(runif(52, min = 0, max = 0.001), size = 52, replace = TRUE),
    value3 = sample(10:20, size = 52, replace = TRUE)
  )

我使用下面的代码制作了这个图

library(circlize)
library(viridis)

matrix <- with(df, table(Description, Genes))  
  
  
  circos.clear()
  circos.par(start.degree = 90)
  
  description_colors <- setNames(viridis(length(rownames(matrix))), rownames(matrix))
  col_names_color <- setNames(rep("grey", length(colnames(matrix))), colnames(matrix))
  all_colors <- c(description_colors, col_names_color)
  
  # Generate the chord diagram with specified colors
  chordDiagram(matrix, transparency = 0.5, 
               annotationTrack = "grid", 
               annotationTrackHeight = c(0.03),
               preAllocateTracks = list(track.height = 0.1), # Reduced track height for genes
               grid.col = all_colors, # Apply colors to both descriptions and genes
               directional = -1,
               big.gap = 30, small.gap = 1)  # Adjust the highlight sector height here
  

  
  # Text labels for the sectors
  circos.trackPlotRegion(track.index = 1, panel.fun = function(x, y) {
    xlim = get.cell.meta.data("xlim")
    ylim = get.cell.meta.data("ylim")
    sector.name = get.cell.meta.data("sector.index")
    circos.text(CELL_META$xcenter, ylim[1] + cm_h(2), sector.name, 
                facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5))
  }, bg.border = NA)

问题 正如你所看到的,df 也有

$value1, $value2, $value3
。 我想添加这样的内容:

df$value
可以表示
log2-fold change
FDR
。因此,应添加渐变颜色图例并对应 3 个值中的每一个。

OBS:

df$value3
已绘制在“方向”颜色的“下方”/内侧。它也可以绘制在每个 df$Gene 的灰色区域的“外部”,以便方向颜色与每个描述保持清晰的联系。

r plot chord-diagram circlize complexheatmap
1个回答
0
投票

这是否接近您想要实现的目标?

注意,因为

chordDiagram
创建了
value1
value2
列,所以我将
df
值重命名为
v1
v2
v3
,以便在连接到
chordDiagram
的输出时区分它们.

我将要求解释为

v3
仅绘制在左侧;右侧的
v1
v2
仅使用每个描述的相应值的总和。

library(circlize)
library(ComplexHeatmap)
library(tidyverse)
library(viridis)

set.seed(1)

df <- data.frame(
  Description = sample(LETTERS[1:7], size = 52, replace = TRUE),
  Genes = as.character(1:52),
  v1 = sample(0:1, size = 52, replace = TRUE),
  v2 = sample(runif(52, min = 0, max = 0.001), size = 52, replace = TRUE),
  v3 = sample(10:20, size = 52, replace = TRUE)
)

circos.clear()
circos.par(start.degree = 90)
set_track_gap(gap = 0)

matrix <- with(df, table(Description, Genes))
description_colors <- setNames(viridis(length(rownames(matrix))), rownames(matrix))
col_names_color <- setNames(rep("grey", length(colnames(matrix))), colnames(matrix))
all_colors <- c(description_colors, col_names_color)

cdm_res <- chordDiagram(
  df[, 1:2],
  big.gap = 30,
  annotationTrack = c("name", "grid"),
  grid.col = all_colors
) |>
  left_join(df, join_by(rn == Description, cn == Genes)) |>
  mutate(across(c(v1, v2), sum), .by = rn) |>
  mutate(across(c(v1, v2, v3), \(x) ordered(x) |> fct_rev()))

circos.track(
  track.index = 3, ylim = c(-1.05, -1),
  track.height = 0.05, bg.border = "white", bg.col = "white"
)

circos.track(
  track.index = 4, ylim = c(-1.1, -1.05),
  track.height = 0.05, bg.border = "white", bg.col = "white"
)

greens <- c("darkgreen", "lightgreen")
reds <- c("darkred", "pink")
blues <- c("darkblue", "skyblue")

col1 <- colorRampPalette(greens)(n_distinct(cdm_res$v1))
col2 <- colorRampPalette(reds)(n_distinct(cdm_res$v2))
col3 <- colorRampPalette(blues)(n_distinct(cdm_res$v3))

lgd <- \(x, y) Legend(
  at = c("High", "Low"), type = "grid",
  legend_gp = gpar(fill = x, col = "black", lwd = 5),
  title_position = "topleft", title = y, background = "white"
)

lgd1 <- lgd(greens, "Value V1")
lgd2 <- lgd(reds, "Value V2")
lgd3 <- lgd(blues, "Value V3")

lgd_list <- packLegend(lgd1, lgd2, lgd3)

for (i in seq_len(nrow(cdm_res))) {
  circos.rect(
    xleft = cdm_res[i, "x1"], ybottom = -1, xright = 0, ytop = -1.06,
    col = col1[cdm_res[i, "v1"]], border = col1[cdm_res[i, "v1"]],
    sector.index = cdm_res$rn[i], track.index = 3
  )
  circos.rect(
    xleft = cdm_res[i, "x1"], ybottom = -1.04, xright = 0, ytop = -1.1,
    col = col2[cdm_res[i, "v2"]], border = col2[cdm_res[i, "v2"]],
    sector.index = cdm_res$rn[i], track.index = 4
  )
  circos.rect(
    xleft = cdm_res[i, "x2"], ybottom = -1, xright = 0, ytop = -1.13,
    col = col3[cdm_res[i, "v3"]], border = col3[cdm_res[i, "v3"]],
    sector.index = cdm_res$cn[i], track.index = 3
  )
}

draw(lgd_list, x = unit(10, "mm"), y = unit(10, "mm"), just = c("left", "bottom"))

创建于 2024-04-08,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.