我正在尝试使用 circlize 包创建一个和弦图,但是在创建绘图之前我正在努力处理 circos.par() 代码。我认为我不太了解我的错误以及如何解决它。
我的代码如下:
#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)
我收到以下错误:
Error: Since `gap.degree` parameter has length larger than 1, it should have same length as the number of sectors.
但是,当我不使用下面代码中的 circos.par() 参数时,我得到图表:
#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)
上面这个图有两个问题:
任何帮助将不胜感激!
两列中都有 TRAV17 和 TRAV38-1,在 circos.par 中将它们连续两次。因此,您的 gep. Degree 参数的长度为 28,但扇区数为 26。这正是错误告诉您的内容。
这将纠正错误和您的第一个问题:
circos.par(gap.after=c(rep(1,length(unique(subset[[1]]))-3),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))
对于第二个问题,您需要重新排列数据。例如,与:
subset = subset[order(strtrim(subset$from, 4), decreasing = T),]