1

I am trying to draw a flow map using the circlize library. My code is below and the dataset (i75_from_flow.RData) is here: https://drive.google.com/file/d/0B0hTmthiX5dpbFJFc2hfN1Zqd1k/view

library(reshape)
t1 <- cast(i75_from_flow, ORIGFIPS ~ TERMFIPS)
i75_from_flow2<- data.matrix(subset(t1, select=-c(1)))
rownames(i75_from_flow2) <- t1$ORIGFIPS
colnames(i75_from_flow2) <- colnames(i75_from_flow2)

library(circlize)
circos.par(gap.degree = 8)
chordDiagram(i75_from_flow2, grid.col= length(union(rownames(i75_from_flow2),colnames(i75_from_flow2))), directional = TRUE, annotationTrack = "grid",
         preAllocateTracks = list(list(track.height = 0.05),
                                  list(track.height = 0.05)))

circos.trackPlotRegion(track.index = 1, panel.fun = function(x, y) {
xlim = get.cell.meta.data("xlim")
ylim = get.cell.meta.data("ylim")
sector.index = get.cell.meta.data("sector.index")
circos.text(mean(xlim), mean(ylim), sector.index, facing = "inside", niceFacing = TRUE)
}, bg.border = NA)

When I run the above code I get the following error following the chordDiagram and circos.trackPlotRegion lines in the code.

Error in .CELL.DATA[[1]] : subscript out of bounds

My goal is to get an image that shows the matrix cells as flows between the ORIGFIPS and TERMFIPS.

My question is how to fix error in cell data issue. Should the matrix be symmetric for circlize to work properly? It is not always the case in this effort.

Krishnan
  • 1,265
  • 2
  • 13
  • 24

1 Answers1

5

It is because i75_from_flow2 contains NA values. Add following line:

i75_from_flow2[is.na(i75_from_flow2)] = 0
Zuguang Gu
  • 1,301
  • 7
  • 11
  • I feel ridiculous now for forgetting this simple step :-). Thanks. – Krishnan Jul 28 '15 at 21:49
  • No problem. Now in the new version of the package, `chordDiagram()` will automatically convert `NA` to 0 if the input is a matrix. – Zuguang Gu Jul 30 '15 at 10:00
  • That is fantastic. Thanks for update. I wrote up a small brief on how to develop these visualizations and would welcome your comments on how to improve it better. http://rpubs.com/matungawalla/circular_matrix – Krishnan Jul 30 '15 at 10:35