chordDiagram with highlighted, undirected and limited edges (R)

111 Views Asked by At

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

0

There are 0 best solutions below