【问题标题】:How to plot overlapping ranges with ggplot2如何使用 ggplot2 绘制重叠范围
【发布时间】:2014-02-25 17:18:20
【问题描述】:

我正在努力了解ggplot2。特别是,我试图找出是否有更好(更优雅,更简单)的方法来创建在BioconductorIRanges 包小插图中找到的情节(找到here,第 12 页上的图,页面上的代码11)。

在小插图中,情节是使用以下代码生成的:

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)

堆叠条是通过绘制矩形创建的,并且必须计算每个矩形的角点、高度和宽度这一事实让我觉得不是很优雅,ggplot2 有没有更优雅的方法呢?我知道“优雅”不是一个非常精确的描述,但我希望你能理解我的意思(如果不是我会尝试更好地解释)。

【问题讨论】:

  • Gvizggbio 可能已经提供了您正在寻找的内容; ggbio 基于 ggplot2。

标签: r plot ggplot2 bioconductor


【解决方案1】:

这是一种使用ggplot2 生成类似图的方法。我使用IRanges的示例数据。

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()

【讨论】:

  • 你知道如何在 Python 中构建相同的情节吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
相关资源
最近更新 更多