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 6909743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:43:40+00:00 2026-05-27T08:43:40+00:00

I have a dataframe in R like this: dat = data.frame(Sample = c(1,1,2,2,3), Start

  • 0

I have a dataframe in R like this:

dat = data.frame(Sample = c(1,1,2,2,3), Start = c(100,300,150,200,160), Stop = c(180,320,190,220,170))

And I would like to plot it such that the x-axis is the position and the y-axis is the number of samples at that position, with each sample in a different colour. So in the above example you would have some positions with height 1, some with height 2 and one area with height 3. The aim being to find regions where there are a large number of samples and what samples are in that region.

i.e. something like:

      &
     ---
********-  --       **

where * = Sample 1, – = Sample 2 and & = Sample 3

  • 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-05-27T08:43:40+00:00Added an answer on May 27, 2026 at 8:43 am

    This hack may be what you’re looking for, however I’ve greatly increased the size of the dataframe in order to take advantage of stacking by geom_histogram.

    library(ggplot2)
    dat = data.frame(Sample = c(1,1,2,2,3), 
                     Start = c(100,300,150,200,160), 
                     Stop = c(180,320,190,220,170))
    
    # Reformat the data for plotting with geom_histogram.
    dat2 = matrix(ncol=2, nrow=0, dimnames=list(NULL, c("Sample", "Position")))
    
    for (i in seq(nrow(dat))) {
        Position = seq(dat[i, "Start"], dat[i, "Stop"])
        Sample = rep(dat[i, "Sample"], length(Position))
        dat2 = rbind(dat2, cbind(Sample, Position))
    }
    
    dat2 = as.data.frame(dat2)
    dat2$Sample = factor(dat2$Sample)
    
    plot_1 = ggplot(dat2, aes(x=Position, fill=Sample)) +
             theme_bw() +
             opts(panel.grid.minor=theme_blank(), panel.grid.major=theme_blank()) +
             geom_hline(yintercept=seq(0, 20), colour="grey80", size=0.15) +
             geom_hline(yintercept=3, linetype=2) +
             geom_histogram(binwidth=1) +
             ylim(c(0, 20)) +
             ylab("Count") +
             opts(axis.title.x=theme_text(size=11, vjust=0.5)) +
             opts(axis.title.y=theme_text(size=11, angle=90)) +
             opts(title="Segment Plot")
    
    png("plot_1.png", height=200, width=650)
    print(plot_1)
    dev.off()
    

    Note that the way I’ve reformatted the dataframe is a bit ugly, and will not scale well (e.g. if you have millions of segments and/or large start and stop positions).

    enter image description here

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

Sidebar

Related Questions

Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
i have a data frame like this A B value 1 1 0.123 2
I have the following question: I have data frame which looks like this. I
Let's say I have a data frame, like this: df <- data.frame( variable =
Let's have a dataframe with long strings in one column: df<-data.frame(short=rnorm(10,0,1),long=replicate(10,paste(rep(sample(letters),runif(1,5,8)),collapse=))) How could I
Lets have the following dataframe inside R: df <- data.frame(sample=rnorm(1,0,1),params=I(list(list(mean=0,sd=1,dist=Normal)))) df <- rbind(df,data.frame(sample=rgamma(1,5,5),params=I(list(list(shape=5,rate=5,dist=Gamma))))) df

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.