以pdf格式存储图表,我之前能够存储,但不再对代码进行一些修改

时间:2012-03-02 20:45:40

标签: r

下面是调用用户定义函数名称meanValPlot的代码。 我可以在鞋底上获得图形输出,但如果我尝试将其保存到pdf中,它就无法正常工作。我相信我错过了很长一段时间,但是如果你能提供帮助我会很感激。

提前致谢。

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)

1 个答案:

答案 0 :(得分:2)

我认为您希望删除dev.new()的第一行并使用pdf调用来创建适当大小的图片:

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()

enter image description here

您当前的代码将接受PDF尺寸的默认值。你需要保留dev.off()。这是你完成文件的唯一方法。