jokergoo / circlize

Circular visualization in R
http://jokergoo.github.io/circlize_book/book/
Other
959 stars 141 forks source link

Merging sectors on a different track index #383

Closed nfancy closed 7 months ago

nfancy commented 7 months ago

Hi,

Thank you for this amazing package. I was wondering whether there is any way to merge multiple sectors in a different track index? For example, as you've shown we can use highlight.sectors to add annotation for one group of sectors. However, highlight sector does not provide much scope. I wanted to add additional texts/plots in another track. See in the attached, I've added the word "test" for sector A3, B3, C3 because I wanted that for all of the sectors from one group and clockwise. Highlight sector does not allow to face text clockwise. Further, if I wanted to use circos.trackPlotRegion it will work for each sector but not a group of sectors. Is there a way? I am attaching my codes.

mat1 = matrix(rnorm(25), nrow = 5)
rownames(mat1) = paste0("A", 1:5)
colnames(mat1) = paste0("B", 1:5)

mat2 = matrix(rnorm(25), nrow = 5)
rownames(mat2) = paste0("A", 1:5)
colnames(mat2) = paste0("C", 1:5)

mat3 = matrix(rnorm(25), nrow = 5)
rownames(mat3) = paste0("B", 1:5)
colnames(mat3) = paste0("C", 1:5)

mat = matrix(0, nrow = 10, ncol = 10)
rownames(mat) = c(rownames(mat2), rownames(mat3))
colnames(mat) = c(colnames(mat1), colnames(mat2))
mat[rownames(mat1), colnames(mat1)] = mat1
mat[rownames(mat2), colnames(mat2)] = mat2
mat[rownames(mat3), colnames(mat3)] = mat3

nm = unique(unlist(dimnames(mat)))

circos.par(track.height =  mm_h(4))
chordDiagram(mat, group = group, grid.col = grid.col,
             annotationTrack = c("grid"),
             preAllocateTracks = 3)
circos.track(track.index = 3, panel.fun = function(x, y) {
  sector.index = get.cell.meta.data("sector.index")
  xlim = get.cell.meta.data("xlim")
  ylim = get.cell.meta.data("ylim")
  circos.text(x = mean(xlim),
              y =  mean(ylim),
              labels =  sector.index, cex = 0.6, niceFacing = TRUE)
}, bg.border = NA)

highlight.sector(rownames(mat1), track.index = 2, col = "red", 
                 text = "A", cex = 0.8, text.col = "white", niceFacing = TRUE,
                 facing = "outside")
highlight.sector(colnames(mat1), track.index = 2, col = "green", 
                 text = "B", cex = 0.8, text.col = "white", niceFacing = TRUE,
                 facing = "clockwise")
highlight.sector(colnames(mat2), track.index = 2, col = "blue", 
                 text = "C", cex = 0.8, text.col = "white", niceFacing = TRUE)

selected_sector <- tapply(nm, sub("\\d", "", nm), function(x) x[length(x) %/% 2 + 1])

circos.track(track.index = 1, panel.fun = function(x, y) {
  sector.index = get.cell.meta.data("sector.index")
  xlim = get.cell.meta.data("xlim")
  ylim = get.cell.meta.data("ylim")

  if(sector.index %in% selected_sector ){
  circos.text(x = mean(xlim),
              y =  mean(ylim),
              labels =  "test", 
              facing = "clockwise",
              cex = 0.6, niceFacing = TRUE)
  }
}, bg.border = NA)

# Clear the circos plot
circos.clear()

Thanks in advance.

image

nfancy commented 7 months ago

Never mind, I managed to find a way. Thanks.