supplying user defined color in r circlize package

1.8k views Asked by At

I am trying create circle diagram using circlize package in R.

Here example where I use random color:

require(circlize)

circos.par("default.track.height" = 0.1, cell.padding = c(0, 0, 0, 0))
circos.initializeWithIdeogram(plotType = NULL)

bed1 = generateRandomBed(nr = 10)

f = colorRamp2(breaks = c(-1, 0, 1), colors = c("green", "black", "red"))

rand_col = function(k) {
    return(rgb(runif(k), runif(k), runif(k)))
}

circos.genomicTrackPlotRegion(bed1, stack = FALSE,  numeric.column=4, panel.fun = function(region,  value, ...) {
    circos.genomicRect(region, value, col = rand_col(nrow(region)), border = NA, ...)
})

Will lead to figure like this:

enter image description here

Now I would like to pass own colors - not random as generated in previous case.

bed1$clr <- sample(c("red", "green", "blue", "purple", "red",
              "yellow", "blue", "red", "green", "cyan"), nrow(bed1), replace=TRUE)

circos.genomicTrackPlotRegion(bed1, stack = FALSE,  numeric.column=4, panel.fun = function(region,  value,  ...) {
    circos.genomicRect(region, value, col = bed1$clr, border = NA, ...)
})

But hit error:

Error in .normalizeGraphicalParam(col, 1, nr, "col") : 
  The length of `col` (19) should be equal to 1 or the number of your regions (1).
2

There are 2 answers

0
Zuguang Gu On BEST ANSWER

in panel.fun, region and value correspond to the data on each chromosome, so you cannot use bed1$clr because bed1$clr corresponds to the whole data.

actually, in panel.fun, value contains columns excluding the first three columns in bed1 and only contains data for the current chromosome (remember panel.fun applied on each chromosome), so you can change your code to:

circos.genomicTrackPlotRegion(bed1, panel.fun = function(region,  value,  ...) {
    circos.genomicRect(region, value, col = value$clr, border = NA, ...)
})

BTW, replace default.track.height to track.height in circos.par if you are using the latest version of circlize (0.1.3).

0
P_sv On

You can create a new column in which each row is associated with one color or different values ​​in the same color. For example, you can create a empty vector, and the use of a loop that parses all your data.frame. In this case, when the loop finds a value == 1, it adds the color "black" to the vector, when finds a value == 2, it adds the colour "red",and so on.

vector <- c()
for(i in seq(nrow(test))){
  if (test$Color[i] == 1){
  vector <- c(vector, "black")}
  if (test$Color[i] == 2){
  vector <- c(vector, "red")}
}

After, you can add the column in your original data.frame like this:

test["paint"] <- vector

To end, you can add in the border option, the column with your selected colours.

circos.par("track.height" = 0.1, cell.padding = c(0, 0, 0, 0))
circos.initializeWithIdeogram(chromosome.index = chrom)
bed = test
circos.genomicTrackPlotRegion(test, panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, border = bed$paint)
})