Skip to content

Instantly share code, notes, and snippets.

@aaronwolen
Created July 3, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronwolen/e58493509f44243dabca4e6af8bfe67c to your computer and use it in GitHub Desktop.
Save aaronwolen/e58493509f44243dabca4e6af8bfe67c to your computer and use it in GitHub Desktop.
Simple plotting method for R IRanges and GRanges objects
# 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