How to append a plot to an existing pdf file

前端 未结 4 1096
刺人心
刺人心 2020-12-13 09:46

I want to append a plot to an existing pdf long after dev.off() has been called*. After reading the pdf() help file and after reading the Q &

4条回答
  •  臣服心动
    2020-12-13 10:09

    If you are willing to install the small, free, platform-independent pdftk utililty, you could use a system call from R to have it stitch all of your figures together:

    ## A couple of example pdf docs
    pdf("Append to me.1.pdf")
    plot(1:10,10:1)
    dev.off()
    
    pdf("Append to me.2.pdf")
    plot(1:10,rep(5,10)) 
    dev.off()
    
    ## Collect the names of the figures to be glued together
    ff <- dir(pattern="Append to me")
    ## The name of the pdf doc that will contain all the figures
    outFileName <- "AllFigs.pdf"
    
    ## Make a system call to pdftk
    system2(command = "pdftk",
            args = c(shQuote(ff), "cat output", shQuote(outFileName)))
    
    ## The command above is equiv. to typing the following at the system command line
    ## pdftk "Append to me.1.pdf" "Append to me.2.pdf" cat output "AllFigs.pdf"
    

提交回复
热议问题