jokergoo / circlize

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

Strange anti-overlap reaction with circos.labels #357

Closed Glastos closed 1 year ago

Glastos commented 1 year ago

Hello,

First and foremost, thanks for this wonderful package.

I have an issue when I try to plot some labels, were at least two of them will show a shift while not overlapping (whatever the cex or the length of the labels I use). The result is the same if I plot in my Rmd or in svg, png or pdf.

Here is a reproductible example and the result I get.

library(circlize)
library(purrr)

sectors_label <- c(rep("Liver",40),
                   rep("Hypohyse", 30),
                   rep("Thyroide", 10),
                   rep("Surrenale", 20))
sectors_pos <- c(1:40, 1:30, 1:10, 1:20)

dat1 <- cbind.data.frame("gene_name" = sample(LETTERS, 100, replace = TRUE),
              "logFC" = runif(100, -1, 1)) %>%
    cbind(sectors_label, sectors_pos, .)

dat2 <- cbind.data.frame("Sector_var1" = rep("Liver",200),
              "Var1_pos" = rdunif(200,40),
              "Sector_var2" = c(rep("Hypohyse", 50),
                                rep("Thyroide", 80),
                                rep("Surrenale", 70)),
              "Var2_pos" = c(rdunif(50,30), rdunif(80,10), rdunif(70,20)),
              "value" = runif(200, -1, 1))

circos.par(start.degree = 90)
circos.initialize(sectors = unique(sectors_label),
                  sector.width = c(40, 30, 10, 20),
                  xlim = matrix(c(1,40,
                                  1,30,
                                  1,10,
                                  1,20), ncol = 2, byrow = T))

circos.track(unique(sectors_label), rep(0,4), rep(0,4), ylim = c(-2, 2),
             panel.fun = function(x, y) {
                 circos.text(CELL_META$xcenter,y, CELL_META$sector.index,
                             facing = "bending.inside",
                             adj = c(0.5,0.5),
                             cex = 0.5)
             },
             bg.col = c("#CCFF33", "#FFCC66", "#CC66FF", "#66CCFF"),
             bg.border = NA,
             track.height = 0.1)

circos.labels(sectors = dat1$sectors_label, x = sectors_pos, labels = dat1$gene_name,
              cex = 0.2, labels_height = 0.13)

circos.clear()

image

Thanks for your help ! Glastos

P.S.: I also have another issue where the information of the second of four sectors (the .SECTOR.DATA) disappear for the second sector of my plot, but I was not able to replicate it in a small example. I will try and come back with another issue if I manage to do it.

jokergoo commented 1 year ago

Yes, that is a limitation of the algorithm I used. Internally I mapped all data only to one sector and all links are drawn in the coordinate system of that sector.

I will try to improve it.

jokergoo commented 1 year ago

Now it is fixed. The coordinates was wrongly calculated in the old version.

image
Glastos commented 1 year ago

Thanks, it is working very well now. :)