How to build overlapping ranges with ggplot2

I am trying to get around ggplot2 . In particular, I am trying to find out if there is a better (more elegant, simpler) way to create a graph found in the vignette of the Bioconductor IRanges (found here , figure on page 12, code on page 11).

enter image description here

In a vignette, a chart is created with the following code:

 plotRanges <- function(x, xlim = x, main = deparse(substitute(x)), + col = "black", sep = 0.5, ...) +{ + height <- 1 + if (is(xlim, "Ranges")) + xlim <- c(min(start(xlim)), max(end(xlim))) + bins <- disjointBins(IRanges(start(x), end(x) + 1)) + plot.new() + plot.window(xlim, c(0, max(bins)*(height + sep))) + ybottom <- bins * (sep + height) - height + rect(start(x)-0.5, ybottom, end(x)+0.5, ybottom + height, col = col, ...) + title(main) + axis(1) +} ir <- IRanges(c(1, 8, 14, 15, 19, 34, 40), + width = c(12, 6, 6, 15, 6, 2, 7)) plotRanges(ir) 

The fact that stacked bars are created by drawing rectangles, and I need to calculate the corner points, the height and width of each rectangle, which I think are not very elegant, ggplot2 have a more elegant way to do this? I know that β€œelegant” is not a very accurate description, but I hope you understand what I mean (unless I try to explain better).

+8
r plot ggplot2 bioconductor
source share
1 answer

Here you can create a similar story using ggplot2 . I am using the data from the IRanges example.

 library(IRanges) # example data ir <- IRanges(c(1, 8, 14, 15, 19, 34, 40), width = c(12, 6, 6, 15, 6, 2, 7)) # IRanges of length 7 # start end width # [1] 1 12 12 # [2] 8 13 6 # [3] 14 19 6 # [4] 15 29 15 # [5] 19 24 6 # [6] 34 35 2 # [7] 40 46 7 bins <- disjointBins(IRanges(start(ir), end(ir) + 1)) # [1] 1 2 1 2 3 1 1 dat <- cbind(as.data.frame(ir), bin = bins) library(ggplot2) ggplot(dat) + geom_rect(aes(xmin = start, xmax = end, ymin = bin, ymax = bin + 0.9)) + theme_bw() 

enter image description here

+11
source share

All Articles