具有突出显示、无向和有限边缘的和弦图 (R)

发布于 2025-01-13 11:50:13 字数 2604 浏览 1 评论 0原文

我想展示某些项目(A01,...,Q04)及其协作(边缘)的网络。应该可以突出显示从一个项目到其他项目的所有边缘(黄色)。 这是目前的结果。

circlize

我有三个问题,我需要帮助解决:

  1. 我的边缘列表被认为是有向的,但我希望它是无向的。所有项目只应在圆圈中显示一次。
  2. 项目的顺序应为“A01,...,A09,B01,...B07,Q01,...Q04”,如crc_color_circlize所示。
  3. 来自一个顶点的边数应始终为“一”。现在,我在“B06”和其他项目之间有很多边缘,但这些应该“合并”到“B06”之外的一个边缘中。换句话说,所有项目顶点都具有相同的宽度。

这是我的代码:

library(circlize)


crc_edges <- read.table(header=TRUE,
                        sep=",",
                        text="from,to
  A01,A02
  A01,B06
  A02,A03
  A02,B05
  A02,B06
  B06,A01
  B06,A07
  B06,Q01
  B06,Q02
  B06,B02
  B06,B03
  B06,B04
  B06,A05
  B06,A09
  B06,B01
  A03,A01
  A05,A04
  A04,B01
  A06,Q02
  A07,Q04")
crc_edges

crc_color_circlize <- c(
          A01 = "#e30066",
          A02 = "#e30066",
          A03 = "#e30066",
          A04 = "#e30066",
          A05 = "#e30066",
          A06 = "#e30066",
          A07 = "#e30066",
          A08 = "#e30066",
          A09 = "#e30066",
          B01 = "#00549f",
          B02 = "#00549f",
          B03 = "#00549f",
          B04 = "#00549f",
          B05 = "#00549f",
          B06 = "#00549f",
          B07 = "#00549f",
          Q01 = "#57ab27",
          Q02 = "#57ab27",
          Q03 = "#57ab27",
          Q04 = "#57ab27")

crc_circlize_group <- function(crc_name) {
  crc_circlize_local <- function(crc_name){
    set.seed(123)
    chordDiagram(crc_edges,
                 row.col = crc_color_circlize,
                 grid.col = crc_color_circlize,
                 col =  with(crc_edges,
                             ifelse(from %in% crc_name, "#ffed00",
                                    ifelse(to == crc_name, "#ffed00",
                                           "#ECEDED"))),
                 transparency = 0.5,
                 annotationTrack =  c("grid"),
                 preAllocateTracks = list(track.height = max(strwidth(unlist(dimnames(crc_edges))))))
    # we go back to the first track and customize sector labels
    circos.track(track.index = 1, panel.fun = function(x, y) {
      circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index,
                  facing = "clockwise", niceFacing = TRUE, adj = c(0, 0))
    }, bg.border = NA,
    
    ) # here set bg.border to NA is important
    circos.clear()
  }
  crc_circlize_local(crc_name)
}

# hightlight no edge
crc_circlize_group("")
# highlight edges for one vertex
crc_circlize_group("B06")

I would like to display a network of certain projects (A01,...,Q04) and their collaborations (edges). It should be possible to highlight all the edges from one projects to the others (in yellow).
This is the current result.

circlize

I have three problems, I need help to solve:

  1. my edge list is considered as directed, but I want it to be undirected. All projects should be shown just once in the circle.
  2. The order of the projects should be "A01,...,A09,B01,...B07,Q01,...Q04", as shown in crc_color_circlize.
  3. The amount of edges from one vertex should always be "one". Right now I have many edges between "B06" and other projects, but those should be 'merged' into one edge going out of "B06". In other words all project-vertices have the same width.

This is my code:

library(circlize)


crc_edges <- read.table(header=TRUE,
                        sep=",",
                        text="from,to
  A01,A02
  A01,B06
  A02,A03
  A02,B05
  A02,B06
  B06,A01
  B06,A07
  B06,Q01
  B06,Q02
  B06,B02
  B06,B03
  B06,B04
  B06,A05
  B06,A09
  B06,B01
  A03,A01
  A05,A04
  A04,B01
  A06,Q02
  A07,Q04")
crc_edges

crc_color_circlize <- c(
          A01 = "#e30066",
          A02 = "#e30066",
          A03 = "#e30066",
          A04 = "#e30066",
          A05 = "#e30066",
          A06 = "#e30066",
          A07 = "#e30066",
          A08 = "#e30066",
          A09 = "#e30066",
          B01 = "#00549f",
          B02 = "#00549f",
          B03 = "#00549f",
          B04 = "#00549f",
          B05 = "#00549f",
          B06 = "#00549f",
          B07 = "#00549f",
          Q01 = "#57ab27",
          Q02 = "#57ab27",
          Q03 = "#57ab27",
          Q04 = "#57ab27")

crc_circlize_group <- function(crc_name) {
  crc_circlize_local <- function(crc_name){
    set.seed(123)
    chordDiagram(crc_edges,
                 row.col = crc_color_circlize,
                 grid.col = crc_color_circlize,
                 col =  with(crc_edges,
                             ifelse(from %in% crc_name, "#ffed00",
                                    ifelse(to == crc_name, "#ffed00",
                                           "#ECEDED"))),
                 transparency = 0.5,
                 annotationTrack =  c("grid"),
                 preAllocateTracks = list(track.height = max(strwidth(unlist(dimnames(crc_edges))))))
    # we go back to the first track and customize sector labels
    circos.track(track.index = 1, panel.fun = function(x, y) {
      circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index,
                  facing = "clockwise", niceFacing = TRUE, adj = c(0, 0))
    }, bg.border = NA,
    
    ) # here set bg.border to NA is important
    circos.clear()
  }
  crc_circlize_local(crc_name)
}

# hightlight no edge
crc_circlize_group("")
# highlight edges for one vertex
crc_circlize_group("B06")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文