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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:37:05+00:00 2026-05-26T20:37:05+00:00

I have 2 dataframes, Tg and Pf, each of 127 columns. All columns have

  • 0

I have 2 dataframes, Tg and Pf, each of 127 columns. All columns have at least one row and can have up to thousands of them. All the values are between 0 and 1 and there are some missing values (empty cells). Here is a little subset:

Tg
Tg1 Tg2 Tg3 ... Tg127
0.9 0.5 0.4     0
0.9 0.3 0.6     0
0.4 0.6 0.6     0.3
0.1 0.7 0.6     0.4
0.1 0.8
0.3 0.9
    0.9
    0.6
    0.1

Pf
Pf1 Pf2 Pf3 ...Pf127
0.9 0.5 0.4    1
0.9 0.3 0.6    0.8 
0.6 0.6 0.6    0.7
0.4 0.7 0.6    0.5
0.1     0.6    0.5
0.3
0.3
0.3

Note that some cell are empty and the vector lengths for the same subset (i.e. 1 to 127) can be of very different length and are rarely the same exact length.
I want to generate 127 graph as follow for the 127 vectors (i.e. graph is for col 1 from each dataframe, graph 2 is for col 2 for each dataframe etc…):

enter image description here

Hope that makes sense. I’m looking forward to your assistance as I don’t want to make those graphs one by one…
Thanks!

  • 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-26T20:37:05+00:00Added an answer on May 26, 2026 at 8:37 pm

    Here is an example to get you started (data at https://gist.github.com/1349300). For further tweaking, check out the excellent ggplot2 documentation that is all over the web.

    library(ggplot2)
    
    # Load data
    Tg = read.table('Tg.txt', header=T, fill=T, sep=' ')
    Pf = read.table('Pf.txt', header=T, fill=T, sep=' ')
    
    # Format data
    Tg$x        = as.numeric(rownames(Tg))
    Tg          = melt(Tg, id.vars='x')
    Tg$source   = 'Tg'
    Tg$variable = factor(as.numeric(gsub('Tg(.+)', '\\1', Tg$variable)))
    
    Pf$x        = as.numeric(rownames(Pf))
    Pf          = melt(Pf, id.vars='x')
    Pf$source   = 'Pf'
    Pf$variable = factor(as.numeric(gsub('Pf(.+)', '\\1', Pf$variable)))
    
    # Stack data
    data = rbind(Tg, Pf)
    
    # Plot
    dev.new(width=5, height=4)
    p = ggplot(data=data, aes(x=x)) + geom_line(aes(y=value, group=source, color=source)) + facet_wrap(~variable)
    p
    

    enter image description here


    Highlighting the area between the lines

    First, interpolate the data onto a finer grid. This way the ribbon will follow the actual envelope of the lines, rather than just where the original data points were located.

    data = ddply(data, c('variable', 'source'), function(x) data.frame(approx(x$x, x$value, xout=seq(min(x$x), max(x$x), length.out=100))))
    names(data)[4] = 'value'
    

    Next, calculate the data needed for geom_ribbon – namely ymax and ymin.

    ribbon.data = ddply(data, c('variable', 'x'), summarize, ymin=min(value), ymax=max(value))
    

    Now it is time to plot. Notice how we’ve added a new ribbon layer, for which we’ve substituted our new ribbon.data frame.

    dev.new(width=5, height=4)
    p + geom_ribbon(aes(ymin=ymin, ymax=ymax),  alpha=0.3, data=ribbon.data)
    

    enter image description here


    Dynamic coloring between the lines

    The trickiest variation is if you want the coloring to vary based on the data. For that, you currently must create a new grouping variable to identify the different segments. Here, for example, we might use a function that indicates when the “Tg” group is on top:

    GetSegs <- function(x) {
      segs = x[x$source=='Tg', ]$value > x[x$source=='Pf', ]$value
      segs.rle = rle(segs)
    
      on.top = ifelse(segs, 'Tg', 'Pf')
      on.top[is.na(on.top)] = 'Tg'
    
      group = rep.int(1:length(segs.rle$lengths), times=segs.rle$lengths)
      group[is.na(segs)] = NA
    
      data.frame(x=unique(x$x), group, on.top)
    }
    

    Now we apply it and merge the results back with our original ribbon data.

    groups = ddply(data, 'variable', GetSegs)
    ribbon.data = join(ribbon.data, groups)
    

    For the plot, the key is that we now specify a grouping aesthetic to the ribbon geom.

    dev.new(width=5, height=4)
    p + geom_ribbon(aes(ymin=ymin, ymax=ymax, group=group, fill=on.top),  alpha=0.3, data=ribbon.data)
    

    enter image description here

    Code is available together at: https://gist.github.com/1349300

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

Sidebar

Related Questions

I have two dataframes and I wish to insert the values of one dataframe
I have two data frames with two columns each. The first column is timestamps
I have a dataframe of 9 columns consisting of an inventory of factors. Each
I have a big dataframe with columns such as: ID, time, OS, IP Each
I have a dataframe made up of 6 columns. Columns 1 to 5 each
I'm opening a text file which can hold anywhere between 100 and 50,000 dataFrames,
I have three dataframes with this structure (but different values): V1 V2 2010-04-30 30
I have 2 data frames with different number of columns each. Some of the
I have two data.frames each with three columns: chrom, start & stop, let's call
I have 2 data frames w/ 5 columns and 100 rows each. id price1

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.