title: Line connecting the points in the plot function in R
body: <p>I have a simple problem in the plot function of R programming language. I want to draw a line between the points (<a href="https://stackoverflow.com/questions/14100897/plot-lines-instead-of-points-r">see this link</a> and <a href="http://www.sr.bham.ac.uk/~ajrs/R/r-plot_data.html" rel="noreferrer">how to plot in R</a>), however, what I am getting something weird. I want only one point is connected with another point, so that I can see the function in a continuous fashion, however, in my plot points are connected randomly some other points. Please see the second plot.</p>
  
  <p>Below is the code: </p>
  
  <pre><code>x &lt;- runif(100, -1,1) # inputs: uniformly distributed [-1,1]
  noise &lt;- rnorm(length(x), 0, 0.2) # normally distributed noise (mean=0, sd=0.2)
  f_x &lt;- 8*x^4 - 10*x^2 + x - 4  # f(x), signal without noise
  y &lt;- f_x + noise # signal with noise
  
  # plots 
  x11()
  # plot of noisy data (y)
  plot(x, y, xlim=range(x), ylim=range(y), xlab="x", ylab="y", 
       main = "observed noisy data", pch=16)
  
  x11()
  # plot of noiseless data (f_x)
  plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y", 
       main = "noise-less data",pch=16)
  lines(x, f_x, xlim=range(x), ylim=range(f_x), pch=16)
  
  # NOTE: I have also tried this (type="l" is supposed to create lines between the points in the right order), but also not working: 
  plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y", 
       main = "noise-less data", pch=16, type="l")
  </code></pre>
  
  <p>First plot is correct: <a href="https://i.stack.imgur.com/lSF4Q.png" rel="noreferrer"><img src="https://i.stack.imgur.com/lSF4Q.png" alt="enter image description here"></a>
  While second is not what I want, I want a continuous plot: <a href="https://i.stack.imgur.com/zYpHe.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zYpHe.png" alt="enter image description here"></a> </p>