title: R: Object not found with ggplot
body: <p>I am trying to add legend names to a ggplot but I always get an "object not found error". </p>
  
  <pre><code>  mycol = pft$colour
    myname = pft$name
    #mycol "#E5E503" "#9FFF8C" "#44CC29" "#137300" "#B2B224" "#0066CC" "#99CCFF" "#00407F" "#FF999B" "#E5171A" "#990003" "#A38FCC" "#7F40FF" "#A1E5CF" "#6B998A" "#F2F291" "#BF60A7" "#404040"
    #myname "C4 grass" "Early tropical" "Mid tropical" "Late tropical" "Temperate C3 Grass" "North Pine" "South Pine" "Late conifer" "Early hardwood" "Mid hardwood" "Late hardwood" "C3 crop" "C3 pasture" "C4 crop" "C4 pasture" "C3 grass" "Lianas" "Total"             
    test_data_long = melt(szpft[[vnam]][,ndbh+1,],
                          varnames = c("year","mpft"), na.rm = T)
    ggplot(data=test_data_long,aes(x=year,y=value, colour = mycol[mpft])) +
      geom_line(aes(group = mpft)) +
      scale_colour_identity(guide = "legend") +
      scale_fill_continuous(name="PFT", labels = myname[mpft])
  </code></pre>
  
  <p><code>test_data_long</code> is a <code>data.frame</code> that looks like this. </p>
  
  <pre><code>    year mpft     value
  20  2004    2  2.294562
  21  2005    2  2.415901
  22  2006    2  2.532214
  23  2007    2  2.649968
  24  2008    2  2.760934
  25  2009    2  2.849097
  26  2010    2  2.967846
  27  2011    2  3.102287
  28  2012    2  3.244338
  29  2013    2  3.386014
  30  2014    2  3.528662
  31  2015    2  3.675095
  32  2016    2  3.828054
  33  2017    2  3.976928
  34  2018    2  4.133859
  35  2019    2  4.305039
  36  2020    2  4.488999
  37  2021    2  4.673952
  38  2022    2  4.861845
  39  2004    3  4.518262
  40  2005    3  4.668800
  41  2006    3  4.821924
  42  2007    3  4.973597
  </code></pre>
  
  <p>I would like to use the <code>mpft</code> column as an index to define grouping, color, title ecc.</p>
  
  <p><code>mycol</code> and <code>myname</code> are vectors that contain colours (hex) and names corresponding to the different <code>mpft</code> lines to plot. The exact error I get is</p>
  
  <p><code>Error in check_breaks_labels(breaks, labels) : object 'mpft' not found</code></p>
  
  <p>Removing the last line of the script produce the next figure, so the problem lies in the last line. Why is <code>mpft</code> recognized before and not after?</p>
  
  <p><a href="https://i.stack.imgur.com/gKHdF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gKHdF.png" alt=""></a></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>To be clearer,</p>
  
  <pre><code>  mycol = pft$colour
    myname = pft$name
    test_data_long = melt(szpft[[vnam]][,ndbh+1,],
                          varnames = c("year","mpft"), na.rm = T)
    ggplot(data=test_data_long,aes(x=year,y=value, colour = mycol[mpft])) +
      geom_line(aes(group = mpft)) +
      scale_colour_identity(guide = "legend") 
  </code></pre>
  
  <p>produces the graph in figure and gives no error.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I'll provide a simplified, reproducible example of what I want to achieve here.</p>
  
  <pre><code>mycol = c("#A38FCC","#7F40FF")
  mynam = c("random_line1", "random_line2")
  set.seed(123)
  df=data.frame(month = month.abb, 
            mpft  = c(rep(1,6),rep(2,6)), 
            ran = runif(12,0.,10.))
  </code></pre>
  
  <p>That produces a dataframe with <code>month</code>, <code>mpft</code>, and <code>ran</code> value. I want the <code>ggplot</code> to have <code>month</code> on the x-axis and <code>ran</code> on the y-axis. Furthermore I want the points to be plotted with <code>mycol[1]</code> colour ("#A38FCC" color) and the legend to display <code>mynam[1]</code> as title (random_line1 as title) if <code>mpft = 1</code>.</p>