title: Add legend to density plot in R
body: <p>I'm using the following code in R to draw two density curves on a single graph;</p>
  
  <pre><code>mydata1&lt;-read.csv(file="myfile1.csv",head=TRUE,sep=",")
  mydata2&lt;-read.csv(file="myfile2.csv",head=TRUE,sep=",")
  
  pdf("comparison.pdf")
  
  plot.multi.dens &lt;- function(s)   
  {
      junk.x = NULL
      junk.y = NULL
      for(i in 1:length(s)) {
          junk.x = c(junk.x, density(s[[i]])$x)
          junk.y = c(junk.y, density(s[[i]])$y)
      }
      xr &lt;- range(junk.x)
      yr &lt;- range(junk.y)
      plot(density(s[[1]]), xlim = xr, ylim = yr, xlab="Usage",main = "comparison")
      for(i in 1:length(s)) {
          lines(density(s[[i]]), xlim = xr, ylim = yr, col = i)
      }
  }
  
  plot.multi.dens( list(mydata2$usage,mydata1$usage))    
  dev.off()
  </code></pre>
  
  <p>Now the problem is that the graph which is being produced shows two lines but the graph doesn't include the information that which line is which. For example, in the output, it should show that the red line is "a" and the black line is "b". I'm a newbie to R which is why i'm having some difficulty. any help will be appreciated!</p>