title: for loop with ggplot only uses last value, even with vector defined outside the loop
body: <p>I have been working on a for loop that produces 2 different plots generated with ggplot. On each plot, two groups of the same length are defined, and within each group, each individual line has a colour.  </p>
  
  <p>Each plot has a different number of lines (see picture) that can be divided by two (both plots represent an equal number of males and females).
  I managed to obtain my two different plots, but the line colours are wrong. The colour values correspond to the last plot's, instead of adapting to each plot. I tried defining the colours both inside and outside the loop, but the result is the same. </p>
  
  <p>Here is a reproducible example:</p>
  
  <pre><code># Create the dataframe:
  Strain &lt;- rep(c(rep("A", times=2), rep("B", times=4)), times=2)
  Sex_ID &lt;- rep(c("M_1", "F_2", "M_3", "F_4", "M_5", "F_6"), times=2)
  State &lt;- rep(c("virgin", "mated", "expecting", "parent"), each=6)
  Huddling &lt;- seq(from=1.5, to=3.8, by=0.1)
  
  d&lt;-data.frame(Strain, Sex_ID, State, Huddling)
  
  level&lt;-levels(d$Strain)
  huddlist&lt;-list()
  
  # How many colours do we need? Different reds for each female, blues for males
  len &lt;- c(length(d$Sex_ID[d$Strain=="A"])/8,length(d$Sex_ID[d$Strain=="B"])/8)
  
  for(i in 1:length(level)){
    ss&lt;- subset(d, Strain==level[i]) # subset only for one species at a time
    m &lt;- scales::seq_gradient_pal("cyan2", "midnightblue", "Lab")(seq(0,1,length.out = len[i]))
    f&lt;-scales::seq_gradient_pal("tomato", "red4", "Lab")(seq(0,1,length.out = len[i]))
    fm&lt;-c(f,m)
    ymax &lt;- max(ss$Huddling); ymin &lt;- min(ss$Huddling)
  
    # The plot
    huddling&lt;-ggplot(ss, aes(x=factor(State), y=Huddling, color=factor(Sex_ID), group=factor(Sex_ID)))+
      geom_point(shape=21, size=3, position=position_dodge(width=0.3))+ 
      geom_line(size=0.7, position=position_dodge(width=0.3)) +
      scale_color_manual(values=fm)+
      scale_fill_manual(values="white")+
      ylim(ymin,ymax)+
      labs(y="Time huddling (s)", x="Reproductive stage")+
      theme_classic()+
      theme(axis.line.x = element_line(color="black", size = 1),
            axis.line.y = element_line(color="black", size = 1))+
      theme(axis.text=element_text(size=17),axis.title=element_text(size=19,face="bold"))+
      theme(legend.title=element_text(size=17))+
      theme(legend.text=element_text(size=15))+
      theme(legend.position="none")+ # if legend should be removed
      theme(plot.title = element_text(lineheight=.8, face="bold",size=22))+
      scale_x_discrete(limits=c("virgin", "mated", "expecting", "parent"), labels=c("Virgin", "Mated", "Expecting", "Parent"))
  
    huddlist[[i]] &lt;- huddling
  }
  
  library(gridExtra)
  do.call("grid.arrange", c(huddlist))
  </code></pre>
  
  <p>Alternatively, inside the loop:</p>
  
  <pre><code>  for(i in 1:length(level)){
    ss&lt;- subset(d, Strain==level[i]) # subset only for one species at a time
    len&lt;-length(levels(factor(ss$Sex_ID)))/2 # number of individuals of each sex in sample
    # this number allows to calculate the right number of reds and blues                       # to plot for females and males, respectively
    m &lt;- scales::seq_gradient_pal("cyan2", "midnightblue", "Lab")(seq(0,1,length.out = len[i]))
    f&lt;-scales::seq_gradient_pal("tomato", "red4", "Lab")(seq(0,1,length.out = len[i]))
    fm&lt;-c(f,m)
    ymax &lt;- max(ss$Huddling); ymin &lt;- min(ss$Huddling)
  
    # The plot
    huddling&lt;-ggplot(ss, aes(x=factor(State), y=Huddling, color=factor(Sex_ID), group=factor(Sex_ID)))+
      geom_point(shape=21, size=3, position=position_dodge(width=0.3))+ 
      geom_line(size=0.7, position=position_dodge(width=0.3)) +
      scale_color_manual(values=fm)+
      scale_fill_manual(values="white")+
      ylim(ymin,ymax)+
      labs(y="Time huddling (s)", x="Reproductive stage")+
      theme_classic()+
      theme(axis.line.x = element_line(color="black", size = 1),
            axis.line.y = element_line(color="black", size = 1))+
      theme(axis.text=element_text(size=17),axis.title=element_text(size=19,face="bold"))+
      theme(legend.title=element_text(size=17))+
      theme(legend.text=element_text(size=15))+
      theme(legend.position="none")+ # if legend should be removed
      theme(plot.title = element_text(lineheight=.8, face="bold",size=22))+
      scale_x_discrete(limits=c("virgin", "mated", "expecting", "parent"), labels=c("Virgin", "Mated", "Expecting", "Parent"))
  
    huddlist[[i]] &lt;- huddling
  }
  
  library(gridExtra)
  do.call("grid.arrange", c(huddlist))
  </code></pre>
  
  <p>This is what the plots look like. Normally, the first plot should have one red and one blue line, while the second should have two red and two blue lines.</p>
  
  <p><img src="https://i.stack.imgur.com/ImUpi.jpg" alt="This is what the plots look like. Normally, the first plot should have one red and one blue line, while the second should have two red and two blue lines."></p>