如何在R Circlize中添加轨道标签?

7
使用 R 中的 "circlize" 包,我正在尝试为每个轨道添加标签。在下面的示例中,我希望在扇区 Y 和扇区 1 之间显示 "A. Ideogram"、"B. Expression" 和 "C: Count"。
library(circlize) 

circos.initializeWithIdeogram()


bed = generateRandomBed(nr = 500, fun = function(k) runif(k)*sample(c(-1, 1), k, replace = TRUE))
circos.genomicTrackPlotRegion(bed, ylim = c(-1, 1), panel.fun = function(region, value, ...) {
  col = ifelse(value[[1]] > 0, "red", "green")
  circos.genomicPoints(region, value, col = col, cex = 0.5, pch = 16)
  cell.xlim = get.cell.meta.data("cell.xlim")
  for(h in c(-1, -0.5, 0, 0.5, 1)) {
    circos.lines(cell.xlim, c(h, h), col = "#00000040")
  }
}, track.height = 0.1)

bed = generateRandomBed(nr = 500, fun = function(k) rnorm(k, 0, 50))
circos.genomicTrackPlotRegion(bed, panel.fun = function(region, value, ...) {
  x = (region[[2]] + region[[1]]) / 2
  y = value[[1]]
  loess.fit = loess(y ~ x)
  loess.predict = predict(loess.fit, x, se = TRUE)
  d1 = c(x, rev(x))
  d2 = c(loess.predict$fit + loess.predict$se.fit, rev(loess.predict$fit - loess.predict$se.fit))
  circos.polygon(d1, d2, col = "#CCCCCC", border = NA)
  circos.points(x, y, pch = 16, cex = 0.5)
  circos.lines(x, loess.predict$fit)
}, track.height = 0.1)

circos.clear()

enter image description here


1
你能展示一下期望的输出结果吗?我不确定图例应该放在哪里,因为在 Y1 之间似乎没有空间。 - Cath
@Cath,是的,目前还没有,现在,Storm的解决方案在我玩创建间隙后可以使用。谢谢。 - Ananta
1个回答

7

希望这能帮助到您。新的行已经被注释了。

library(circlize) 

circos.par(gap.after=3)   #Increase gap size
circos.initializeWithIdeogram()

#First label, depending on final plot resolution and gap size you'll have to tune the positions
circos.text(sector.index="chr1",track.index = 1,get.cell.meta.data("cell.xlim")-mean(get.cell.meta.data("cell.xlim"))/2,
            get.cell.meta.data("cell.ylim")-max(get.cell.meta.data("cell.ylim"))/2, labels = "A. Ideogram",facing = "clockwise", 
            niceFacing = TRUE, adj = c(0,0),cex = 0.5)


bed = generateRandomBed(nr = 500, fun = function(k) runif(k)*sample(c(-1, 1), k, replace = TRUE))
circos.genomicTrackPlotRegion(bed, ylim = c(-1, 1), panel.fun = function(region, value, ...) {
  col = ifelse(value[[1]] > 0, "red", "green")
  circos.genomicPoints(region, value, col = col, cex = 0.5, pch = 16)
  cell.xlim = get.cell.meta.data("cell.xlim")

  for(h in c(-1, -0.5, 0, 0.5, 1)) {
    circos.lines(cell.xlim, c(h, h), col = "#00000040")
  }
}, track.height = 0.1)

#Second label, depending on final plot resolution and gap size you'll have to tune the positions
circos.text(sector.index="chr1",track.index = 2,get.cell.meta.data("cell.xlim")-mean(get.cell.meta.data("cell.xlim"))/2,
            get.cell.meta.data("cell.ylim")-2*max(get.cell.meta.data("cell.ylim")), labels = "B. Expression",facing = "clockwise", 
            niceFacing = TRUE, adj = c(0,0),cex=0.5)


bed = generateRandomBed(nr = 500, fun = function(k) rnorm(k, 0, 50))
circos.genomicTrackPlotRegion(bed, panel.fun = function(region, value, ...) {
  x = (region[[2]] + region[[1]]) / 2
  y = value[[1]]
  loess.fit = loess(y ~ x)
  loess.predict = predict(loess.fit, x, se = TRUE)
  d1 = c(x, rev(x))
  d2 = c(loess.predict$fit + loess.predict$se.fit, rev(loess.predict$fit - loess.predict$se.fit))
  circos.polygon(d1, d2, col = "#CCCCCC", border = NA)
  circos.points(x, y, pch = 16, cex = 0.5)
  circos.lines(x, loess.predict$fit)
}, track.height = 0.1)

#Thrid label, depending on final plot resolution and gap size you'll have to tune the positions
circos.text(sector.index="chr1",track.index = 4,get.cell.meta.data("cell.xlim")-mean(get.cell.meta.data("cell.xlim"))/2,
            get.cell.meta.data("cell.ylim"), labels = "C. Count",facing = "clockwise", niceFacing = TRUE, adj = c(0,0),cex=0.5)



circos.clear()

以下是结果:

标签


感谢@storm提供答案。看起来可以工作。我很惊讶它是不必要的复杂,也许开发人员可以添加一些简单的功能。 - Ananta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接