title: How to drop a perpendicular line from each point in a scatterplot to an (Eigen)vector?
body: <p>I'm creating a visualization to <em>illustrate</em> how Principal Components Analysis works, by plotting Eigenvalues for some <em>actual</em> data (for the purposes of the illustration, I'm subsetting to 2 dimensions).</p>
  
  <p>I'm want a combination of these two plots from <a href="https://georgemdallas.wordpress.com/2013/10/30/principal-component-analysis-4-dummies-eigenvectors-eigenvalues-and-dimension-reduction/" rel="noreferrer">this fantastic PCA tutorial</a>, only for my <em>real</em> data.</p>
  
  <p><img src="https://i.stack.imgur.com/bAbAt.jpg" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/K738O.jpg" alt="enter image description here"></p>
  
  <p>I can plot the vectors and all ok:</p>
  
  <pre><code>Person1 &lt;- c(-3,1,1,-3,0,-1,-1,0,-1,-1,3,4,5,-2,1,2,-2,-1,1,-2,1,-3,4,-6,1,-3,-4,3,3,-5,0,3,0,-3,1,-2,-1,0,-3,3,-4,-4,-7,-5,-2,-2,-1,1,1,2,0,0,2,-2,4,2,1,2,2,7,0,3,2,5,2,6,0,4,0,-2,-1,2,0,-1,-2,-4,-1)
  Person2 &lt;- c(-4,-3,4,-5,-1,-1,-2,2,1,0,3,2,3,-4,2,-1,2,-1,4,-2,6,-2,-1,-2,-1,-1,-3,5,2,-1,3,3,1,-3,1,3,-3,2,-2,4,-4,-6,-4,-7,0,-3,1,-2,0,2,-5,2,-2,-1,4,1,1,0,1,5,1,0,1,1,0,2,0,7,-2,3,-1,-2,-3,0,0,0,0)
  df &lt;- data.frame(cbind(Person1, Person2))
  g &lt;- ggplot(data = df, mapping = aes(x = Person1, y = Person2))
  g &lt;- g + geom_point(alpha = 1/3)  # alpha b/c of overplotting
  g &lt;- g + geom_smooth(method = "lm")  # just for comparsion
  g &lt;- g + coord_fixed()  # otherwise, the angles of vectors are off
  corre &lt;- cor(x = df$Person1, y = df$Person2, method = "spearman")  # calculate correlation, must be spearman b/c of measurement
  matrix &lt;- matrix(c(1, corre, corre, 1), nrow = 2)  # make this into a matrix
  eigen &lt;- eigen(matrix)  # calculate eigenvectors and values
  eigen$vectors.scaled &lt;- eigen$vectors %*% diag(sqrt(eigen$values))  
    # scale eigenvectors to length = square-root
    # as per http://stats.stackexchange.com/questions/9898/how-to-plot-an-ellipse-from-eigenvalues-and-eigenvectors-in-r
  g &lt;- g + stat_ellipse(type = "norm")
  g &lt;- g + stat_ellipse(type = "t")
    # add ellipse, though I am not sure which is the adequate type
    # as per https://github.com/hadley/ggplot2/blob/master/R/stat-ellipse.R
  g &lt;- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,1], colour = "green")  # add slope for pc1
  g &lt;- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,2], colour = "red")  # add slope for pc2
  g &lt;- g + geom_segment(aes(x = 0, y = 0, xend = max(df), yend = eigen$vectors.scaled[1,1] * max(df)), colour = "green", arrow = arrow(length = unit(0.2, "cm")))  # add arrow for pc1
  g &lt;- g + geom_segment(aes(x = 0, y = 0, xend = max(df), yend = eigen$vectors.scaled[1,2] * max(df)), colour = "red", arrow = arrow(length = unit(0.2, "cm")))  # add arrow for pc1
  g
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/8gRXz.png" alt="enter image description here"></p>
  
  <p>So far so good (well). 
  <strong>How do I know use <code>geom_segment</code> to drop a perpendicular from every datapoint to, say, the green <em>first</em> principal component?</strong></p>