How to add a color legend to plotKaryotype and kpPlotRegions ?
1
1
Entering edit mode
Last seen 7 months ago
Germany

karyoploteR::plotKaryotype is great and works fine - thank you, Bernat :-) :

kp <- karyoploteR::plotKaryotype('mm10')
granges1 <- GenomicRanges::GRanges(c('chr1', 'chr2'), c('100-200', '100-200'))
granges2 <- GenomicRanges::GRanges(c('chr3', 'chr4'), c('100-200', '100-200'))
karyoploteR::kpPlotRegions(kp, granges1, col = "red")
karyoploteR::kpPlotRegions(kp, granges2, col = "green")


But how do I add a color legend (explaining what green and red set are)?

karyoploter • 849 views
2
Entering edit mode
bernatgel ▴ 130
@bernatgel-7226
Last seen 9 days ago
Spain

You can use legend as you would use with any other plot.

kp <- karyoploteR::plotKaryotype('mm10')
granges1 <- GenomicRanges::GRanges(c('chr1', 'chr2'), c('100-200', '100-200'))
granges2 <- GenomicRanges::GRanges(c('chr3', 'chr4'), c('100-200', '100-200'))
karyoploteR::kpPlotRegions(kp, granges1, col = "red")
karyoploteR::kpPlotRegions(kp, granges2, col = "green")

legend(x = "bottomright", fill = c("red", "green"), legend = c("Ranges1", "Ranges2"))


That will give you something like this

I usually prefer them as a single line below the plot as in this image, although that would require a bit more of fiddling with the positions.

0
Entering edit mode

Thank you Bernat, for brushing up my rusty base graphics knowledge, and making this great package!

May I ask one more question? Is it possible to store the Karyoplot object in a variable and print it to a plot later? This works for ggplot2 and grid. I thought it doesn't for base graphics, but with Karyoplot you seem to have found a way around.

0
Entering edit mode

Hehe! glad you find it useful!

karyoploteR is pure base graphics and so it's not possible to store it in a variable to plot later. Actually, karyoploteR plays really bad with graphics composition packages such as grid or the base par(mfrow=(A,B)). This is something I'd like to improve in the future, but for now the combination of karyoplots with other plots must be done out of R.

0
Entering edit mode

Hi again,

I just found out you can use karyoploteR with grid, cowplot and patchwork using the package ggplotify.

You can find the code in this github issue.

0
Entering edit mode

Oh, that's pretty cool indeed! Thanks for sharing.