【问题标题】:Using circlize::draw.sectors inside a function在函数中使用 circlize::draw.sectors
【发布时间】:2021-07-27 13:46:28
【问题描述】:

我想使用 circlize 包制作 RGB 配色方案。首先,我尝试制作一个函数,这样我只需要输入颜色和扇区,该函数就可以完成这项工作

代码如下:

colorpallete <- function(color, x){
    draw.sector(get.cell.meta.data("cell.start.degree", sector.index = x),
        get.cell.meta.data("cell.end.degree", sector.index = x),
        rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
        rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 3),
            col = adjustcolor(col = color, alpha.f = 0.2))

    draw.sector(get.cell.meta.data("cell.start.degree", sector.index = x),
        get.cell.meta.data("cell.end.degree", sector.index = x),
        rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
        rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 2),
            col = adjustcolor(col = color, alpha.f = 0.2))
            
    draw.sector(get.cell.meta.data("cell.start.degree", sector.index = x),
        get.cell.meta.data("cell.end.degree", sector.index = x),
        rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
        rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 1),
            col = adjustcolor(col = color, alpha.f = 0.2))
    }

circos.initialize(sectors, xlim = c(0, 1))
        for(i in 1:3) {
            circos.track(ylim = c(0, 1))
            }
colset = (c('blue', 'cyan', 'green', 'yellow', 'red', 'violet'))
sectors = letters[1:6]    
colorpallete(colset, sectors)

当我尝试运行代码时,它返回以下错误:

 Error: Length of `sector.index` should only be 1.

如何让函数对应每个列表,比如我调用函数时,只调用了'blue'和'a',后面跟着'cyan'和'b'等

非常感谢您

【问题讨论】:

    标签: r data-visualization circlize


    【解决方案1】:

    更新:我设法运行了该功能:

    colorpallete <- function(sectors, color){
            circos.initialize(sectors, xlim = c(0, 1))
            for(i in 1:3) {
                circos.track(ylim = c(0, 1))
                }
            for(i in 1:6){
            draw.sector(get.cell.meta.data("cell.start.degree", sector.index = sectors[i]),
                get.cell.meta.data("cell.end.degree", sector.index = sectors[i]),
                rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
                rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 3),
                    col = adjustcolor(col = color[i], alpha.f = 0.2))
        
            draw.sector(get.cell.meta.data("cell.start.degree", sector.index = sectors[i]),
                get.cell.meta.data("cell.end.degree", sector.index = sectors[i]),
                rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
                rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 2),
                    col = adjustcolor(col = color[i], alpha.f = 0.2))
                    
            draw.sector(get.cell.meta.data("cell.start.degree", sector.index = sectors[i]),
                get.cell.meta.data("cell.end.degree", sector.index = sectors[i]),
                rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
                rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 1),
                    col = adjustcolor(col = color[i], alpha.f = 0.2))
            } circos.clear()}
    sectors = letters[1:6]
    colset = (c('blue', 'cyan', 'green', 'yellow', 'red', 'violet'))        
    colorpallete(sectors, colset)
    

    制作自定义的扇区和颜色集(不必为 6 个)

    colorpallete <- function(sectors, color){
        if(length(sectors) != length(color)){
            print("Error: sectors should have the same length as color")
            } else {
            circos.initialize(sectors, xlim = c(0, 1))
            for(i in 1:3) {
                circos.track(ylim = c(0, 1))
                }
            for(i in 1:length(sectors)){
                draw.sector(get.cell.meta.data("cell.start.degree", sector.index = sectors[i]),
                get.cell.meta.data("cell.end.degree", sector.index = sectors[i]),
                rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
                rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 3),
                    col = adjustcolor(col = color[i], alpha.f = 0.2))
    
            draw.sector(get.cell.meta.data("cell.start.degree", sector.index = sectors[i]),
                get.cell.meta.data("cell.end.degree", sector.index = sectors[i]),
                rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
                rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 2),
                    col = adjustcolor(col = color[i], alpha.f = 0.2))
                
            draw.sector(get.cell.meta.data("cell.start.degree", sector.index = sectors[i]),
                get.cell.meta.data("cell.end.degree", sector.index = sectors[i]),
                rou1 = get.cell.meta.data("cell.top.radius", track.index = 1),
                rou2 = get.cell.meta.data('cell.bottom.radius', track.index = 1),
                    col = adjustcolor(col = color[i], alpha.f = 0.2))
                }
        circos.clear()
        }}
    sectors = letters[1:4]
    colset = (c('darkblue', 'blue', 'darkred', 'red'))
    colorpallete(sectors, colset)
    

    当然,sectorscolset 需要有相同的长度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多