title: ggplot in function using environment variables: arguments imply differing number of rows
body: <p>I have a function to print and save some charts using ggplot.  The data frame being used for the plots has NAs in some of the columns - so I subset the data first (and not in ggplot).  When I run the function for the first time, it works (and when I run the code in R line-by-line and not as a function, it works as well for ALL inputs).  However, when I try to run the function a second time with another input, it receive the error:</p>
  
  <pre><code>Error in data.frame(x = c(1, 21, 41, 53, 75, 80, 87, 100), y = c(1000,  : 
  arguments imply differing number of rows: 8, 6
  </code></pre>
  
  <p>After some research, I'm convinced I'm not calling out a correct data frame somewhere in the ggplot layers, and the environment variable is getting used instead of the input variable.  But, I cannot pinpoint the error.  I've listed some sample data that works in the function.   </p>
  
  <pre><code>smpl_data &lt;- data.frame(c(2011,2011,2011,2011,2012,2012,2012,2012),c(1,21,41,53,75,80,87,100),c(1000,1100,1200,1300,950,1050,1150,1250),c(2100,NA,2200,2300,2050,2150,NA,2350))
  names(smpl_data) &lt;- c("year","Date_Rank","ModWS_00X_3","ModWS_53X_3")
  
  #THE FOLLOWING CODE WORKS:
  testfunc(DF=smpl_data,year1=2011,year2=2012,module="00X",workscope=3)
  #THIS DOESNT - BUT WILL IF I MANUALLY RESET MY chrt DATASETS:
  testfunc(DF=smpl_data,year1=2011,year2=2012,module="53X",workscope=3)
  
  testfunc &lt;- function(DF, year1, year2, module, workscope) { 
  
  #SUBSET DATA
  mod_ws = paste("ModWS_", module, "_", workscope, sep="")
  chrt_data &lt;- subset(DF, !is.na(DF[[mod_ws]]))
  chrt_d1 &lt;- subset(chrt_data, chrt_data$year == year1)
  chrt_d2 &lt;- subset(chrt_data, chrt_data$year == year2)
  
  #RUN CHART
  mean1 &lt;- mean(chrt_d1[[mod_ws]])
  mean2 &lt;- mean(chrt_d2[[mod_ws]])
  upsig1 &lt;- mean1+(3*(sqrt(var(chrt_d1[[mod_ws]]))))
  dnsig1 &lt;- mean1-(3*(sqrt(var(chrt_d1[[mod_ws]]))))
  upsig2 &lt;- mean2+(3*(sqrt(var(chrt_d2[[mod_ws]]))))
  dnsig2 &lt;- mean2-(3*(sqrt(var(chrt_d2[[mod_ws]]))))
  upmax = max(upsig1,upsig2)
  dnmin = min(dnsig1, dnsig2)
  
  runchart &lt;- ggplot(chrt_data) +
  geom_line(aes(x=chrt_data$Date_Rank,y=chrt_data[[mod_ws]],colour=as.factor(chrt_data$year)),size=2) + 
  geom_segment(x=0,xend=74,y=mean1,yend=mean1,linetype=2,size=1,colour="red") +
  geom_segment(x=61,xend=102,y=mean2,yend=mean2,linetype=2,size=1,colour="blue") +
  geom_segment(x=0,xend=74,y=upsig1,yend=upsig1,linetype=2,size=1,colour="green") +
  geom_segment(x=0,xend=74,y=dnsig1,yend=dnsig1,linetype=2,size=1,colour="green") +
  geom_segment(x=61,xend=102,y=upsig2,yend=upsig2,linetype=2,size=1,colour="green") +
  geom_segment(x=61,xend=102,y=dnsig2,yend=dnsig2,linetype=2,size=1,colour="green") + 
  ylim(dnmin-1000,upmax+1000) + xlab("Date") + ylab("Cost") + scale_colour_discrete(name="Year")
  
  print(runchart)
  dev.print(bmp, file=paste("C:/filepath/R function output/R_charts_",mod_ws,"IMR.bmp",sep=""),width=900,height=150,units="px")
  dev.off
  
  #DENSITY PLOT
  MEANS &lt;- data.frame(c(2011,2012),c(mean(chrt_d1[[mod_ws]]),mean(chrt_d2[[mod_ws]])))
  names(MEANS)=c("year","avg")
  cmax = max(max(chrt_d1[[mod_ws]]),max(chrt_d2[[mod_ws]]))
  cmin = min(min(chrt_d1[[mod_ws]]),min(chrt_d2[[mod_ws]]))
  lrange = cmin - (.5*(cmax-cmin))
  rrange = cmax + (.5*(cmax-cmin))
  
  Dplot &lt;- ggplot(chrt_data, aes(x=chrt_data[[mod_ws]], colour=as.factor(chrt_data$year))) + geom_density(size=2) + geom_hline(yintercept=0,size=2) +
  geom_vline(data=MEANS, xintercept=MEANS$avg, linetype=2, size=1) + 
  xlim(lrange, rrange) +
  xlab(paste(mod_ws," Cost",sep="")) +
  scale_colour_discrete(name="Year")
  
  print(Dplot)
  dev.print(bmp, file=paste("C:/filepath/R function output/R_charts_",mod_ws,"Density.bmp",sep=""),width=900,height=150,units="px")   
  dev.off()
  
  #BOX PLOT
  BOX &lt;- ggplot(chrt_data, aes(x=as.factor(chrt_data$year),y=chrt_data[[mod_ws]], colour=as.factor(chrt_data$year)))+
  geom_boxplot(size=1.5, outlier.colour="green",outlier.size=3) +
  guides(fill=FALSE) + ylim(lrange,rrange) + coord_flip() +
  ylab(paste(mod_ws," Cost",sep="")) +
  xlab("Year") +
  scale_colour_discrete(name="Year")
  
  print(BOX)
  dev.print(bmp,file=paste("C:/filepath/R function output/R_charts_",mod_ws,"BOX.bmp",sep=""),width=900,height=150,units="px")
  dev.off()
  
  
  return("Done")
  
  }
  </code></pre>