title: Bivariate normal with marginal and conditional densities
body: <p>I am trying to create a figure in R. It consists of the contour plot of a bivariate normal distribution for the vector variable (x,y) along with the marginals f(x), f(y); the conditional distribution f(y|x) and the line through the conditioning value X=x (it will be a simple abline(v=x)).
  I already got the contour and the abline: </p>
  
  <p><img src="https://i.stack.imgur.com/u3WX5.png" alt="http://i62.tinypic.com/noyfls.png"></p>
  
  <p>but I don't know how to continue.</p>
  
  <p>Here is the code I used so far:</p>
  
  <pre><code>bivariate.normal &lt;- function(x, mu, Sigma) {
    exp(-.5 * t(x-mu) %*% solve(Sigma) %*% (x-mu)) / sqrt(2 * pi * det(Sigma))
  }
  
  mu &lt;- c(0,0)
  Sigma &lt;- matrix(c(1,.8,.8,1), nrow=2)
  x1 &lt;- seq(-3, 3, length.out=50)
  x2 &lt;- seq(-3, 3, length.out=50)
  
  z &lt;- outer(x1, x2, FUN=function(x1, x2, ...){
               apply(cbind(x1,x2), 1, bivariate.normal, ...)
             }, mu=mu, Sigma=Sigma)
  
  contour(x1, x2, z, col="blue", drawlabels=FALSE, nlevels=4,
          xlab=expression(x[1]), ylab=expression(x[2]), lwd=1)
  abline(v=.7, col=1, lwd=2, lty=2)
  text(2, -2, labels=expression(x[1]==0.7))
  </code></pre>