r - ggplot in function using environment variables: arguments imply differing number of rows -


i have function print , save charts using ggplot. data frame being used plots has nas in of columns - subset data first (and not in ggplot). when run function first time, works (and when run code in r line-by-line , not function, works inputs). however, when try run function second time input, receive error:

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 

after research, i'm convinced i'm not calling out correct data frame somewhere in ggplot layers, , environment variable getting used instead of input variable. but, cannot pinpoint error. i've listed sample data works in function.

smpl_data <- 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) <- 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 - if manually reset chrt datasets: testfunc(df=smpl_data,year1=2011,year2=2012,module="53x",workscope=3)  testfunc <- function(df, year1, year2, module, workscope) {   #subset data mod_ws = paste("modws_", module, "_", workscope, sep="") chrt_data <- subset(df, !is.na(df[[mod_ws]])) chrt_d1 <- subset(chrt_data, chrt_data$year == year1) chrt_d2 <- subset(chrt_data, chrt_data$year == year2)  #run chart mean1 <- mean(chrt_d1[[mod_ws]]) mean2 <- mean(chrt_d2[[mod_ws]]) upsig1 <- mean1+(3*(sqrt(var(chrt_d1[[mod_ws]])))) dnsig1 <- mean1-(3*(sqrt(var(chrt_d1[[mod_ws]])))) upsig2 <- mean2+(3*(sqrt(var(chrt_d2[[mod_ws]])))) dnsig2 <- mean2-(3*(sqrt(var(chrt_d2[[mod_ws]])))) upmax = max(upsig1,upsig2) dnmin = min(dnsig1, dnsig2)  runchart <- 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 <- 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 <- 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 <- 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")  } 


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -