title: Plot rows of numbers that fall within a defined proximity to other rows in R
body: <p>I have a dataframe of numbers (genetics data) on different chromosomes that can be considered factors to separate the numbers on. It looks like this (with adjacent columns containing Sample info for each position): </p>
  
  <pre><code>awk '{print $2 "\t"   $3}' log_values | head 
  Chr   Start    Sample1     Sample2
  1   102447376  0.46957632  0.38415043
  1   102447536  0.49194950  0.30094824
  1   102447366  0.49874880 -0.17675325
  2   102447366 -0.01910729  0.20264680
  1   108332063 -0.03295081  0.07738970
  1   109472445  0.02216355 -0.02495788
  </code></pre>
  
  <p>What I want do is to make a series of plots taking values from other columns in this file. Instead of plotting one for each row (which would represent the results in a different region and/or different sample), I want to draw plots covering ranges if there are values in the Start column close enough to each other. To start, I would like a plot to be made if there are three values in the Start column within say 1000 of each other. That is, a 1000 from A to B to C inclusive so that A to B &lt;= 1000 and B to C is &lt;= 1000 but A to C does not have to be &lt;= 1000. In the code below, this 1000 is "CNV_size". The "flanking_size" variable is just to zoom the plot out a bit so I can give it some context. </p>
  
  <p>Taking the sample values, Rows 1 2 and 3 would be highlighted as one plot for Sample1. These  sample numbers are log2Ratios so I only want to plot the significant ones. I define this as above 0.4 or below -0.6. This means that the same three rows would not yield a plot for sample 2. </p>
  
  <p>The fourth row would not be included as the Chr column number/factor is different. That's a separate plot for each column showing the values only in the rows that meet this condition. So I can have more than plot per sample but each set of regions that meets this criterion will be plotted in all samples.  If this doesn't make sense, perhaps my ineffective attempt below will help explain what I'm waffling about. </p>
  
  <pre><code>pdf("All_CNVs_closeup.pdf")
  
  CNV_size &lt;- 1000 # bp 
  flanking_size &lt;- 1000 # bp
  
  #for(chr in 1:24){
  for(chr in 1:1){
   #for(array in 1:24) {
  for(array in 1:4) {
  
   dat &lt;- subset(file, file$Chr == chr ) 
   dat &lt;- subset(dat, dat[,array+6] &gt; 0.4 | dat[,array+6] &lt; -0.6) 
   if(length(dat$Start) &gt; 1 ) { 
   dat &lt;- dat[with(dat, order(Start)), ]
  
  x=dat$Start[2:length(dat$Start)]-dat$Start[1:(length(dat$Start)-1)]
  cnv &lt;- 1
  while(cnv &lt;= length(x)) { 
  for(i in cnv:length(x) ) {
      if(x[i] &gt;= CNV_size) {
          plot_title &lt;- paste(sample_info$Sample.ID[array], files[array],   sep = "   ") 
          plot(dat$Start, -dat[,array+6], main = plot_title , ylim = c(-2,2),      xlim = c(dat$Start[cnv] - flanking_size , dat$Start[i ] + flanking_size) , xlab = chr, ylab = "Log2 Ratio") 
          abline(h = 0.4, col="blue") 
          abline(h = 0, col="red") 
          abline(h = -0.6, col="blue") 
      break
      } # if(x[i] &gt;= CNV_size) {      
      #if(x[i] &lt; CNV_size) i &lt;- i + 1
  }   # for(i in cnv:length(x) ) {
  
  cnv &lt;- i 
  
  } # while(x[cnv] &lt;= length(x)) { 
    } # if(length(dat$Start) &gt; 1 ) { 
    } # for(array in 1:24) {
  } # for(chr in 1:24){
  
  
  
   dev.off()
  </code></pre>