Created
July 3, 2019 14:46
-
-
Save aaronwolen/e58493509f44243dabca4e6af8bfe67c to your computer and use it in GitHub Desktop.
Simple plotting method for R IRanges and GRanges objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IRanges plot | |
# by Aaron Wolen (2016) | |
plot.GRanges <- function(x, ...) plot(GenomicRanges::ranges(x)) | |
plot.IRanges <- | |
function(x, | |
xlim = NULL, | |
ylim = NULL, | |
height = 0.8, | |
col = "grey", | |
border = NULL, | |
add = FALSE, | |
xlab = NULL, | |
ylab = NULL, | |
add.labels = FALSE, | |
col.labels = "black", | |
...) { | |
if (!requireNamespace("GenomicRanges", quietly = TRUE)) | |
stop("GenomicRanges package is required.", call. = FALSE) | |
if (is.null(xlab)) | |
xlab <- "" | |
if (is.null(ylab)) | |
ylab <- "" | |
xrng <- range(x) | |
group <- GenomicRanges::disjointBins(x) | |
if (is.null(xlim)) | |
xlim <- c(IRanges::start(xrng), IRanges::end(xrng)) | |
if (is.null(ylim)) | |
ylim <- c(1 - height / 2, max(group) + height / 2) | |
if (!add) { | |
plot.default( | |
NULL, | |
xlim = xlim, | |
ylim = ylim, | |
axes = FALSE, | |
xlab = xlab, | |
ylab = ylab | |
) | |
abline(h = unique(group), col = "grey90") | |
axis(1, at = pretty(xlim), tick = TRUE) | |
} | |
rect( | |
xleft = start(x), | |
ybottom = group - height / 2, | |
xright = end(x) , | |
ytop = group + height / 2, | |
col = col, | |
border = border | |
) | |
if (add.labels & !is.null(names(x))) { | |
text( | |
x = mid(x), | |
y = group, | |
labels = names(x), | |
col = col.labels | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment