title: Plotting each column of a dataframe as one line using ggplot
body: <p>The whole dataset describes a module (or cluster if you prefer).</p>
  
  <p>In order to reproduce the example, the dataset is available at:
  <a href="https://www.dropbox.com/s/y1905suwnlib510/example_dataset.txt?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/y1905suwnlib510/example_dataset.txt?dl=0</a></p>
  
  <p>(54kb file)</p>
  
  <p>You can read as:</p>
  
  <pre><code>test_example &lt;- read.table(file='example_dataset.txt')
  </code></pre>
  
  <p>What I would like to have in my plot is <a href="http://oi60.tinypic.com/3038mlh.jpg" rel="nofollow noreferrer">this</a></p>
  
  <p>On the plot, the x-axis is my Timepoints column, and the y-axis are the columns on the dataset, except for the last 3 columns. Then I used facet_wrap() to group by the ConditionID column.</p>
  
  <p>This is exactly what I want, but the way I achieved this was with the following code:</p>
  
  <pre><code>plot &lt;- ggplot(dataset, aes(x=Timepoints))
  plot &lt;- plot + geom_line(aes(y=dataset[,1],colour = dataset$InModule))
  plot &lt;- plot + geom_line(aes(y=dataset[,2],colour = dataset$InModule))
  plot &lt;- plot + geom_line(aes(y=dataset[,3],colour = dataset$InModule))
  plot &lt;- plot + geom_line(aes(y=dataset[,4],colour = dataset$InModule))
  plot &lt;- plot + geom_line(aes(y=dataset[,5],colour = dataset$InModule))
  plot &lt;- plot + geom_line(aes(y=dataset[,6],colour = dataset$InModule))
  plot &lt;- plot + geom_line(aes(y=dataset[,7],colour = dataset$InModule))
  plot &lt;- plot + geom_line(aes(y=dataset[,8],colour = dataset$InModule))
  ...
  </code></pre>
  
  <p>As you can see it is not very automated. I thought about putting in a loop, like</p>
  
  <pre><code>columns &lt;- dim(dataset)[2] - 3
  for (i in seq(1:columns))
  {
    plot &lt;- plot + geom_line(aes(y=dataset[,i],colour = dataset$InModule))
  }
  (plot &lt;- plot + facet_wrap(  ~ ConditionID, ncol=6) )
  </code></pre>
  
  <p>That doesn't work.
  I found this topic
  <a href="https://stackoverflow.com/questions/14704742/use-for-loop-to-plot-multiple-lines-in-single-plot-with-ggplot2">Use for loop to plot multiple lines in single plot with ggplot2</a> which corresponds to my problem.
  I tried the solution given with the melt() function.</p>
  
  <p>The problem is that when I use melt on my dataset, I lose information of the Timepoints column to plot as my x-axis. This is how I did:</p>
  
  <pre><code>data_melted &lt;- dataset
  as.character(data_melted$Timepoints)
  dataset_melted &lt;- melt(data_melted)
  </code></pre>
  
  <p>I tried using aggregate</p>
  
  <pre><code>aggdata &lt;-aggregate(dataset, by=list(dataset$ConditionID), FUN=length)
  </code></pre>
  
  <p>Now with aggdata at least I have the information on how many Timepoints for each ConditionID I have, but I don't know how to proceed from here and combine this on ggplot.</p>
  
  <p>Can anyone suggest me an approach.
  I know I could use the ugly solution of creating new datasets on a loop with rbind(also given in that link), but I don't wanna do that, as it sounds really inefficient. I want to learn the right way.</p>
  
  <p>Thanks</p>