用ggplot2画图,想将图存成pdf文件。
同样的代码,在函数中执行生成的pdf文件是损坏的,而在console执行确实可以正确查看。
请问可能是什么原因造成的?
代码如下:
<br />
# show script.<br />
# analysis all the K_alpha values in different place</p>
<p>data = read.table("Kalpha-merged.txt",header=TRUE)<br />
# 数据文件可从http://dl.vmall.com/c06q02liqh下载<br />
library("ggplot2")</p>
<p>P_CAR <- 1<br />
P_Washroom <-2<br />
P_PLAY<-3<br />
P_EXIT<-4<br />
P_ENTRANCE<-5</p>
<p>PLACE <- c("自驾车","影院大厅","放映厅","洗手间","影院出口")</p>
<p>getSegLimitPdf<-function(pop,lower,upper,pos)<br />
{<br />
kvaluefile = paste0("Kalpha/","pop",pop,"-begin",lower,<br />
"-end",upper,"-var-",<br />
pos,".pdf")<br />
x <- data[data$popsize==pop & data$lower==lower & data$upper==upper & data$pos==pos,]<br />
title <- paste0("种群大小",pop,<br />
",生命周期(",lower,",",upper,<br />
"),位置",PLACE[pos],"在不同分块下的K值")<br />
pdf(file=kvaluefile,family="GB1")<br />
# plot(x,main="不定分块大小")<br />
qplot(x$seglmt,x$k,x,<br />
xlab="分块大小",ylab="K值",<br />
main= title)<br />
dev.off()<br />
}</p>
<p>
</p>
数据 data的属性是:
<br />
> str(data)<br />
'data.frame': 206567 obs. of 6 variables:<br />
$ popsize: int 10 10 10 10 10 10 10 10 10 10 ...<br />
$ lower : int 10 10 10 10 10 10 10 10 10 10 ...<br />
$ upper : int 10 10 10 10 10 10 10 10 10 10 ...<br />
$ seglmt : int 10 10 10 10 10 10 10 10 10 10 ...<br />
$ pos : int 1 1 1 1 1 1 1 1 1 1 ...<br />
$ k : num 1.12 1.229 1.232 0.825 0.728 ...<br />
</p>
执行过程如下:
<br />
> source("test.R",encoding='UTF-8')<br />
> getSegLimitPdf(pop=100,lower=5,upper=15,pos=P_PLAY)<br />
null device<br />
1 </p>
<p>> traceback()<br />
1: source("test.R")<br />
> sessionInfo()<br />
R version 2.15.1 (2012-06-22)<br />
Platform: i386-pc-mingw32/i386 (32-bit)</p>
<p>locale:<br />
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936<br />
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936<br />
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936<br />
[4] LC_NUMERIC=C<br />
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936 </p>
<p>attached base packages:<br />
[1] stats graphics grDevices utils datasets methods base </p>
<p>other attached packages:<br />
[1] ggplot2_0.9.1</p>
<p>loaded via a namespace (and not attached):<br />
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2<br />
[4] grid_2.15.1 labeling_0.1 MASS_7.3-20<br />
[7] memoise_0.1 munsell_0.3 plyr_1.7.1<br />
[10] proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.1<br />
[13] scales_0.2.1 stringr_0.6.1 tools_2.15.1<br />
> </p>
<p>
但是打开生成的pdf文件“Kalpha/pop100-5-15-var-3.pdf”发现文件是损坏的,
无法打开。但是文件不是空的,生成的pdf文件,可以从链接中http://dl.vmall.com/c0v4bvxeez 查看。</p>
但是我将函数getSegLimitPdf中的语句在console中手动执行确实可以生成好的pdf文件(见链接http://dl.vmall.com/c0a5cww1ha),
过程如下:
<br />
> ls()<br />
[1] "data" "getSegLimitPdf" "P_CAR" "P_ENTRANCE"<br />
[5] "P_EXIT" "P_PLAY" "P_Washroom" "PLACE"<br />
> pop=100<br />
> lower=5<br />
> upper=15<br />
> pos = P_PLAY<br />
> kvaluefile = paste0("Kalpha/","pop",pop,"-begin",lower,<br />
+ "-end",upper,"-var-",<br />
+ pos,".pdf")<br />
> x <- data[data$popsize==pop & data$lower==lower & data$upper==upper & data$pos==pos,]<br />
> title <- paste0("种群大小",pop,<br />
+ ",生命周期(",lower,",",upper,<br />
+ "),位置",PLACE[pos],"在不同分块下的K值")<br />
> pdf(file=kvaluefile,family="GB1")<br />
> # plot(x,main="不定分块大小")<br />
> qplot(x$seglmt,x$k,x,<br />
+ xlab="分块大小",ylab="K值",<br />
+ main= title)<br />
> dev.off()<br />
null device<br />
1<br />
> traceback()<br />
1: source("test.R")<br />
> sessionInfo()<br />
R version 2.15.1 (2012-06-22)<br />
Platform: i386-pc-mingw32/i386 (32-bit)</p>
<p>locale:<br />
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936<br />
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936<br />
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936<br />
[4] LC_NUMERIC=C<br />
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936 </p>
<p>attached base packages:<br />
[1] stats graphics grDevices utils datasets methods base </p>
<p>other attached packages:<br />
[1] ggplot2_0.9.1</p>
<p>loaded via a namespace (and not attached):<br />
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2<br />
[4] grid_2.15.1 labeling_0.1 MASS_7.3-20<br />
[7] memoise_0.1 munsell_0.3 plyr_1.7.1<br />
[10] proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.1<br />
[13] scales_0.2.1 stringr_0.6.1 tools_2.15.1<br />
><br />
</p>