rdata-visualizationcirclize

Using circlize::draw.sectors inside a function


I would like to make a RGB color scheme using the circlize package. First, I try to make a function so that I only have to input the color and sectors, and the function will do the job

Here is the code:

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)

When I try to run the code, it returns the following error:

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

How can I make the function corresponds to each list, such as when I call the function, only 'blue' and 'a' get called, followed by 'cyan' and 'b', etc.

Thank you very much in advance


Solution

  • Update: I managed to run the function:

    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)
    

    enter image description here

    To make custom set of sectors and colors (where it doesn't have to be 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)
    

    Of course, sectors and colset need to have the same length

    enter image description here