【问题标题】:How to colour labels in R chordDiagram如何在 R chordDiagram 中为标签着色
【发布时间】:2020-06-11 14:53:57
【问题描述】:

从几年前的this example 开始,我想使用R 中的circlize 包对chordDiagram 中的段标签进行颜色编码。

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

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

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

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 

【问题讨论】:

    标签: r circlize


    【解决方案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 package (v0.3.0) 于 2020 年 6 月 27 日创建

    【讨论】:

      【解决方案2】:

      circos.trackPlotRegion 函数循环遍历其参数 x,每次都调用 panel.function 函数。效果是每次调用都使用col 的第一个值(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)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多