Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:26:49+00:00 2026-06-18T09:26:49+00:00

I have a two big files with Start and End positions and two sample

  • 0

I have a two big files with Start and End positions and two sample columns (numeric).

File 1:

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

File 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

First, I would like to take the first Start and End positions from first file and make a segment plot. The plot must also take into account Start-20 and End+20 for each position in first file.

Then I would like to take the overlapping Start and End positions from the second file and plot it on the above plot. In this way there will be many plots based on the Start and End positions from the first file and the one with out overlaps will also be plotted individually.

The color for each segment will be based on the two sample numbers (for example in both the files if its 1 and 4 the color of segment will be red, if its 1 and 1 the color of segment will be green and so on).

I would really appreciate if someone make me understand how to make function for this in R.

Thanks in advance.

PS I have attached the drawing for anenter image description here output. I have shown only two results.

Below is the code which I wrote but it gives an error

Error in match.names(clabs, names(xi)) :
names do not match previous names

Also I need to specify red color to dataset1 line segment and green color to
dataset2 line segments. How will I implement it in the code below?

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:26:50+00:00Added an answer on June 18, 2026 at 9:26 am

    Something like this??

    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

    Edit: After looking at your updated drawing, maybe this is what you want?

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

    ggplot2_facet_geom_segment

    Edit: Save each plot in facet in separate file. You can do this by generating the plot each time by splitting on 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)
    })
    

    Set the path in fn accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files client.php and server.php. The client file send a HTTP request
I have two CSV files (three columns) which I need to compare and extract
I have created a virtual file system(very simular to fat) based on two files.
I have two big text Files each having more than 10 Million lines. How
I have two tab delimited files, file 1 contains identifiers and file 2 has
I have two big matrices in two files, A (21,000 x 80,000) and B(3,000
I have two hashes, one big and one small. All of the smaller hash's
I have two QT apps. One app can be considered to hold a big
I have a big problem. I have a activity where I have two imageViews.
I have SQL Server 2008 Ent and OLTP database with two big tables. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.