title: R ggplot2: draw multiple line segments connecting pairs of points on map
body: <p>Consider a map with multiple pairs of points. Using <code>ggplot2</code>, how do you connect each pair of points with a line segment?</p>
  
  <p>Consider the following reproducible example. </p>
  
  <pre><code># Load libraries.
  library(ggmap)    # for getting map
  library(ggplot2)  # for plots
  
  # Create data frame of points on the map.
  data &lt;- data.frame(p1 = c(1, 3, 5), p2 = c(2, 4, 6), 
                     lat_p1 = c(37.78, 37.75, 37.73), 
                     lon_p1 = c(-122.41, -122.40, -122.405), 
                     lat_p2 = c(37.77, 37.75, 37.72), 
                     lon_p2 = c(-122.43, -122.42, -122.415))
  data
    p1 p2 lat_p1   lon_p1 lat_p2   lon_p2
  1  1  2  37.78 -122.410  37.77 -122.430
  2  3  4  37.75 -122.400  37.75 -122.420
  3  5  6  37.73 -122.405  37.72 -122.415
  
  # Get map. 
  map &lt;- get_map(location = c(left = -122.523, bottom = 37.69,
                              right = -122.35, top = 37.813),
                 maptype = "roadmap", source = "osm", color = "bw")
  
  # Plot points on the map. 
  ggmap(map) + 
    geom_point(data = data, aes(x = lon_p1, y = lat_p1), color = "red", 
               size = 1) + 
    geom_point(data = data, aes(x = lon_p2, y = lat_p2), color = "purple", 
               size = 1)
  </code></pre>
  
  <p>In particular, how to connect the red and purple points on the plot with a line segment? </p>
  
  <p>There are several related questions that use <code>geom_line()</code> to draw a single line segment through multiple points. However, I haven't seen a post quite like this one. </p>
  
  <p><a href="https://i.stack.imgur.com/CvqUr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CvqUr.png" alt="enter image description here"></a></p>