Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R circlize: Error in circos.initialize

I am able to follow the Circlize example in the description of the package on CRAN easily:

library('circlize')
set.seed(123)
mat = matrix(sample(1:100, 18, replace = TRUE), 3, 6)
rownames(mat) = letters[1:3]
colnames(mat) = LETTERS[1:6]
### basic settings
par(mfrow = c(3, 2))
par(mar = c(1, 1, 1, 1))
chordDiagram(mat)

however, when I replace mat with myMatrix I get this error:

Error in circos.initialize(factors = factor(cate, levels = cate), xlim = cbind(rep(0,  : 
  Since `xlim` is a matrix, it should have same number of rows as the length of the level of `factors` and number of columns of 2.

Can somebody explain why I am getting that message? I do not see a difference between mat and myMatrix other than myMatrix is larger:

    A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   A2  B2  C2  D2
A   1060360.659 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
B   0   32143148.75 996976.8445 0   4944648.524 5688385.041 61990.5913  0   0   0   0   -1563.225   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   31922242.6
C   0   0   6342776.843 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
D   0   0   0   28617385.81 17842142.64 0   0   0   0   0   0   0   0   409444.5633 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
E   0   0   0   4990921.202 105686446.3 536246.2188 0   0   0   0   0   0   0   8587899.583 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   378565.5746
F   0   92732.7741  0   4282.9319   33543553.89 36773976.59 1894761.93  0   0   333209.342  0   20739.0655  327956.7365 0   1022673.163 12229.0255  0   0   386112.1743 224039.3207 0   2395066.197 268247.2897 0   0   0   0   0   0   11926701.96
G   0   0   0   0   0   0   7753767.003 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
H   0   0   0   0   0   5184133.29  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
I   0   0   0   0   462767.7374 0   0   0   8992223.296 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
J   0   0   0   0   0   0   0   0   0   1950552.642 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
K   0   0   0   0   891032.5584 0   0   0   0   0   520107.9821 0   0   0   0   0   0   0   0   0   0   0   0   0   0   26724.8402  0   0   0   418902.5203
L   0   0   0   0   32044317.54 28147.5693  0   0   0   0   0   5383919.293 0   489912.5412 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   4559115.003
M   0   0   0   0   0   3125823.41  0   0   0   0   0   0   1738293.164 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
N   0   1053825.966 -8526.9758  1283429.314 60333051.34 2621812.931 -1130.1924  0   -779545.8004    8055145.684 918.8702    -379747.1919    -177.6205   298563606.5 -9316.8654  0   0   0   0   0   2631991.077 0   0   0   0   0   1107369.803 0   0   118812465
O   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1500451.292 0   0   0   0   0   0   0   0   0   0   0   0   0   0   7432418.396
P   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Q   0   0   1496058.76  0   -4056617.74 294503  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   410.4   0   0   0   0   0   0   0   1765984767

Code

dd <- read.table(header = TRUE, text = "  rn  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   A2  B2  C2  D2
A   1060360.659 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 B   0   32143148.75 996976.8445 0   4944648.524 5688385.041 61990.5913  0   0   0   0   -1563.225   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   31922242.6
                 C   0   0   6342776.843 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 D   0   0   0   28617385.81 17842142.64 0   0   0   0   0   0   0   0   409444.5633 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 E   0   0   0   4990921.202 105686446.3 536246.2188 0   0   0   0   0   0   0   8587899.583 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   378565.5746
                 F   0   92732.7741  0   4282.9319   33543553.89 36773976.59 1894761.93  0   0   333209.342  0   20739.0655  327956.7365 0   1022673.163 12229.0255  0   0   386112.1743 224039.3207 0   2395066.197 268247.2897 0   0   0   0   0   0   11926701.96
                 G   0   0   0   0   0   0   7753767.003 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 H   0   0   0   0   0   5184133.29  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 I   0   0   0   0   462767.7374 0   0   0   8992223.296 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 J   0   0   0   0   0   0   0   0   0   1950552.642 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 K   0   0   0   0   891032.5584 0   0   0   0   0   520107.9821 0   0   0   0   0   0   0   0   0   0   0   0   0   0   26724.8402  0   0   0   418902.5203
                 L   0   0   0   0   32044317.54 28147.5693  0   0   0   0   0   5383919.293 0   489912.5412 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   4559115.003
                 M   0   0   0   0   0   3125823.41  0   0   0   0   0   0   1738293.164 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 N   0   1053825.966 -8526.9758  1283429.314 60333051.34 2621812.931 -1130.1924  0   -779545.8004    8055145.684 918.8702    -379747.1919    -177.6205   298563606.5 -9316.8654  0   0   0   0   0   2631991.077 0   0   0   0   0   1107369.803 0   0   118812465
                 O   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1500451.292 0   0   0   0   0   0   0   0   0   0   0   0   0   0   7432418.396
                 P   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
                 Q   0   0   1496058.76  0   -4056617.74 294503  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   410.4   0   0   0   0   0   0   0   1765984767")

myMatrix <- as.matrix(dd[, -1])
rownames(myMatrix) <- dd[, 1]
chordDiagram(myMatrix)
like image 800
user3390169 Avatar asked Jan 27 '16 16:01

user3390169


1 Answers

In the old version of circlize, the matrix must be of a matrix class instead of a data.frame, so you need to convert the data frame explicitly by:

myMatrix = as.matrix(A + B)

In circlize, a data frame is for data stored as a adjacency list (e.g the first column for group1, second column for group2, third column for the strength of the relation).

Since read.table() always returns a data.frame class, in the newer version of circlize, it is fine if the matrix represents as a data frame. When it is a data frame, the chordDiagram() will first check whether the number of columns is larger than 3 and all columns are numeric. If so, it will be converted to a matrix internally.

like image 91
Zuguang Gu Avatar answered Oct 31 '22 21:10

Zuguang Gu