rchord-diagram

Making chord diagram in R with orginal rownames


How to put rownames from original data into chord diagram.

The following link is for my dataset:

https://docs.google.com/spreadsheets/d/1HdjZvWvkt1YuFuqNaiCp9OvmhX1nRohqswkxroZVjW8/edit?usp=sharing

I tried the following code:

library(circlize)
library(readr)
df <- read_csv("chord_diag_final.csv", show_col_types = FALSE)
rownames(df) <- df[[1]]
df <- df[,-1]
print(rownames(df))
data <- as.matrix(df)

chordDiagram(as.data.frame(data), annotationTrack = "grid", preAllocateTracks = 1)
circos.trackPlotRegion(track.index = 2, 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(mean(xlim), ylim[1] + 2.5, sector.name, 
              facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex=0.6)
  
  circos.axis(h = "top", labels.cex = 0.5, major.tick.percentage = 0.2, 
              sector.index = sector.name, track.index = 2)
}, bg.border = NA)

I got the following Diagram: Chord Diagram

Instead of R1, R2, R3, i want the original rownames as labels for the sectors.

Thanks in advance!!


Solution

  • Tidy doesn't like rownames, use read.csv instead. To illustrate the problem, compare these outputs:

    # example csv
    x <- "c1,c2,c3
    r1,22,33
    r2,44,55"
    

    Using read_csv, rownames can be assigned with a warning, but rownames gets dropped when we drop the column 1:

    d <- read_csv(x)
    rownames(d)
    # [1] "1" "2"
    rownames(d) <- d$c1
    # Warning message:
    #   Setting row names on a tibble is deprecated. 
    rownames(d)
    # [1] "r1" "r2"
    d <- d[, -1 ]
    rownames(d)
    # [1] "1" "2"
    

    Using read.csv, keeps rownames when column 1 is dropped.

    d <- read.csv(text = x)
    rownames(d)
    # [1] "1" "2"
    rownames(d) <- d$c1
    rownames(d)
    # [1] "r1" "r2"
    d <- d[, -1 ]
    rownames(d)
    # [1] "r1" "r2"