0

继几年前的这个例子之后,我想chordDiagram使用circlizeR 中的包对a 中的段标签进行颜色编码。

中的文档?circos.text告诉我,我应该使用col图形par参数中的参数来设置 this。但是,par(col)不接受颜色向量。

有人可以建议如何做到这一点吗?非常感谢。

circlize此处使用R 中的包的示例代码。

library(circlize)
set.seed(999)

## generate example data
mat <- matrix(sample(18, 18), 3, 3)
rownames(mat) <- colnames(mat) <- paste0("A", 1:3)
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
                to = rep(colnames(mat), each = nrow(mat)),
                value = as.vector(mat),
                stringsAsFactors = FALSE)

## set colours for segments
grid.col <- setNames(rainbow(nrow(mat)), rownames(mat))

# now, plot the image with rotated labels
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.5))
  circos.axis(h = "top", 
              labels.cex = 0.5, 
              major.tick.percentage = 0.2, 
              sector.index = sector.name, 
              track.index = 2)
}, bg.border = NA)

这产生了这个漂亮的情节

在此处输入图像描述

如何将标签 A1、A2 和 A3 更改为与其段相同的颜色?par(col = grid.col)不起作用,因为它只需要向量中的一种颜色。非常感谢。

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats4    parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggplot2_3.3.1        reshape2_1.4.4       pRolocdata_1.26.0    pRoloc_1.29.0       
 [5] BiocParallel_1.22.0  MLInterfaces_1.68.0  cluster_2.1.0        annotate_1.66.0     
 [9] XML_3.99-0.3         AnnotationDbi_1.50.0 IRanges_2.22.2       MSnbase_2.14.2      
[13] ProtGenerics_1.20.0  S4Vectors_0.26.1     mzR_2.22.0           Rcpp_1.0.4.6        
[17] Biobase_2.48.0       BiocGenerics_0.34.0  dplyr_1.0.0          circlize_0.4.9      
[21] migest_1.8.1 
4

2 回答 2

1

或者,您可以提供以下颜色grid.col[sector.name]

library(circlize)
set.seed(999)

## generate example data
mat <- matrix(sample(18, 18), 3, 3)
rownames(mat) <- colnames(mat) <- paste0("A", 1:3)
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
                to = rep(colnames(mat), each = nrow(mat)),
                value = as.vector(mat),
                stringsAsFactors = FALSE)

## set colours for segments
grid.col <- setNames(rainbow(nrow(mat)), rownames(mat))

# now, plot the image with rotated labels
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.5),
                col=grid.col[sector.name])
    circos.axis(h = "top", 
                labels.cex = 0.5, 
                major.tick.percentage = 0.2, 
                sector.index = sector.name, 
                track.index = 2)
}, bg.border = NA)

reprex 包(v0.3.0)于 2020-06-27 创建

于 2020-06-27T16:12:19.033 回答
1

circos.trackPlotRegion函数循环遍历其参数,每次都x调用函数。panel.function效果是使用colfor each call( col[1]) 的第一个值,而不是考虑参数中的值向量col

一种解决方案可能是调用circos.trackPlotRegion每个扇区,如下所示:

# replace code after the call to `chordDiagram` with this

for (i in seq_len(nrow(mat))) { # use for loop to label each sector
  myFactor <- rownames(mat)[i] # assuming this defines the sectors
  myCol <- grid.col[i] # defined in the question
  circos.trackPlotRegion(track.index = 1, factor = myFactor,
    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,
        col = myCol,
        facing = "clockwise",
        niceFacing = TRUE, 
        adj = c(-0.5, 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)
}
于 2020-06-20T19:28:54.297 回答