我正在尝试使用ggplot2
与geom_segment
和geom_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)
这是我得到的情节:
基本上,我使用geom_segment
为TF结合位点绘制一条DNA线(黑色)和彩色框,并使用geom_text_repel
根据数据中的名称
列。
我的问题是标签线从每个框的开头开始,而不是框的中心,并且我无法通过 point.padding
和box.padding
。另外,有些框重叠,我无法找出显示重叠的最佳方式。我尝试过透明度,但没有成功(见下图)。
在这一点上,我真的很感激任何改进这个情节的建议和指示。
最佳答案
首先要解决最后一个问题,显示重叠特征的一种方法是将不相交的范围特征放入不同的容器中。您可以使用 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))
请注意,对于上面的示例,我没有 cbp2
变量,因此我替换了 PWM/PFM ID(至少这是我的猜测)。
关于r - ggplot2:使用 geom_segment 和 geom_text_repel 创建由线条和填充框组成的线性图(转录因子结合位点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62505794/