Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mass Export Stata Graphs to PDF

    We are creating a series of process charts (XmR charts) as part of a reporting system for a clinical care pathway for a subset of our patients. Consider the following code:

    foreach indicator in unitdelay icutime mobdelay tfeed foleyout downsize decandays decandelay pointake los {
    * creates x chart *
    twoway (line `indicator' sxdate, sort) (line mean`indicator' sxdate, sort) (line unpl`indicator' sxdate, sort) ///
    (line lnpl`indicator' sxdate, sort), ///
    xtitle(, size(medsmall)) xlabel(, labsize(vsmall)) ytitle(Days) title(Time till `indicator' (days), ///
    size(medium)) legend(off) clegend(position(3)) yscale(range(0 10)) name(`indicator', replace)
    graph save "`indicator'", replace

    * creates mR chart *
    twoway (line mr`indicator' sxdate, sort) (line meanmr`indicator' sxdate, sort) (line url`indicator' sxdate, sort), ///
    xtitle(, size(medsmall)) xlabel(, labsize(vsmall)) ytitle(Days) legend(off) yscale(range(0 10)) name(mr`indicator', replace)
    graph save "mr`indicator'", replace

    * create the XmR chart *
    graph combine "`indicator'" "mr`indicator'", cols(1) name(xmr`indicator', replace)
    graph save "xmr `indicator'", replace
    graph export "xmr `indicator'.tif", replace
    }

    As a result of the loop, we have a series of 30 graphs in total all saved to disk and also all retained in the graph window. Being able to save all of the graphs collected in the graph window as a single pdf file would make my boss a very happy person. I am aware that we can manually compile these files into a pdf document, but that will be more labour intensive and error prone than we're interested.

    There will certainly be some sort of AppleScript that I can run to accomplish this goal. However, the question is whether or not there is some manner to do entirely from within Stata that I can incorporate into my do file.

  • #2
    May be generate multiple graphs, combine them and export them.



    Code:
    sysuse auto, clear
    scatter price rep78, name(one, replace)
    scatter rep78 mpg, name(two, replace)
    graph combine one two 
    graph export new.pdf


    Thanks,
    Anwar

    Comment


    • #3
      I usually handle this using graphexport from SSC and shelling out to the command line with pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) to merge the individual graphs into a master file:

      Code:
      tw ...
      graphexportpdf g1, dropeps;
      
      tw ...
      graphexportpdf g2, dropeps;
      
      !rm master_file.pdf;
      !pdftk g1.pdf g2.pdf cat output master_file.pdf;
      winexec xpdf master_file.pdf -z 300;
      The last line will open the combined file in a pdf viewer.

      Comment


      • #4
        Originally posted by Steve Nakoneshny View Post
        As a result of the loop, we have a series of 30 graphs in total all saved to disk and also all retained in the graph window.
        Steve, have you managed to put all 30 graphs in a single graph window?? In multiple tabs within one window?
        If yes, I'd really like to know how.

        GhostScript has been with us for about 25 years now, and should be able to handle what you requested. See e.g.
        this article and this reference.

        Thanks, Sergiy

        Attached Files

        Comment


        • #5
          Originally posted by dvmaster View Post
          I usually handle this using graphexport from SSC and shelling out to the command line with pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) to merge the individual graphs into a master file:

          Code:
          tw ...
          graphexportpdf g1, dropeps;
          
          tw ...
          graphexportpdf g2, dropeps;
          
          !rm master_file.pdf;
          !pdftk g1.pdf g2.pdf cat output master_file.pdf;
          winexec xpdf master_file.pdf -z 300;
          The last line will open the combined file in a pdf viewer.
          What is the advantage of SSC's graphexportpdf relative to the standard graph export foo.pdf?

          I have found that graph export occasionally produces pdfs with errors (for example, sometimes it struggles with greek characters or math symbols) so I prefer to send to, e.g., epstopdf. Unfortunately this becomes difficult when running in batch mode since for some reason Stata does not like calling external programs when running in batch mode.

          PS: Stata 12.2 SE on Win 7 - 64.
          PPS:
          $ epstopdf --version
          MiKTeX EPS-to-PDF Converter 2.9.4535 (MiKTeX 2.9 64-bit)

          Comment


          • #6
            I'm curious about your statement that Stata doesn't like calling external programs in batch. I do this routinely (very typically running esptopdf as it happens) in Linux. Does Windows behave differently?

            Comment


            • #7
              Originally posted by Brendan Halpin View Post
              I'm curious about your statement that Stata doesn't like calling external programs in batch. I do this routinely (very typically running esptopdf as it happens) in Linux. Does Windows behave differently?
              In my experience, yes. See http://www.stata.com/statalist/archi.../msg00537.html and http://www.stata.com/statalist/archi.../msg01232.html.

              Comment


              • #8
                To get back to the OP's question, I believe he wanted to combine multiple graphs as separate pages in a single PDF file. I don't believe Stata can do this, but there are several other options, and most of these are easily accessible on OS X (your mention of Applescript suggests that you are using OS X). One of the best options is Ghostscript (as suggested by Sergiy). This doesn't come preinstalled on OS X, but it is easily installed (I believe my version is the one I got with the MacTex installer, but you could also install it with brew). Once you got it installed, you can add something like this to your do-file:
                Code:
                !/usr/local/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dAutoRotatePages=/None -sOutputFile=combined.pdf plots/?*.pdf
                which would combine all of the PDF files in the plots subdirectory into a single file called combined.pdf. Ghostscript has scores (or even hundreds) of options, which should accommodate most additional requirements you might have.

                If you want to avoid dealing with Ghostscript, you could also consider use of Automator on OS X (see this example).

                Finally, since PDF is a native file format on OS X, you shouldn't have any problems generating the original PDF files from within Stata. However, if you prefer to generate the original files in postscript, Ghostscript can translate those to PDF and combine them for you.
                Last edited by Phil Schumm; 26 Apr 2014, 09:23.

                Comment


                • #9
                  Phil, Thank you! Your post saved my day (I also use a Mac). Instead of combining several pdf files that are located in a given folder, as the one called "plots" in your example, is there a way to combine specific files, but not others? If for instance if I have 4 pdf documents in the folder "plots", called gr1.pdf-gr4.pdf, how can I (1) combine gr1.pdf and gr2.pdf (but not gr3 and gr4) in a single file "combined.pdf", and (2) make sure that the order of gr1, then gr2?

                  Comment


                  • #10
                    Note that combining PDF documents (or subsets) in OS X is easy using Preview. See instructions here. Pay particular attention to the note of item 5.

                    Comment


                    • #11
                      @Sergiy wanted to know how to draw 30 graphs in one graph window.
                      Code:
                      sysuse auto
                      set autotabgraphs on
                      forval i = 1/30 {
                        scatter mpg weight, name(graph`i')
                      }
                      Click image for larger version

Name:	30graphs.png
Views:	1
Size:	16.8 KB
ID:	1293143

                      Comment


                      • #12
                        One solution I have used when faced with hundreds of graphs is to embed them (as links) in an RTF file; Adobe can then convert the entire RTF file to a single PDF. There's a package on SSC, -rtfutil-, which helps with this. A similar strategy also works with TeX/LaTex.

                        Comment

                        Working...
                        X