title: Drawing line segments in R
body: <p>I've got some x and y coordinates that I am trying to plot into line segments. And I'm getting some unexpected behavior from what I think should work. </p>
  
  <p>For each segment, there is a starting set of coordinates (x1,y1) and an ending set of coordinates (x2,y2). It's a data frame (call it 'df') that looks like this:</p>
  
  <pre><code>  x1   y1   x2   y2
  34.9 67.9 62.5 68.8
  66.8 80.9 58.8 88.4
  58.8 88.4 66.0 68.4
  64.0 65.8 56.2 62.6
  56.2 62.6 56.6 75.3
  54.5 70.0 72.9 51.3
  </code></pre>
  
  <p>The segments are not necessarily continuous. By that I mean sometimes the ending x,y of one segment is the starting point of the next segment; other times it is not. </p>
  
  <p>So I try to plot them </p>
  
  <pre><code>lines(c(df$x1, df$x2), c(df$y1, df$y2))
  </code></pre>
  
  <p>And I get the following, which is not at all what I want. There are extra segments being drawn and they are all coming out connected. And it's just wrong. It looks like it's plotting 11 or 12 segments from 6 sets of start/end points. </p>
  
  <p><img src="https://i.stack.imgur.com/OW4nM.jpg" alt="bad plot"></p>
  
  <p>Now, I can step through and plot them one at a time: </p>
  
  <pre><code>lines(c(df$x1[1], df$x2[1]), c(df$y1[1], df$y2[1]))
  lines(c(df$x1[2], df$x2[2]), c(df$y1[2], df$y2[2]))
  lines(c(df$x1[3], df$x2[3]), c(df$y1[3], df$y2[3]))
  Etc.
  </code></pre>
  
  <p>I get the plot below, which is what I'm after. 
  <img src="https://i.stack.imgur.com/ik0Bb.jpg" alt="good lines">
  So can someone help explain what it happening in the first instance that makes it different from the second? And is there a way to do this all in one line without having to step through or write a function that loops through? </p>