0

我有一个相当复杂的马戏团情节,并且在对齐标签时遇到了一些麻烦。

为了演示这个问题,我在下面提供了一个简短的可重现示例。

我无法弄清楚如何很好地对齐每个标签,以便它们与相应的段的距离完全相同,例如根据标签 tyranosauras rex。许多标签是隐藏的、重叠的,并且与绘图边缘的距离不同。任何帮助将不胜感激。

library("circlize")
circos.clear()

# create data
somelongnames <- c("homo sapiens", "arabidopsis thaliania", "Tyrannosaurus rex",
                  "some other long name", letters[seq(4)])

df <- data.frame(x = somelongnames,
                 y = c("this label is very far away from the plot", "Golgi", 
                       letters[13:18]),
                 count = c(2, 10, 4, 5, 5, 1, 9, 3))

# set colours
ll <- unique(c(df$x, df$y))
grid.col <- rainbow(length(ll))
grid.col <- setNames(grid.col, ll)

# create plot
par(mar = c(1,1,1,1)*12, cex = 0.6, xpd=NA)

chordDiagram(df, annotationTrack = "grid", 
             preAllocateTracks = 1, 
             grid.col = grid.col,
             directional = 1, 
             direction.type = c("diffHeight", "arrows"), 
             link.arr.type = "big.arrow")

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.5, 0.1),
              cex = 1,
              col=grid.col[sector.name],
              font = 2)
  circos.axis(h = "top",
              labels.cex = .6,
              major.tick.length = 1,
              sector.index = sector.name,
              track.index = 2)
}, bg.border = NA)

在此处输入图像描述

4

1 回答 1

0

circlize 的包作者在这里已经回答了这个问题

在这里重新发布以回答上述问题-

主要问题是更改当前曲目的设置track.index = 2circos.axis()

将 y 轴上的位置更改为ylim[1] + cm_h(2)。这意味着 2cm 的偏移量y = ylim[1]。(图上好像不是真的2cm,但是你可以手动改变这个值)。 adj改为adj = c(0, 0.5)。注意 的值adj乘以文本长度的长度,因此所有扇区名称的物理长度并不相同。删除major.tick.length = 1,因为我认为默认的刻度长度应该看起来不错。

下面的代码解决了上面的问题

chordDiagram(df, annotationTrack = "grid", 
             preAllocateTracks = 1, 
             grid.col = grid.col,
             directional = 1, 
             direction.type = c("diffHeight", "arrows"), 
             link.arr.type = "big.arrow")

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(CELL_META$xcenter, 
              ylim[1] + cm_h(2), 
              sector.name, 
              facing = "clockwise",
              niceFacing = TRUE, 
              adj = c(0, 0.5),
              cex = 1,
              col=grid.col[sector.name],
              font = 2)
  circos.axis(h = "bottom",
              labels.cex = .6,
              sector.index = sector.name
              )
}, bg.border = NA)

在此处输入图像描述

于 2021-08-17T15:52:35.420 回答