r - 在 R 中绘制来自不同文件的重叠位置

标签 r plot ggplot2 lattice

我有两个大文件,其中包含 StartEnd 位置以及两个示例列(数字)。

文件 1:

Start  End  Sample1  Sample2
1         60      1       4
100       200     2       1
201       250     1       4
300       450     1       1

文件 2:

Start  End  Sample1  Sample2
40         60      1       1
70        180      1       1
240       330      2       1
340       450      1       4
500       900      1       4
980       1200     2       1

首先,我想从第一个文件中获取第一个 StartEnd 位置并制作一个分段图。该图还必须考虑第一个文件中每个位置的 Start-20End+20

然后我想从第二个文件中获取overlapping StartEnd 位置并将其绘制在上面的图上。通过这种方式,将有许多基于第一个文件的 StartEnd 位置的图,没有重叠的图也将单独绘制。

每个段的颜色将基于两个样本编号(例如,在这两个文件中,如果其1和4段的颜色将是red,如果它的 1 和 1 段的颜色将是 green 等等)。

如果有人让我了解如何在 R 中为此创建函数,我将不胜感激。

提前致谢。

PS 我附上了 enter image description here 的图纸输出。我只显示了两个结果。

下面是我写的代码,但是报错了

ma​​tch.names(clabs, names(xi)) 错误: 名称与以前的名称不匹配

我还需要为 dataset1 线段指定红色,为 dataset1 线段指定绿色 dataset2 线段。我将如何在下面的代码中实现它?

overlap_func <- function(dataset1,dataset2) {

for(i in 1:nrow(dataset1))
 {

 loop_start <- dataset1[i,"Start"]
 loop_end <- dataset1[i,"End"]
 p <- dataset2[,c(1,2)]   
 dataset1_pos <- data.frame(loop_start,loop_end)
 dataset2_filter <- p[p$Start >= (loop_start-(loop_start/2)) & p$End <= (loop_end+ (loop_end/2)), ]
 data_in_loop <- rbind(dataset1_pos,dataset2_filter)
 plot_function(data_in_loop,loop_start,loop_end)

 }
 }


plot_function <- function(loop_data,start,end){ 
 pos <- 1:nrow(loop_data)
 dat1 <- cbind(pos,loop_data)
 colnames(dat1) <- c("pos","start","end")
 pdf(file=paste0("path where plots are generated","_",start,"-",end,"_","overlap.pdf"))
 plot(dat1$pos, type = 'n', xlim = range(c(start-(start/2), end+(end/2))))
 segments(dat1$start, dat1$pos, dat1$end, dat1$pos)
 dev.off()
 }


df1 <- read.table(header=T, text="Start  End  Sample1  Sample2
1         60      1       4
100       200     2       1
201       250     1       4
300       450     1       1")

df2 <- read.table(header=T, text="Start  End  Sample1  Sample2
40         60      1       1
70        180      1       1
240       330      2       1
340       450      1       4
500       900      1       4
980       1200     2       1")

 overlap_func(df1,df2)

最佳答案

像这样的东西??

df1 <- read.table(header=T, text="Start  End  Sample1  Sample2
1         60      1       4
100       200     2       1
201       250     1       4
300       450     1       1")

df2 <- read.table(header=T, text="Start  End  Sample1  Sample2
40         60      1       1
70        180      1       1
240       330      2       1
340       450      1       4
500       900      1       4
980       1200     2       1")

require(IRanges)
require(ggplot2)
require(plyr)

df1$id <- factor(1:nrow(df1))
ir2 <- IRanges(df2$Start, df2$End)
out <- ddply(df1, .(id), function(x) {
    ir1 <- IRanges(x$Start, x$End)
    o.idx <- as.data.frame(findOverlaps(ir1, ir2))$subjectHits
    df.out <- rbind(x[, 1:4], df2[o.idx, ])
    df.out$id1 <- x$id
    df.out$id2 <- seq_len(nrow(df.out))
    df.out
})
out$id1 <- factor(out$id1)
out$id2 <- factor(out$id2)
out$id3 <- factor(1:nrow(out))

p <- ggplot(out, aes(x = Start, y = id3 , colour = id2)) 
p <- p + geom_segment(aes(xend = End, ystart = id3, yend = id3))
p <- p + scale_colour_brewer(palette = "Set1")
p

gglot2_no_facet_geom_segment

编辑:看完更新后的绘图,也许这就是您想要的?

p + facet_wrap( ~ id1, scales="free")

ggplot2_facet_geom_segment

编辑:将facet中的每个图保存在单独的文件中。您可以通过在 id1

上拆分每次生成绘图来实现此目的
d_ply(out, .(id1), function(ww) {
    p <- ggplot(ww, aes(x = Start, y = id3 , colour = id2)) 
    p <- p + geom_segment(aes(xend = End, ystart = id3, yend = id3))
    p <- p + scale_colour_brewer(palette = "Set1")
    fn <- paste0("~/Downloads/id", as.numeric(as.character(ww$id1[1])), ".pdf")
    ggsave(fn, p)
})

相应地在fn中设置路径。

关于r - 在 R 中绘制来自不同文件的重叠位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14646809/

相关文章:

r - R 中是否有一个函数可以完成 C 中 atoi() 的功能?

r - 在 R 中将列表列的部分设置为 NULL

python - 如何使我的 Bokeh 图居中

r - 使用ggplot2仅将一个分割添加到一个构面

r - R 中 For 循环的警告消息?

r - 在r/formattable中,如何调整列格式不包括 `total`行

python - 在python 3.6.3中安装ggp​​lot

r - 在 R 中的图中添加第二个图例

r - 结合 3D/2D 绘图

wolfram-mathematica - Mathematica,FrameTicks 在不同类型的图中不一致