jokergoo / circlize

Circular visualization in R
http://jokergoo.github.io/circlize_book/book/
Other
968 stars 146 forks source link

Color vector in circos.lines not working #309

Closed cgrootcrego closed 2 years ago

cgrootcrego commented 2 years ago

Hello :)

I have been using circlize a lot recently and enjoying it. But there are a couple of things I need help with (perhaps explicit code examples in the user guide could really help).

I am trying to color the area in circos.lines differently for sectors in my plot (they represent chromosomes of two species, so I want different color for each species (25 sectors have color A and 25 sectors have color B). This is the code I have:

gene_counts_per_mb_windows <- read.table(args[[2]], header = T)
print("Darwing first track: Gene density...")
# obtain the number of observations for each species
length_Tfas <- sum(grepl("Tfas", gene_counts_per_mb_windows$chrom))
length_Tlei <- sum(grepl("Tlei", gene_counts_per_mb_windows$chrom))
# Make color vector specifying color for each observation (length of this vector == x in circos. lines)
color_gene_density <- c(rep("seagreen3", length_Tfas), rep("seagreen", length_Tlei))
circos.track(gene_counts_per_mb_windows$chrom, y = gene_counts_per_mb_windows$gene_counts, 
             x = gene_counts_per_mb_windows$start_window, 
             bg.col = "grey92", panel.fun = function(x, y) {
               circos.lines(x, y, area = T, col = color_gene_density)
               circos.yaxis(c("left"), sector.index = "Tfas_chr1", labels = F, at = c(0,70,146),
                            labels.cex = 0.3, labels.col="khaki4", tick.length = 2)
             }, track.height = 0.15, bg.border = "black")
for(sn in get.all.sector.index()) {
  set.current.cell(sector.index = sn, track.index = get.current.track.index())
  breaks = seq(0, CELL_META$ylim[2], by = 50)
  for(b in breaks) {
    circos.lines(CELL_META$cell.xlim, rep(b, 2), lty = 3, col = "#00000040")
  }
}

color_gene_density is a character vector of the same length as gene_counts_per_mb_windows, so it should technically work, but all sectors have the first color (seagreen3). I have no idea what I am doing wrong. Let me know. :)

jokergoo commented 2 years ago

I assume gene_counts_per_mb_windows only contains two sectors ("Tfas" and "Tlei"), then actually you can do:

gene_counts_per_mb_windows <- read.table(args[[2]], header = T)

color_gene_density <- c(Tfas = "seagreen3", Tlei = "seagreen")
circos.track(gene_counts_per_mb_windows$chrom, y = gene_counts_per_mb_windows$gene_counts, 
             x = gene_counts_per_mb_windows$start_window, 
             bg.col = "grey92", panel.fun = function(x, y) {
               circos.lines(x, y, area = T, col = color_gene_density[CELL_META$sector.index])
               circos.yaxis(c("left"), sector.index = "Tfas_chr1", labels = F, at = c(0,70,146),
                            labels.cex = 0.3, labels.col="khaki4", tick.length = 2)
             }, track.height = 0.15, bg.border = "black")
cgrootcrego commented 2 years ago

Hi, the sectors are chromosomes, there are 25 for Tfas and 24 for Tlei. I changed the code a bit to adjust for that and it worked! So thanks a lot.

I am also experiencing problems with circos.link. Initially, the line width was somehow not passing to the PDF format of the picture, and later on the code didn#t work at all anymore (without making any changes - the links just stopped appearing)

my code:

# Make plot
pdf(paste0(output_name, ".pdf"), width = 10, height = 8)
print("Initializing the plot...")

#-------------------TRACK 4: SYNTENY-------------------#

### Add links from Tfas to Tlei
synteny_genes <- read.table("orthogroups_Tfas_Tlei_Acom.per_gene.with_functional_info.no_TEs.one-to-one.circlize.txt.no-Tlei_chr2526", 
                            header = T,sep = "\t")
synteny_genes <- read.table(args[[5]], header = T,sep = "\t")
print("Drawing fourth track: Synteny...")
# Create color palette for links
nb.cols <- 25
mycolors <- sample(colorRampPalette(brewer.pal(8, "Set1"))(nb.cols))

### Add links from Tfas to Tlei
for (j in 1:25){
  loc=paste0("Tfas_chr", j)
  genes <- synteny_genes[synteny_genes$Tfas_chrom == loc,]
  for (i in 1:nrow(genes)){
    circos.link(sector.index1=genes[i,3], genes[i,4], sector.index2=genes[i,7], 
                genes[i,8],col=mycolors[j], lwd = .03)
  }
}

dev.off()
print("Done!")
jokergoo commented 2 years ago

I think the line width is way too small (lwd = .03)? 0.3 might be a proper value?