Is there any way to alter the position or orientation of a legend in a trackViewer lollipopPlot (or fudge something that performs the same function), so that it's on the side of the plot and directly labeling each level of the stack? I'm plotting the positions of SNPs in different samples and with the amount of samples and SNPs involved it's getting a bit hard to keep track of what is what.
For illustration, a simplified example of what I've got is this:
(Code used to create this:)
# set up features
chromosome.feature.test <- GRanges("test", IRanges(c(1), width=c(500), names=c("test")))
test.snps <- GRanges("test", IRanges(c(10, 10, 25, 150, 10, 450), width=1, names=c("C10A", "C10A", "A25G", "T150C", "C10A", "T450G"),
stack.factor=c("test_snp_1", "test_snp_2", "test_snp_2", "test_snp_2", "test_snp_3", "test_snp_3")))
# set up visualization details
samples.palette.test <- list("#E69F00", "#56B4E9", "#009E73")
names(samples.palette.test) <- c("test_snp_1", "test_snp_2", "test_snp_3")
test.snps$color <- samples.palette.test[test.snps$stack.factor]
legend.test <- list(labels=c("test_snp_1", "test_snp_2", "test_snp_3"),
col="gray20", fill=sapply(samples.palette.test, `[`, 1))
# plot
lolliplot(test.snps, chromosome.feature.test, type="pie.stack", legend=legend.test, dashline.col="black")
and what I'd want is this:
I can't see anything about changing the legend position to that degree in the lollipopPlot vignette - am I overlooking something?
As far as I can see from the code, it seems the legend uses grid.legend, so I've tried to kludge something together using that - not outputting the legend in the lolliplot command itself but layering it on with a separate viewport:
# set up features
chromosome.feature.test <- GRanges("test", IRanges(c(1), width=c(500), names=c("test")))
test.snps <- GRanges("test", IRanges(c(10, 10, 25, 150, 10, 450), width=1, names=c("C10A", "C10A", "A25G", "T150C", "C10A", "T450G"),
stack.factor=c("test_snp_1", "test_snp_2", "test_snp_2", "test_snp_2", "test_snp_3", "test_snp_3")))
# set up visualization details
samples.palette.test <- list("#E69F00", "#56B4E9", "#009E73")
names(samples.palette.test) <- c("test_snp_1", "test_snp_2", "test_snp_3")
test.snps$color <- samples.palette.test[test.snps$stack.factor]
legend.test <- list(labels=c("test_snp_1", "test_snp_2", "test_snp_3"),
col="gray20", fill=sapply(samples.palette.test, `[`, 1))
# plot lolliplot
lolliplot(test.snps, chromosome.feature.test, type="pie.stack",dashline.col="black")
# set up legend fill params
gp <- legend.test[!names(legend.test) %in% c("labels")]
gp$fill <- gp$fill[order(names(gp$fill), decreasing=TRUE)]
class(gp) <- "gpar"
# plot legend
pushViewport(viewport(x=0.94, y=0.215))
grid.legend(labels=c("test_snp_1", "test_snp_2", "test_snp_3"), ncol=1, pch=21, vgap=0, gp=gp)
popViewport()
but getting it to line up with the levels feels rather fiddly to me:
Is there a better solution?


