Reputation: 6489
below is the code that invokes a user-defined function name meanValPlot. I am able to get the graphic output on sole but if I try to save the same into pdf, it's not working. I believe I am missing out something very minute but appreciate if you could help.
Thanks in advance.
library(raster)
library(splines)
#function definition
meanValPlot<-function(input,disp=1, xlims=c(1,24),
product='test model', upDir=getwd(),
lineCols='orange', ylims=c(0,1)){
counter<-dim(input)[2]
# specifies the number of models we have for particular variable
dev.new(width=3.33,height=3.0)
#pdf(file='test1march.pdf',width=3.33,height=3.0)
par(mar=c(2.5,3,1,1))
plot(NULL,NULL,xlim=xlims,ylim=ylims,axes=FALSE, ann=FALSE)
axis(1, at=c(0,4,8,12,16,20,24),cex.axis=0.7,mgp=c(3, .3, 0))
axis(2, las=1,cex.axis=0.7,at=c(-2,-1,0,1,2), mgp=c(3, .7, 0))
mtext(side = 1, text =expression('Hour'), line = 1,font=15)
mtext(side = 2, text = expression(paste('NEE ', m^-2,s^-1)), line = 1.5,font=15)
sapply(1:counter,function(x){
xa<-1:24
yy<-predict(interpSpline(xa,input[,x]))
lines(yy,col=lineCols[x],type="l", pch=22, lty=1, lwd=1)
})
box()
#dev.off()
par(mar=c(5, 4, 4, 2) + 0.1)
}#end meanValPlot
#setting the array values
NEE_values<-mat.or.vec(24,2)
NEE_values[,1]<-c(0.0188026316,0.0185296053,0.0181710526,0.0169046053,0.0140860927,0.0101523179,0.0051907895,0.0005065359,-0.0034477124,-0.0065620915,-0.0092810458, -0.0118823529,-0.0138267974, -0.0154084967, -0.0163039216,-0.0155261438, -0.0145522876, -0.0108921569,-0.0045164474,0.0018388158, 0.0083453947, 0.0136710526,
0.0170065789,0.0184342105)
NEE_values[,2]<-c(0.85902632, 0.90200329,0.91822697, 0.85789145,0.73128808,0.52779470, 0.20285855,-0.12150654,-0.45393464,-0.82072876,-1.08471242,-1.32177124,
-1.38211765,-1.39515033,-1.38801961,-1.31286601,-1.22851307,-1.03020588,-0.72745066,-0.39643750, 0.02863816,0.37534539,0.60530921, 0.79088816)
#invoking function here
ylims<-c(-2,2)
lineCols <- c('blue','black')
meanValPlot(input=NEE_values,product='NEE Site data',upDir=outpath, lineCols=lineCols, ylims=ylims)
Upvotes: 0
Views: 164
Reputation: 263481
I think you want to drop the first line with dev.new()
and use the pdf
call to create the proper sized image:
xlims<-c(1,24)
ylims<-c(-2,2)
pdf(file='test1march.pdf', width=3.33,height=4)
plot(NULL,NULL,xlim=xlims,ylim=ylims,axes=FALSE, ann=FALSE)
axis(1, at=c(0,4,8,12,16,20,24),cex.axis=0.7,mgp=c(3, .3, 0))
axis(2, las=1,cex.axis=0.7,at=c(-2,-1,0,1,2), mgp=c(3, .7, 0))
mtext(side = 1, text =expression('Hour'), line = 1,font=15)
mtext(side = 2, text = expression(paste('NEE ', m^-2,s^-1)), line = 1.5,font=15)
abline(0, 2/24) # should give a diagonal line
box()
dev.off()
Your current code will be accepting the default for the PDF dimensions. You do need to keep the dev.off(). That's the only way you can finish the file.
Upvotes: 2