jokergoo / circlize

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

Wrap text of sectors names in order not to be cut off #328

Open elpavlos opened 2 years ago

elpavlos commented 2 years ago

Hi,

The code used to end up with the attached plot chordDiagram_test

is the following:

svg(filename = "../results/chordDiagram_test.svg", width = 16, height = 18)
chordDiagram(adjacencyData, 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), cex = 1)
  #circos.axis(h = "top", labels.cex = 0.2, sector.index = sector.name, track.index = 2, labels.niceFacing = TRUE)
}, bg.border = NA)
dev.off()

I was wondering if there is a way to wrap long text used for sector names.

jokergoo commented 2 years ago

You can set the global option circle.margin in the beginning of your code:

circos.par$circle.margin = c(0.5, 0.5, 0.5, 0.5)
...

Note the radius of the circle is 1, so you will know what does 0.5 correspond to.

elpavlos commented 2 years ago

Thanks for the tip, it works!

Other than this, there is no way to wrap the text only for the very long labels?

jokergoo commented 2 years ago

Maybe you can manually wrap it like "Diabetes\nmellitus"?

rbutleriii commented 3 weeks ago

@elpavlos you can use str_wrap from the stringr package:

# term name labels
circos.labels(
  sectors = a$dx, 
  x = a$x - 0.5, 
  labels = str_wrap(a$term_name, width = 20), 
  side = "outside"
)

image