美文网首页R ggplot基因组学绘图技巧
R 数据可视化 —— circlize chordDiagram

R 数据可视化 —— circlize chordDiagram

作者: 名本无名 | 来源:发表于2021-06-07 09:00 被阅读0次

    前言

    默认风格的 chordDiagram() 已经可以满足大部分需求了,下面我们介绍一些更高级别的图形配置

    使用

    1. 轨迹的排布

    chordDiagram() 函数默认会创建两个轨迹,一个表示标签另一个为扇形和轴

    例如,创建如下图

    mat <- matrix(sample(18, 18), 3, 6) 
    rownames(mat) <- paste0("S", 1:3)
    colnames(mat) <- paste0("E", 1:6)
    chordDiagram(mat)
    circos.clear()
    

    可以获取该图像对象的信息

    > circos.info()
    All your sectors:
    [1] "S1" "S2" "S3" "E1" "E2" "E3" "E4" "E5" "E6"
    
    All your tracks:
    [1] 1 2
    
    Your current sector.index is E6
    Your current track.index is 2
    

    可以看到,有两个轨迹,这两个轨迹是由 annotationTrack 参数来控制的。可选的值为:gridnameaxis,其中 axis 必须在设置了 grid 的情况下才能使用

    轨迹的的高度可以由 annotationTrackHeight 参数控制,可是相对于半径的大小或者使用 mm_h() 函数表示的绝对单位。例如

    library(RColorBrewer)
    grid.col <- brewer.pal(9, "Set1")
    
    op <- par(no.readonly = TRUE)
    
    par(mfrow = c(1, 3))
    chordDiagram(mat, grid.col = grid.col, 
      annotationTrack = "grid"
    )
    chordDiagram(mat, grid.col = grid.col, 
      annotationTrack = c("name", "grid"),
      annotationTrackHeight = c(0.03, 0.01)
    )
    chordDiagram(mat, grid.col = grid.col, 
      annotationTrack = NULL
    )
    
    par(op)
    

    可以在绘制和弦图之前,添加空的轨迹,在绘制完和弦图之后在空轨迹中添加自定义图形。由 preAllocateTracks 参数来控制空轨迹的数量和图形属性,例如

    > chordDiagram(mat, preAllocateTracks = 2)
    > circos.info()
    All your sectors:
    [1] "S1" "S2" "S3" "E1" "E2" "E3" "E4" "E5" "E6"
    
    All your tracks:
    [1] 1 2 3 4
    
    Your current sector.index is E6
    Your current track.index is 4
    

    这些预先放置的轨迹的默认设置为

    list(ylim = c(0, 1),
       track.height = circos.par("track.height"),
       bg.col = NA,
       bg.border = NA,
       bg.lty = par("lty"),
       bg.lwd = par("lwd")
    )
    

    可以覆盖这些默认值

    chordDiagram(
      mat, annotationTrack = NULL,
      preAllocateTracks = list(track.height = 0.3)
    )
    

    如果要放置多个轨迹,可以使用嵌套 list 的形式设置每个轨迹的属性值

    chordDiagram(
      mat, annotationTrack = NULL,
      preAllocateTracks = list(
        list(track.height = 0.1),
        list(bg.border = "black")
      )
    )
    

    2. 自定义扇形标签

    chordDiagram() 函数中,没有对扇形标签进行控制的参数,我们可以使用预先放置的空轨迹来个性化轴标签。例如

    # 添加一个空轨迹
    chordDiagram(
      mat, grid.col = grid.col, annotationTrack = "grid", 
      preAllocateTracks = list(
        track.height = max(strwidth(unlist(dimnames(mat))))
      )
    )
    # 在空轨迹中放置文本标签
    circos.track(
      track.index = 1, panel.fun = function(x, y) {
        circos.text(
          CELL_META$xcenter, CELL_META$ylim[1], 
          CELL_META$sector.index,  facing = "clockwise", 
          niceFacing = TRUE, adj = c(0, 0.5)
        )
      }, bg.border = NA
    )
    

    也可以直接将标签放置到格子里面

    chordDiagram(
      mat, grid.col = grid.col, 
      annotationTrack = c("grid", "axis"), 
      annotationTrackHeight = mm_h(5)
    )
    for(si in get.all.sector.index()) {
      xlim = get.cell.meta.data(
        "xlim", sector.index = si, track.index = 1
      )
      ylim = get.cell.meta.data(
        "ylim", sector.index = si, track.index = 1
      )
      circos.text(
        mean(xlim), mean(ylim), si, sector.index = si, 
        track.index = 1,  facing = "bending.inside", 
        niceFacing = TRUE, col = "white"
      )
    }
    

    添加条件判断,将角度小于 10 度的扇形设置为不同的颜色和文本朝向

    set.seed(12345)
    mat2 <- matrix(rnorm(100), 10)
    
    chordDiagram(
      mat2, annotationTrack = "grid", 
      preAllocateTracks = list(
        track.height = max(strwidth(unlist(dimnames(mat))))
      )
    )
    circos.track(
      track.index = 1, panel.fun = function(x, y) {
        xlim = get.cell.meta.data("xlim")
        xplot = get.cell.meta.data("xplot")
        ylim = get.cell.meta.data("ylim")
        sector.name = get.cell.meta.data("sector.index")
        
        if (abs(xplot[2] - xplot[1]) < 10) {
          circos.text(
            mean(xlim), ylim[1], sector.name,
            facing = "clockwise", niceFacing = TRUE,
            adj = c(0, 0.5), col = "red"
          )
        } else {
          circos.text(
            mean(xlim), ylim[1], sector.name,
            facing = "inside", niceFacing = TRUE,
            adj = c(0.5, 0), col = "blue"
          )
        }
      }, bg.border = NA
    )
    

    3. 自定义扇形的轴

    类似的,我们也可以对轴进行个性化设置,例如

    chordDiagram(
      mat, grid.col = grid.col, 
      annotationTrack = "grid", 
      preAllocateTracks = list(track.height = mm_h(5))
    )
    for(si in get.all.sector.index()) {
      circos.axis(
        h = "top", labels.cex = 0.3, 
        sector.index = si, track.index = 2
      )
    }
    

    我们可以在第一条空白轨迹中添加另一个百分比坐标轴

    circos.track(
      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")
        xplot = get.cell.meta.data("xplot")
        # 添加点线
        circos.lines(xlim, c(mean(ylim), mean(ylim)), lty = 3)
        # 大于 30 度的间隔为 0.2,小于 30 度的间隔为 0.5
        by = ifelse(abs(xplot[2] - xplot[1]) > 30, 0.2, 0.5)
        # 绘制百分比刻度标签
        for (p in seq(by, 1, by = by)) {
          circos.text(
            p * (xlim[2] - xlim[1]) + xlim[1],
            mean(ylim) + 0.1,
            paste0(p * 100, "%"),
            cex = 0.3,
            adj = c(0.5, 0),
            niceFacing = TRUE
          )
        }
        # 添加扇形标签
        circos.text(
          mean(xlim), 1, sector.name, 
          niceFacing = TRUE, adj = c(0.5, 0)
        )
      }, bg.border = NA)
    circos.clear()
    

    4. 水平或竖直对称放置

    如果和弦图只包含两个分组,则将其放置在水平或竖直对称的位置会比较好看些

    par(mfrow = c(1, 2))
    circos.par(start.degree = 0)
    chordDiagram(mat, grid.col = grid.col, big.gap = 20)
    abline(h = 0, lty = 2, col = "#fdb46280", lwd = 2)
    circos.clear()
    
    circos.par(start.degree = 90)
    chordDiagram(mat, grid.col = grid.col, big.gap = 20)
    abline(v = 0, lty = 2, col = "#fdb46280", lwd = 2)
    circos.clear()
    

    5. 和弦图的对比

    和弦图的连接的大小是与其在矩阵中的值相关的,会自动根据值的大小来调整。如果要比较两个和弦图,则要将两个图的的单位宽度设置在同一标度。

    我们可以设置类之间的间距,例如

    mat1 <- matrix(sample(20, 25, replace = TRUE), 5)
    chordDiagram(
      mat1, directional = 1, grid.col = rep(1:5, 2), 
      transparency = 0.5, big.gap = 10, small.gap = 1
    )
    

    然后,使用 calc_gap() 计算在相同的标度下第二个和弦图的类之间的间距

    mat2 <- mat1 / 2
    gap <- calc_gap(mat1, mat2, big.gap = 10, small.gap = 1)
    chordDiagram(
      mat2, directional = 1, grid.col = rep(1:5, 2), 
      transparency = 0.5, big.gap = gap, small.gap = 1
    )
    

    6. 多分组和弦图

    0.4.10 版本之后,chordDiagram() 函数有一个新的 group 参数,可以很方便的创建多组和弦图

    我们先创建多分组随机数据

    mat1 <- matrix(rnorm(25), nrow = 5)
    rownames(mat1) <- paste0("A", 1:5)
    colnames(mat1) <- paste0("B", 1:5)
    
    mat2 <- matrix(rnorm(25), nrow = 5)
    rownames(mat2) <- paste0("A", 1:5)
    colnames(mat2) <- paste0("C", 1:5)
    
    mat3 <- matrix(rnorm(25), nrow = 5)
    rownames(mat3) <- paste0("B", 1:5)
    colnames(mat3) <- paste0("C", 1:5)
    
    mat <- matrix(0, nrow = 10, ncol = 10)
    rownames(mat) <- c(rownames(mat2), rownames(mat3))
    colnames(mat) <- c(colnames(mat1), colnames(mat2))
    mat[rownames(mat1), colnames(mat1)] <- mat1
    mat[rownames(mat2), colnames(mat2)] <- mat2
    mat[rownames(mat3), colnames(mat3)] <- mat3
    

    创建分组变量,扇形标签作为命名向量的名称

    nm <- unique(unlist(dimnames(mat)))
    group <- structure(gsub("\\d", "", nm), names = nm)
    

    绘制和弦图

    grid.col <- structure(
      c(rep("#fb8072", 5), rep("#80b1d3", 5), rep("#fdb462", 5)),
      names = c(paste0("A", 1:5), paste0("B", 1:5), paste0("C", 1:5))
      )
    chordDiagram(mat, group = group, grid.col = grid.col)
    circos.clear()
    

    或者根据数值进行分组

    > group <- structure(gsub("^\\w", "", nm), names = nm)
    > group
     A1  A2  A3  A4  A5  B1  B2  B3  B4  B5  C1  C2  C3  C4  C5 
    "1" "2" "3" "4" "5" "1" "2" "3" "4" "5" "1" "2" "3" "4" "5" 
    

    绘制和弦图

    chordDiagram(mat, group = group, grid.col = grid.col)
    circos.clear()
    

    如果要对扇形进行排序,可以使用 factor

    group <- structure(gsub("\\d", "", nm), names = nm)
    group <- factor(
      group[sample(length(group), length(group))], 
      levels = c("C", "A", "B")
    )
    
    chordDiagram(mat, group = group, grid.col = grid.col)
    circos.clear()
    

    使用 big.gap 控制组间的间距,small.gap 控制组内间距

    group <- structure(gsub("\\d", "", nm), names = nm)
    chordDiagram(
      mat, group = group, grid.col = grid.col, 
      big.gap = 20, small.gap = 5
    )
    circos.clear()
    

    结合前面的方式,可以为每个分组在外围添加一圈表示分组的格子

    group <- structure(gsub("\\d", "", nm), names = nm)
    # 最外层添加一个空白轨迹
    chordDiagram(
      mat, group = group, grid.col = grid.col, 
      annotationTrack = c("grid", "axis"),
      preAllocateTracks = list(
        track.height = mm_h(4),
        track.margin = c(mm_h(4), 0)
      )
    )
    # 将扇形标签放置在格子中
    circos.track(
      track.index = 2, 
      panel.fun = function(x, y) {
        sector.index = get.cell.meta.data("sector.index")
        xlim = get.cell.meta.data("xlim")
        ylim = get.cell.meta.data("ylim")
        circos.text(
          mean(xlim), mean(ylim),
          sector.index, cex = 0.6,
          niceFacing = TRUE
          )
      }, 
      bg.border = NA
    )
    
    # 高亮最外层的扇形格子
    highlight.sector(
      rownames(mat1), track.index = 1, col = "#fb8072", 
      text = "A", cex = 0.8, text.col = "white", 
      niceFacing = TRUE
    )
    highlight.sector(
      colnames(mat1), track.index = 1, col = "#80b1d3", 
      text = "B", cex = 0.8, text.col = "white", 
      niceFacing = TRUE
    )
    highlight.sector(
      colnames(mat2), track.index = 1, col = "#fdb462", 
      text = "C", cex = 0.8, text.col = "white", 
      niceFacing = TRUE
    )
    circos.clear()
    

    相关文章

      网友评论

        本文标题:R 数据可视化 —— circlize chordDiagram

        本文链接:https://www.haomeiwen.com/subject/ukxheltx.html