rchord-diagramcirclize

chordDiagram() error in number of sectors


I am trying to create a chord diagram using the circlize package, however I am struggling with the circos.par() codes just before creating the plot. I don't think I quite understand my mistakes and how to fix it.

My code below:

#data
subset<- structure(list(from = c("TRBV5-6", "TRBV6-1", "TRAV17", "TRBV14", 
"TRBV7-2", "TRBV15", "TRAV38-1", "TRBV29-1", "TRBV6-5", "TRBV6-6", 
"TRBV28", "TRBV7-8", "TRBV20-1", "TRBV7-8", "TRBV29-1", "TRBV29-1", 
"TRBV9"), to = c("TRAV1-1", "TRAV1-2", "TRAV12-2", "TRAV13-1", 
"TRAV14/DV4", "TRAV17", "TRAV20", "TRAV20", "TRAV25", "TRAV27", 
"TRAV3", "TRAV3", "TRAV38-1", "TRAV38-2/DV8", "TRAV5", "TRAV8-2", 
"TRAV8-2"), value = c(56L, 61L, 66L, 48L, 53L, 67L, 61L, 37L, 
86L, 64L, 38L, 144L, 52L, 56L, 31L, 84L, 188L)), class = "data.frame", row.names = c(NA, 
-17L))

#creating the chord diagram
circos.clear()
gridcols<- brewer.pal(12,"Paired")
gridcols<- colorRampPalette(gridcols)(length(sort(unique(c(subset$from, subset$to)))))
circos.par(gap.after=c(rep(1,length(unique(subset[[1]]))-1),10,
                       rep(1,length(unique(subset[[2]]))-1),10),
           canvas.xlim = c(-1.5, 1.5), # Adjust x-axis limits for more space
           canvas.ylim = c(-1.5, 1.5))
chordDiagram(subset, grid.col = gridcols,annotationTrack = "grid", preAllocateTracks = 1) #this creates the plot w/o labelling

#add label and axis
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")

#print labels
circos.text(mean(xlim), ylim[1] + 2.5, sector.name,
        facing = "clockwise",niceFacing = TRUE, adj = c(0, 0.5), cex = 0.6)

#print axis
circos.axis(h = "top",labels.cex = 0.5,major.tick.length = 0.2,sector.index = sector.name,track.index = 2)
}, bg.border = NA)

I am getting the following error:

Error: Since `gap.degree` parameter has length larger than 1, it should have same length as the number of sectors.

However, when I do not use the circos.par() argument as in the code below, I get the graph:

#data
subset<- structure(list(from = c("TRBV5-6", "TRBV6-1", "TRAV17", "TRBV14", 
"TRBV7-2", "TRBV15", "TRAV38-1", "TRBV29-1", "TRBV6-5", "TRBV6-6", 
"TRBV28", "TRBV7-8", "TRBV20-1", "TRBV7-8", "TRBV29-1", "TRBV29-1", 
"TRBV9"), to = c("TRAV1-1", "TRAV1-2", "TRAV12-2", "TRAV13-1", 
"TRAV14/DV4", "TRAV17", "TRAV20", "TRAV20", "TRAV25", "TRAV27", 
"TRAV3", "TRAV3", "TRAV38-1", "TRAV38-2/DV8", "TRAV5", "TRAV8-2", 
"TRAV8-2"), value = c(56L, 61L, 66L, 48L, 53L, 67L, 61L, 37L, 
86L, 64L, 38L, 144L, 52L, 56L, 31L, 84L, 188L)), class = "data.frame", row.names = c(NA, 
-17L))

#creating the chord diagram
circos.clear()
gridcols<- brewer.pal(12,"Paired")
gridcols<- colorRampPalette(gridcols)(length(sort(unique(c(subset$from, subset$to)))))

chordDiagram(subset, grid.col = gridcols,annotationTrack = "grid", preAllocateTracks = 1) #this creates the plot w/o labelling

#add label and axis
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")

#print labels
circos.text(mean(xlim), ylim[1] + 2.5, sector.name,
        facing = "clockwise",niceFacing = TRUE, adj = c(0, 0.5), cex = 0.6)

#print axis
circos.axis(h = "top",labels.cex = 0.5,major.tick.length = 0.2,sector.index = sector.name,track.index = 2)
}, bg.border = NA)

enter image description here

Two issues with this plot above:

  1. I do not have any gaps between the sectors in the top half and bottom half
  2. The TRAVs and TRBVs are mixed (ideally I want the TRAVs to be at the top and TRBVs at the bottom.

Any help would be much appreciated!


Solution

  • You have TRAV17 and TRAV38-1 in both columns, counting them twice in circos.par. Thus, your gep.degree parameter has length 28, but the number of sectors is 26. This is exactly what the error tells you.

    This will correct the error and your first issue:

    circos.par(gap.after=c(rep(1,length(unique(subset[[1]]))-3),10, # Remove two extra sectors
                           rep(1,length(unique(subset[[2]]))-1),10),
               canvas.xlim = c(-1.5, 1.5), # Adjust x-axis limits for more space
               canvas.ylim = c(-1.5, 1.5))
    

    For the 2nd issue, you need to rearrange the data before drawing the diagram. For example, with:

    subset = subset[order(strtrim(subset$from, 4), decreasing = T),]
    

    UPDATE: Another option to rearrange the sectors without rearranging the data is to use order argument of chordDiagram. For this you can use vector of sector names in the order you need.