r - ggplot2:使用 geom_segment 和 geom_text_repel 创建由线条和填充框组成的线性图(转录因子结合位点)

标签 r ggplot2

我正在尝试使用ggplot2geom_segmentgeom_text_repel在某些基因的启动子DNA序列上创建转录因子结合位点图谱>.

这是我的数据片段:

mincle.tf <- structure(list(
  Matrix.ID = c(
    "MA0007.3", "PB0118.1", "MA0608.1",
    "MA0474.1", "MA0608.1", "MA0098.2", "MA0007.3", "PB0012.1"
  ),
  Name = structure(c(1L, 5L, 2L, 4L, 2L, 6L, 1L, 3L), .Label = c(
    "Ar",
    "Creb3l2", "Elf3_1", "Erg", "Esrra_2", "Ets1"
  ), class = "factor"),
  Score = c(
    13.8552, 13.7763, 13.6251, 13.0501, 12.9081, 12.7074,
    12.6526, 12.4201
  ), Relative.score = c(
    0.8831708, 0.9177318,
    0.9736072, 0.9454676, 0.9613641, 0.8977955, 0.8696497, 0.9363881
  ), Sequence.ID = c(
    "seq1", "seq1", "seq1", "seq1", "seq1",
    "seq1", "seq1", "seq1"
  ), Start = c(
    603L, 505L, 28L, 873L,
    29L, 872L, 603L, 873L
  ), End = c(
    619L, 521L, 36L, 883L, 37L,
    886L, 619L, 885L
  ), Strand = c(
    "-", "+", "+", "-", "-", "+",
    "+", "-"
  ), Predicted.sequence = c(
    "gggaacataatgtgcat", "gtaaaggggtcagattc",
    "tccacgtgt", "agaggaaatga", "gacacgtgg", "atcatttcctctttt",
    "atgcacattatgttccc", "aaagaggaaatga"
  )
), row.names = c(
  NA,
  -8L
), class = "data.frame")

这是我的代码:

ggplot(mincle.tf, aes(Start, 3, label=Name)) + 
  geom_segment(aes(x=Start, y=3, xend=End, yend=3), size=10, data=mincle.tf[1:8], color=cbp2, alpha=0.5) + 
  geom_segment(aes(x = 0, y = 3, xend = 1200, yend = 3), size=1, color="black") + 
  geom_text_repel(data=mincle.tf[1:8],nudge_y = 0.1,point.padding = 0, box.padding = 0, direction = "x", angle=90, hjust=0.5, vjust=0.5)

这是我得到的情节:

enter image description here

基本上,我使用geom_segment为TF结合位点绘制一条DNA线(黑色)和彩色框,并使用geom_text_repel根据数据中的名称列。

我的问题是标签线从每个框的开头开始,而不是框的中心,并且我无法通过 point.paddingbox.padding。另外,有些框重叠,我无法找出显示重叠的最佳方式。我尝试过透明度,但没有成功(见下图)。

enter image description here

在这一点上,我真的很感激任何改进这个情节的建议和指示。

最佳答案

首先要解决最后一个问题,显示重叠特征的一种方法是将不相交的范围特征放入不同的容器中。您可以使用 Bioconductor 的 IRanges 包来计算此值。

mincle.tf$bin <- with(mincle.tf, IRanges::disjointBins(IRanges::IRanges(Start, End))) - 1

您可以使用计算出的 bin 作为 y 位置的偏移量。请注意下面的 y = 3 + bin * 0.06,其中 0.06 只是适合绘图的缩放因子。

要将标签放在 TFBS 中间,您可以将 x 位置设置为 (Start + End)/2

您似乎无法缩短标签线的原因是因为本质上您的整个绘图在 y 轴上的宽度(在数据空间中)接近 0。扩展 y 轴可以缩短标签线。

ggplot(mincle.tf, aes(Start, 3, label=Name)) + 
  geom_segment(aes(x=Start, y=3 + bin * 0.06, 
                   xend=End, yend=3 + bin * 0.06, colour = Matrix.ID), 
               size=10, data=mincle.tf[1:8,], alpha=0.5) + 
  geom_segment(aes(x = 0, y = 3, xend = 1200, yend = 3), size=1, color="black") + 
  geom_text_repel(data=mincle.tf[1:8,],
                  aes(x = (Start + End) / 2,
                      y = 3 + bin * 0.06),
                  nudge_y = 0.1,
                  point.padding = 0, box.padding = 0.5, direction = "x", 
                  angle=90, hjust=0.5, vjust=0.5) +
  scale_y_continuous(limits = c(2.5, 3.5))

enter image description here

请注意,对于上面的示例,我没有 cbp2 变量,因此我替换了 PWM/PFM ID(至少这是我的猜测)。

关于r - ggplot2:使用 geom_segment 和 geom_text_repel 创建由线条和填充框组成的线性图(转录因子结合位点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62505794/

相关文章:

在 OSX 10.9.5 (R 3.1.2) 中使用 ggplot2 按因子对 geom_point 着色时,R session 崩溃

python - Python 中的 ElasticNetCV 与 R 中的 cvglmnet

r - 在(R,ggplot2)中围绕条形图和类别绘制一个矩形

r - 检查R中是否存在对象

r - 使用 ggplot2 绘制 map - 创建填充框的掩码,不包括单个国家/地区

r - ggplot 科学轴标签 - 按 1e6 缩放值并格式 "E-06"

r - 为什么这个 ggplot 上的颜色是错误的?

r - ggplot for 循环输出所有相同的图形

r - 如何根据 R 中定义的列中缺失值的数量返回行值的总和?

r - 安装旧包 R