The problem is I want to add multiple legend to different layer of layoutkaryogram function in ggbio plot.
library(GenomicRanges)
library(ggbio)
df<-data.frame(chromosome=c(9,9,9,9,9,9),start=c(12229,12244,12245,12248,19576961,19792417)
,end=c(12229,12244,12245,12248,19576961,19792417),sample1=c("KDNL","CT","IR","IR","DH","DH"),
marker=c('snp',"snp",'snp',"snp",'ssr',"ssr"))
myideo<-data.frame(chromosome=9,start=1,end=21757032)
Then I change to GRanges object
> dfGR
GRanges object with 6 ranges and 2 metadata columns:
seqnames ranges strand | sample marker
<Rle> <IRanges> <Rle> | <factor> <factor>
[1] 9 [ 12229, 12229] * | KDNL snp
[2] 9 [ 12244, 12244] * | CT snp
[3] 9 [ 12245, 12245] * | DH snp
[4] 9 [ 12248, 12248] * | DH snp
[5] 9 [19576961, 19576961] * | KDNL ssr
[6] 9 [19792417, 19792417] * | KDNL ssr
-------
seqinfo: 1 sequence from an unspecified genome; no seqlengths
> myideoGR
GRanges object with 1 range and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] 9 [1, 21757032] *
-------
seqinfo: 1 sequence from an unspecified genome; no seqlengths
And try to plot and it give me a nice graph
q1 <- ggplot() + layout_karyogram(myideoGR, cytoband = FALSE)
q2=q1 + layout_karyogram(dfGR[dfGR$marker == "snp",], geom ="rect", ylim =c(0, 10), aes(fill=sample,colour=sample)) +
layout_karyogram(dfGR[dfGR$marker == "ssr",], ylim =c(11, 19), aes(fill=sample,colour=sample)) +
scale_color_manual(values=c("#22B700","#00C0BA", "#F564E3","#FF6C91" ),name="SNP",breaks=c("CT","IR","KDNL")) +
scale_fill_manual(values=c("#22B700","#00C0BA", "#F564E3","#FF6C91"),name="SSR",breaks=c("DH","KDNL")) +
guides(fill = guide_legend(order=2), color = guide_legend(order=1))
However, I have another information I want to add to this plot
gene_overlapGR
GRanges object with 7 ranges and 2 metadata columns:
seqnames ranges strand | sample TRAIT
<Rle> <IRanges> <Rle> | <character> <character>
[1] 9 [10128837, 10131086] * | Sample_5 MXA
[2] 9 [10128837, 10131086] * | Sample_5 MXA
[3] 9 [10128837, 10131086] * | Sample_5 MXA
[4] 9 [10128837, 10131086] * | Sample_5 MXA
[5] 9 [10338558, 10340355] * | Sample_5 MXA
[6] 9 [10338558, 10340355] * | Sample_5 MXA
[7] 9 [10328808, 10333199] * | Sample_5 MXA
I try to add a new plot by the code below but the legend is
q2+ layout_karyogram(gene_overlapGR, geom ="rect",ylim=c(-10,-1),aes(fiil=trait))
Any suggestion to add a third legend with mxa lebeling?