title: Vectorizing: plotting multiple contours with geom_path
body: <p>I'm writing code to create multiple data sets of contours. My steps are:</p>
  
  <ol>
  <li><p>Generate raw data with </p>
  
  <p><code>T&lt;-runif()</code></p></li>
  <li><p>Write functions to describe a contour described by parameteric functions:</p>
  
  <ul>
  <li><code>x&lt;-function(t)</code></li>
  <li><code>y&lt;-function(t)</code></li>
  </ul></li>
  <li><p>Convert vectors of real numbers <code>x,y</code> to complex numbers <code>x + 1i*y</code></p></li>
  <li><p>Apply a complex function on the data, for example, </p>
  
  <p><code>sin</code></p></li>
  <li><p>Extract the real and imaginary parts into sets of real numbers </p>
  
  <p><code>fx, fy</code>.</p></li>
  <li><p>Plot the pairs `</p>
  
  <p><code>geom_path(x=fx,y=fy)</code></p></li>
  <li><p>color code it with a vector of colors</p></li>
  </ol>
  
  <p>I've been reusing the code every time I want to draw a contour, but I'd like to vectorize it so I can minimize the repetition on my code. How can I do that?</p>
  
  <p>Here's an example:</p>
  
  <pre><code>  #Create data
  
    #the parameter:
    T&lt;-sort(runif(100,min=-1, max=1))
  
  
    #x(t), y(t)
    xt.hor&lt;-function(t,c){
      return(c)
    }
    yt&lt;-function(t,scale){
      return (scale*t)
    }
  
  
    X1&lt;-mapply(xt.hor,T,c=-10)
    # X2, X3, X4
    X5&lt;-mapply(xt.hor,T,c=10)
  
    YHor&lt;-mapply(yt,T,scale=10)
  
    df1&lt;-data.frame(x1=X1,
                    #x2,x3,x4,
                    x5=X5,yh=YHor)
  
  
    #convert to complex numbers:
  
    toComplex&lt;-function(x,y){complex(real=x,imaginary=y)}
  
    z1&lt;-mapply(toComplex,df1$x1,y=df1$yh)
    #z2, z3, z4
    z5&lt;-mapply(toComplex,df1$x5,y=df1$yh)
  
    fz1&lt;-sapply(z1,sin)
    #fz2,fz3,fz4
    fz5&lt;-sapply(z5,sin)
  
    fx1&lt;-sapply(fz1,Re)
    #fx2,fx3,fx4
    fx5&lt;-sapply(fz5,Re)
  
    fy1&lt;-sapply(fz1,Im)
    #fy2,fy3,fy4
    fy5&lt;-sapply(fz5,Im)
  
    df2&lt;-data.frame(fx1=fx1,fy1=fy1,
                    #curve2, curve 3, curve 4
                    fx5=fx5,fy5=fy5)
  
    RedOrgVector&lt;-c('#fd8d3c','#fc4e2a','#e31a1c','#bd0026','#800026')
  
    p &lt;- ggplot(data = df2)
  
    p+xlim(c(-10,10))+ylim(c(-10,10))+
      geom_path(x=fx1,y=fy1,size=.75,color=RedOrgVector[1],linetype=1)+
      #curve2, curve3, curve4
      geom_path(x=fx5,y=fy5,size=.75,color=RedOrgVector[5],linetype=1)
  </code></pre>