title: Plotting regressions from slope and intercept (lattice or ggplot2)
body: <p>I have a microarray dataset on which I performed a <code>limma</code> <code>lmFit()</code> test. If you haven't heard of it before, it's a powerful linear model package that tests differential gene expressions for >20k genes. You can extract the slope and intercept from the model for each one of these genes. </p>
  
  <p><strong>My problem is: given a table of slope and intercept values, how do I match a plot (I don't mind either <code>ggplot2</code>'s <code>geom_abline</code>, <code>lattice</code>'s <code>panel.abline</code>, or an alternative if necessary) with its corresponding slope and intercept?</strong></p>
  
  <p>My table (call it "slopeInt") has intercept as column 1 and slope as column 2, and has row names that correspond to the name of the gene. Their names look like this:</p>
  
  <pre><code>"202586_at"   "202769_at"   "203201_at"   "214970_s_at" "219155_at"
  </code></pre>
  
  <p>These names match my gene names in another table ("Data") containing some details about my samples (I have 24 samples with different IDs and Time/Treatment combination) and the gene expression values. </p>
  
  <p>It's in the long format with the gene names (as above) repeating every 24 rows (different expression levels for same gene, for each one of my samples):</p>
  
  <pre><code>ID    Time  Treatment  Gene_name    Gene_exp
  ...   ...   ...        ...          ...
  </code></pre>
  
  <p>I have overall eight genes I'm interested to plot, and the names in my <code>Data$Gene_name</code> match the row names of my <code>slopeInt</code> table. I can also merge the two tables together, that's not a problem. But I tried the following two approaches to give me graphs with graphs for every one of my genes with the appropriate regression, to no avail:</p>
  
  <p>Using <code>ggplot2</code>:</p>
  
  <pre><code>ggplot(Data, aes(x = Time, y = Gene_exp, group = Time, color = Treatment)) +
  facet_wrap(~ Gene_name, scales = "free_x") +
  geom_point() +
  geom_abline(intercept = Intercept, slope = Time), data = slopeInt) +
  theme(panel.grid.major.y = element_blank())`
  </code></pre>
  
  <p>And also using <code>Lattice</code>:</p>
  
  <pre><code>xyplot(Gene_exp ~ Time| Gene_name, Data, 
     jitter.data = T,
     panel = function(...){
       panel.xyplot(...)
       panel.abline(a = slopeInt[,1], b = slopeInt[,2])},
     layout = c(4, 2))
  </code></pre>
  
  <p>I've tried multiple other methods in the actual <code>geom_abline()</code> and <code>panel.abline()</code> arguments, including some for loops, but I am not experienced in R and I cannot get it to work.. I can also have the data file in a wide format (separate columns for each gene).</p>
  
  <p>Any help and further directions will be greatly appreciated!!!</p>
  
  <p>Here is some code for a reproducible example:</p>
  
  <pre><code>Data &lt;- data.frame(
   ID = rep(1:24, 8),
   Time = (rep(rep(c(1, 2, 4, 24), each = 3), 8)),
   Treatment = rep(rep(c("control", "smoking"), each = 12), 8),
   Gene_name = rep(c("202586_at", "202769_at", "203201_at", "214970_s_at",
     "219155_at", "220165_at", "224483_s_at", "227559_at"), each = 24),
   Gene_exp = rnorm(192))
  
  slopeInt &lt;- data.frame(
   Intercept = rnorm(8),
   Slope = rnorm(8))
  
  
  row.names(slopeInt) &lt;- c("202586_at", "202769_at", "203201_at",
  "214970_s_at", "219155_at", "220165_at", "224483_s_at", "227559_at")
  </code></pre>