使用R编写导出PDF图形的函数,但PDF已损坏

时间:2015-04-23 21:22:08

标签: r function pdf ggplot2 export-to-pdf

我已经编写了这个函数来导出R中的图形。当我运行这个函数时,创建了pdf文件,但是它们已经损坏并且无法打开(或者当我导出png文件时文件是空的) 。当我运行各行代码时,不是它们工作的功能的一部分。

帮助!!我认为它必须与在函数中制作表格或图形有关,(我必须做一些像返回图形/数据框的东西吗?)但我不知道是什么!

    graphFunction <- function(fileName){

pdfTitle <- paste(fileName, "_graph", ".pdf", sep = "")

File <- read.csv(fileName, quote = "")
ByPopAge<- ddply(File, .(age), summarise, # need to discount trials where no feeding obs and eve
        NCols = length(!is.na(colony_ID)),
        TotNumInd = sum(num.indvs)
)

pdf(pdfTitle, width =10, height =10)

ggplot(data = ByPopAge, aes(x = age, y = TotNumInd)) + geom_line() + geom_point()   
ggplot(data = ByPopAge, aes(x = age, y = NCols)) + geom_line() + geom_point()

dev.off() }

谢谢!

1 个答案:

答案 0 :(得分:3)

尝试做:

var ary1 =[{userName:'vimal', userId:789}, {userName:'kabilan', userId:456}, {userName:'yathavan', userId:123}];
var ary2 = [{userId:123, msg:'hi'}, {userId:789, msg:'yth'}, {userId:789, msg:'hu'}];
function specialMerge(ar1, ar2){
  var r = [];
  for(var i=0,l=ar1.length; i<l; i++){
    var p = ar1[i];
    for(var n=0,c=ar2.length; n<c; n++){
      var m = ar2[n];
      if(p.userId === m.userId){
        r.push({userId:p.userId, userName:p.userName, msg:m.msg});
      }
    }
  }
  return r;
}
var resultArrayOfObjects = specialMerge(ary1, ary2);

用打印包装它应该可以使它工作。