title: How to specify colors manually in ggplot2 within a for loop?
body: <p>I am building some code to plot energy supply vs. energy demand for bird populations with user-defined foraging guilds. I don't know in advance how many guilds a user will specify, and I need to create guild-specific plots. I would like to be able to specify the colors of the lines for energy supply and demand. Here's my data:</p>
  
  <pre><code>guilds &lt;- c("ducks","geese") #I have 2 foraging guilds
  dayz &lt;- c(1,2,3,4,5,6) #The plot will span 6 days
  guild1_supply &lt;- c(100,120,150,130,110,70) #energy available to guild 1
  guild2_supply &lt;- c(70,90,110,120,100,80) #energy available to guild 2
  supply_by_guild &lt;- cbind(guild1_supply,guild2_supply)
  guild1_demand &lt;- c(10,80,120,130,70,20) #energy demand for guild 1
  guild2_demand &lt;- c(5,45,75,60,30,0) #energy demand for guild 2
  demand_by_guild &lt;- cbind(guild1_demand,guild2_demand)
  setwd("C:/Users/XXX) #Set working directory for figure output
  </code></pre>
  
  <p>Now I create guild-specific plots in a for loop.</p>
  
  <pre><code>for (i in 1:length(guilds)) { # use a for loop to create data frames
  temp &lt;- data.frame(cbind(dayz,supply_by_guild[,i],demand_by_guild[,i]))
  names(temp)[2] &lt;- "Food Energy Supply" # rename variable
  names(temp)[3] &lt;- "Food Energy Demand" # rename variable
  temp &lt;- melt(temp, id.vars = c("dayz")) # melt data frame to long format
  names(temp)[2] &lt;- "Legend" # rename variable
  names(temp)[3] &lt;- "energy" # rename variable
  jpeg(paste("Fig",i+4,".jpg",sep=""),width=2000, height=1000, res=300)
  print(ggplot(data=temp, aes(x=dayz, y=energy,group=Legend, color=Legend)) + geom_line() + xlab("Day") + ylab(paste("Energy   (",energy_unit,")",sep="")) + ggtitle(paste("Figure ",i+4,". Daily Food Energy Supply and Demand: ",guilds[i],sep=""))) 
  dev.off()
  }
  </code></pre>
  
  <p>This above code works, but when I try to manually specify the colors of the lines by adding <code>+ scale_color_manual(values=c("cyan4", "orangered1"))</code>, I get the error "non-numeric argument to binary operator." Any help much appreciated.</p>