从 circlize 包中对齐 chordDiagram 中的标签
Aligning labels in a chordDiagram from circlize package
我有一个相当复杂的 circos 图并且在对齐标签时遇到了一些问题。
为了演示这个问题,我在下面提供了一个简短的可重现示例。
我不知道如何很好地对齐每个标签,使它们与相应的段的距离完全相同,例如根据霸王龙标签。许多标签是隐藏和重叠的,并且与绘图边缘的距离不同。任何帮助将不胜感激。
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)
这个问题已经answered here by the package author of circlize
在此重新发帖回答以上问题-
主要问题是circos.axis()
中track.index = 2
的设置改变了当前曲目。
将 y 轴上的位置更改为 ylim[1] + cm_h(2)
。这意味着 y = ylim[1]
偏移了 2cm。 (图上好像不是真的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)
我有一个相当复杂的 circos 图并且在对齐标签时遇到了一些问题。
为了演示这个问题,我在下面提供了一个简短的可重现示例。
我不知道如何很好地对齐每个标签,使它们与相应的段的距离完全相同,例如根据霸王龙标签。许多标签是隐藏和重叠的,并且与绘图边缘的距离不同。任何帮助将不胜感激。
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)
这个问题已经answered here by the package author of circlize
在此重新发帖回答以上问题-
主要问题是circos.axis()
中track.index = 2
的设置改变了当前曲目。
将 y 轴上的位置更改为 ylim[1] + cm_h(2)
。这意味着 y = ylim[1]
偏移了 2cm。 (图上好像不是真的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)