title: Legend not displaying in plot function: R
body: <p>I have tried to plot a simple graph using the following code;</p>
  
  <pre><code>plot(y = Et, x = t, type = "l", col = 1,
   xlab = "Time",     ylab= "Equity ($)",
   main = "Figure 1–3: Randomly Generated Equity Curves") 
  grid()
  abline(h = 10000)
  lines(y = Et2, x = t, col = 2)
  lines(y = Eb, x = t, col = 8)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tDlw0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tDlw0.jpg" alt="enter image description here"></a></p>
  
  <p>Then I try to add a legend to the plot </p>
  
  <pre><code>legend(x = "topleft", col = c(1,2,8), lwd = 2, legend = c("Curve 1",
                                                        "Curve 2",
                                                        "SPY"))
  </code></pre>
  
  <p>And the result I obtain is the following;</p>
  
  <p><a href="https://i.stack.imgur.com/dNPMB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dNPMB.jpg" alt="enter image description here"></a></p>
  
  <p>Where am I going wrong?</p>
  
  <p>EDIT 1: I restarted R studio and re-ran the plots and got the following result.</p>
  
  <p><a href="https://i.stack.imgur.com/zINP0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zINP0.jpg" alt="enter image description here"></a></p>
  
  <p>EDIT 2: Reproducible code:</p>
  
  <pre><code>library(quantmod)
  options("getSymbols.warning4.0" = FALSE,
          "getSymbols.auto.assign" = FALSE)
  
  SPY &lt;- getSymbols(c("SPY"), from = "2016-09-01")
  SPY &lt;- as.numeric(SPY$SPY.Close)
  
  set.seed(123)
  
  #create a time index
  t &lt;- 1:(length(SPY)-1)
  
  #tradable capital vector
  Vt &lt;- c(rep(10000, length(t)))
  
  #Benchmark return series
  Rb &lt;- rep(NA, length(t))
  for(i in 2:length(t)) {
    Rb[i] &lt;- (SPY[i] / SPY[i - 1]) - 1
  }
  
  #Benchmark equity curve
  Eb &lt;- rep(NA, length(t))
  Eb[1] &lt;- Vt[1]
  for(i in 2:length(t)) {
    Eb[i] &lt;- Eb[i - 1] * (1 + Rb[i])
  }
  
  #Randomy simulated return series 1
  Rt &lt;- rep(NA, length(t))
  for(i in 2:length(t)) {
    Rt[i] &lt;- Rb[i] + rnorm(n = 1,
    mean = 0.24/length(t),
    sd = 2.5 * sd(Rb, na.rm = TRUE))
    }
  
  
  #Randomly simulated return series 2
  Rt2 &lt;- rep(NA, length(t))
  for(i in 2:length(t)) {
    Rt2[i] &lt;- Rb[i] + rnorm(n = 1,
                            mean = 0.02/length(t),
                            sd = 0.75 * sd(Rb, na.rm = TRUE))
  }
  
  # Randomly Simulated Equity Curve 1
  Et &lt;- rep(NA, length(t))
  Et &lt;- Vt[1]
  for(i in 2:length(t)) {
    Et[i] &lt;- Et[i-1] * (1 + Rt[i])
  }
  
  # Randomly Simulated Equity Curve 2
  Et2 &lt;- rep(NA, length(t))
  Et2 &lt;- Vt[1]
  for(i in 2:length(t)) {
    Et2[i] &lt;- Et2[i-1] * (1 + Rt2[i])
    }
  
  #Plot of Et1 against the SPY Portfolio
  
  plot(y = Et, x = t, type = "l", col = 1,
       xlab = "Time",     ylab= "Equity ($)",
       main = "Figure 1-3: Randomly Generated Equity Curves") 
  grid()
  abline(h = 10000)
  lines(y = Et2, x = t, col = 2)
  lines(y = Eb, x = t, col = 8)
  legend(x = "topleft", col = c(1,2,8), lwd = 2, legend = c("Curve 1",
                                                            "Curve 2",
                                                            "SPY"))
  </code></pre>
  
  <p>The above code is what I have ran in order to produce the graphs above. If you are able to run it and get the same error let me know.</p>