我有一个使用ggplot打印和保存一些图表的功能。用于绘图的数据框在某些列中具有NA - 因此我首先对数据进行子集化(而不是在ggplot中)。当我第一次运行该函数时,它可以工作(当我逐行运行R代码而不是函数时,它对所有输入都有效)。但是,当我尝试第二次使用另一个输入运行该函数时,它会收到错误:
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
经过一些研究,我确信我没有在ggplot图层中的某处调出正确的数据框架,而是使用环境变量而不是输入变量。但是,我无法确定错误。我列出了一些在函数中有效的示例数据。
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 - BUT WILL IF I MANUALLY RESET MY 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")
}