Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate labels in a chordDiagram (R circlize)

Here is some code from the circlize package for creating a chord diagram.Right now the labels are parallel to the edge of the circle. Is it possible to rotate the labels 90 degrees to they are perpendicular to the circle?

library(circlize)
set.seed(999)
mat = matrix(sample(18, 18), 3, 6)
rownames(mat) = paste0("Start", 1:3)
colnames(mat) = paste0("End", 1:6)
chordDiagram(mat)

In the figure below I manually inserted a few labels to show what I hope to accomplish (End5, End6, End7). Thanks.

enter image description here

like image 325
Brian P Avatar asked Aug 11 '15 13:08

Brian P


People also ask

When to rotate the chord diagram?

In Chord diagram, when there are two groups (which correspond to rows and columns if the input is an adjacency matrix), it is always visually beautiful to rotate the diagram to be symmetric on horizontal direction or vertical direction. See following example: Figure 15.6: Rotate Chord diagram.

How to customize chord diagram in circlize?

Since Chord Diagram is implemented by basic circlize functions, like normal circular plot, the layout can be customized by circos.par (). The gaps between sectors can be set by circos.par (gap.after = ...) (Figure 14.3 ).

How do I change the style of sector labels in chorddiagram?

In chordDiagram (), there is no argument to control the style of sector labels, but this can be done by first pre-allocating an empty track and customizing the labels in it later. In the following example, one track is firstly allocated and a Chord diagram is added without label track and axes.

Why does chorddiagram () change position and orientation of links?

The reason is chordDiagram () automatically optimizes the positions of links according to the arrangement of sectors. Figure 14.5: Change position and orientation of Chord diagram. Grids have different colors to represent different sectors. Generally, sectors are divided into two groups.


1 Answers

Based on your example data, here's one way to do it:

grid.col <- setNames(rainbow(length(unlist(dimnames(mat)))), union(rownames(mat), colnames(mat)))
par(mar = c(0, 0, 0, 0), mfrow = c(1, 2))

# original image
chordDiagram(mat, grid.col = grid.col) 

# now, the image with rotated labels
chordDiagram(mat, annotationTrack = "grid", preAllocateTracks = 1, grid.col = grid.col)
circos.trackPlotRegion(track.index = 1, 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")
  circos.text(mean(xlim), ylim[1] + .1, sector.name, facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5))
  circos.axis(h = "top", labels.cex = 0.5, major.tick.percentage = 0.2, sector.index = sector.name, track.index = 2)
}, bg.border = NA)

Result:

enter image description here

like image 80
lukeA Avatar answered Sep 20 '22 19:09

lukeA