You can use the standard legend()
command as outlined in this issue here: https://support.bioconductor.org/p/124328/
Minimal example based on https://bernatgel.github.io/karyoploter_tutorial//Examples/Rainfall/Rainfall.html :
library(karyoploteR)
somatic.mutations <- read.table(file="ftp://ftp.sanger.ac.uk/pub/cancer/AlexandrovEtAl/somatic_mutation_data/Pancreas/Pancreas_raw_mutations_data.txt", header=FALSE, sep="\t", stringsAsFactors=FALSE)
somatic.mutations <- setNames(somatic.mutations, c("sample", "mut.type", "chr", "start", "end", "ref", "alt", "origin"))
somatic.mutations <- split(somatic.mutations, somatic.mutations$sample)
sm <- somatic.mutations[["APGI_1992"]]
sm.gr <- toGRanges(sm[,c("chr", "start", "end", "mut.type", "ref", "alt")])
seqlevelsStyle(sm.gr) <- "UCSC"
variant.colors <- getVariantsColors(sm.gr$ref, sm.gr$alt)
kp <- plotKaryotype(plot.type=4)
kpPlotRainfall(kp, data = sm.gr, col=variant.colors)
uniq_cols <- variant.colors[!duplicated(variant.colors)]
legend(x = "bottomright", fill=uniq_cols, legend=names(uniq_cols))
Now you can customise the call to legend() in any way you like.
Looks like this:
Thank you very much for your response Phillip. Also, thank you for providing the reference website. It helps a lot. Now I can see the figure legend on my plot.
Thank you again!
Good luck :)
By the way, it is weird to me but I see 7 colors in my plot. Green (T>G), black (C>G), purple (T>A), red (C>T), yellow (T>C), blue (C>A) and gray (blank, no information)
Has anyone been having a same problem with color legend? How come it happens since I only extracted columns of "Chr", "Start", "End", "Mut_type", "REF", "ALT" from loaded Txt file.
Uh,,
I removed all the INDEL from my samples and there are no more gray dot shown in the rainfall plot, which I was expected. However, still gray box exist in the figure legend.
Anyway, the source file downloaded from the website: https://bernatgel.github.io/karyoploter_tutorial//Examples/Rainfall/Rainfall.html has no problem at all for the plot, indicating this is something wrong with my sample file.
I will figure it out. Thank you.
Good work! I reckon
uniq_cols
still has the color-less indel in it.This is what mine looks like:
You might have another entry in there with ' ' or something like that. To remove:
or whatever the position of the empty gray is in your named vector
uniq_cols
.