Search code examples
rfont-facegraphingchord-diagramcirclize

Fix axis numbering in Circlize chord diagram and font issues


I have been experimenting with the 'circlize' package but I have been having issues with my axis label spacing. I wanted to customize the axis labelling to have major ticks at a specified intervals. I succeeded using the below code, but my issues are that as I change font size, the outer labels of each sector of the chord diagram, move in (a white space issue I believe). I cant find anything through my searching and need some help to fix this issue. The larger the font size, the more drastic the labels move inwards.

Additionally, I cannot find out how to change the font to 'Calibri' or bold (I have tried extrafonts and showtext but may not be implementing it correctly). Any thoughts would be appreciated.

Here is a reproducible example:

#sample matrix
A<-c(4680,0,0,0,0,0,0,0,0,0)
B<-c(109,23323,0,0,0,0,0,0,0,0)
C<-c(9,12,5405,0,0,0,0,0,0,0)
D<-c(19,25,26,4615,0,0,0,0,0,0)
E<-c(108,816,32,38,5511,0,0,0,0,0)
F_1<-c(50,319,67,55,202,18198,0,0,0,0)
G<-c(15,11,8,178,11,18,38279,0,0,0)
H<-c(24,180,22,39,171,212,14,10673,0,0)
I<-c(211,2378,18,28,1603,251,25,188,34458,0)
J<-c(162,1623,31,48,1003,370,34,265,5319,27235)
m <- rbind(A,B,C,D,E,F_1,G,H,I,J)
row.names(m) <- LETTERS[seq(1,10,1)]
colnames(m)<- LETTERS[seq(1,10,1)]

The graph:

colfunc <- colorRampPalette(c("#856705", "#d3d3d3", "#113069"))
grid.col <- setNames(colfunc(10), LETTERS[seq(1,10,1)])
grid.col

library(showtext); library(circlize)
  #my attempt to load the correct font but I am unsure how to call the font within circlize.
    
font_add(family = "Calibri", regular = "C:/Windows/Fonts/calibri.ttf", italic ="C:/Windows/Fonts/calibrii.ttf", bold =  "C:/Windows/Fonts/calibrib.ttf", bolditalic = "C:/Windows/Fonts/calibriz.ttf")
circos.clear()
  
showtext_auto()
    
circos.par(start.degree = 90, gap.degree = 5)
par(family = "Calibri") #tried to set the family to "Calibri" 
    
chordDiagram(m/1000,
             transparency = 0.5,
             grid.col = grid.col,
             annotationTrack = "grid",
             self.link = 1,
             link.lwd = 0.5,    # Line width
             link.lty = 1, 
             link.border = "light grey",
             annotationTrackHeight = mm_h(10))
    
    
for(si in get.all.sector.index()) {
  circos.axis(h = 'top',
              major.at = c(0,5,10,15,20,25,30,35,40,45,50),
              labels = c(0,5,10,15,20,25,30,35,40,45,50),
              minor.ticks = 4,
              labels.cex = fontsize(30),
              sector.index = si,
              track.index = 1,
              labels.niceFacing = TRUE)
      xlim = get.cell.meta.data("xlim", sector.index = si, track.index = 1)
      ylim = get.cell.meta.data("ylim", sector.index = si, track.index = 1)
    circos.text(mean(xlim),
                mean(ylim),
                si,
                sector.index = si,
                track.index = 1,
                niceFacing = TRUE,
                cex = fontsize(50),
                col = "white",
                font = par("Calibri"))
}

dev.copy(png, "test chord diagram.png", width = 5, height = 5, units = "cm", res = 600)
dev.off()
    circos.clear()

With this, I get this graph:

enter image description here

As you can see from the graph, the '0' are all off position of their respective mark. Additionally , sector 'C' and 'D' have their axis labels moved in and are overlapping (including for sector 'J').

So, if anyone has an idea to move the end labels above their respective position, how to change the font to 'Calibri' and to bold, that would be great.

Thank you.


Solution

  • Well it seemed that the labels were fixed by using "labels.pos.adjust = c(0,0)", in the "circos.axis" argument. The only issue left is that the font still needs to be change to calibri.

    enter image description here