title: R multiplot - R cookbook error 'Aesthetics must either be length one...'
body: <p>I am trying to make two plots next to one another using the R cookbook <code>multiplot</code> function, although I am getting the following error:</p>
  
  <blockquote>
    <p><code>Aesthetics must either be length one, or the same length as the dataProblems:v.x</code></p>
  </blockquote>
  
  <p>Can someone help? My code is below:</p>
  
  <pre><code>rm(list=ls())
  library(ggplot2)
  library(grid)
  
  # Multiple plot function
  multiplot &lt;- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
    require(grid)
  
    # Make a list from the ... arguments and plotlist
    plots &lt;- c(list(...), plotlist)
  
    numPlots = length(plots)
  
    # If layout is NULL, then use 'cols' to determine layout
    if (is.null(layout)) {
      # Make the panel
      # ncol: Number of columns of plots
      # nrow: Number of rows needed, calculated from # of cols
      layout &lt;- matrix(seq(1, cols * ceiling(numPlots/cols)),
                       ncol = cols, nrow = ceiling(numPlots/cols))
    }
  
    if (numPlots==1) {
      print(plots[[1]])
  
    } else {
      # Set up the page
      grid.newpage()
      pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
  
      # Make each plot, in the correct location
      for (i in 1:numPlots) {
        # Get the i,j matrix positions of the regions that contain this subplot
        matchidx &lt;- as.data.frame(which(layout == i, arr.ind = TRUE))
  
        print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                            layout.pos.col = matchidx$col))
      }
    }
  }
  
  # Define beta binomial function
  dbb &lt;- function(x, N, u, v) {
    beta(x+u, N-x+v)/beta(u,v)*(gamma(N+1)/(gamma(x+1)*gamma(N-x+1)))
  }
  
  #Prior parameters
  a&lt;-1
  b&lt;-1
  
  #Data
  N1&lt;-100
  X&lt;-10
  
  #New sample
  N2 &lt;- 100
  
  # Generate prior, likelihood, posterior and posterior predictive distribution
  v.theta &lt;- seq(0,1,length=1000) 
  v.x &lt;-seq(0,100,length=100) 
  v.prior &lt;- dbeta(v.theta,a,b)
  v.likelihood &lt;- choose(N1,X)*(v.theta^X)*(1-v.theta)^(N1-X);
  v.posterior &lt;- dbeta(v.theta,a+X,b+N1-X)
  v.posteriorPred &lt;- dbb(v.x,N2,a+X,b+N1-X)
  
  # Plot as a subplot
  p1&lt;-qplot(v.x,v.prior,geom = c("point", "line"))
  p2&lt;-qplot(v.x,v.likelihood,geom = c("point", "line"))
  
  multiplot(p1, p2)
  </code></pre>