Announcement

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

  • Graph export multiple .gph files after editing

    Hi,

    I am trying to export image files (in .png format) after removing notes and title from existing graph (.gph) files in Stata. Using gr_edit commands below I can do this for individual .gph files:

    graph use "$dir1/This is graph one.gph"
    gr_edit .note.text = {}
    gr_edit .title.text = {}
    graph export "$dir1/This is graph one - no title.png", replace

    I need to do this for multiple .gph files in multiple folders. Is it possible to use a foreach loop over the list of .gph files in each folder to create .png files as opposed to manually doing it for each file separately as above?

    Although there are hundreds of .gph files in over 10 directories, consider below as the list of existing files:
    $dir1/This is graph one.gph
    $dir1/Graph two.gph
    $dir2/I am graph three.gph
    $dir3/This is graph four.gph

    I need to export these image files:
    $dir1/This is graph one - no title.png
    $dir1/Graph two - no title.png
    $dir2/I am graph three - no title.png
    $dir3/This is graph four - no title.png


    Any help is appreciated.

  • #2
    Don't use graph editor. What graphs are you working with? Give a dataset and code you wanna use to make your graphs (using the dataex command, please). Welcome to Statalist. Please, do read the FAQ section.

    Comment


    • #3
      I am sorry, I was not completely clear in my initial post. The graph generation process that I am currently adopting involves creating two versions of each graph - one with title/notes and the other without (for presentation purposes). This results in unwieldy and duplicate code, and managing the code is becoming difficult because any changes that are made in one version needs to be made in the other version too.

      Alternatively, I can manually take the saved .gph file for each graph (version With Title Notes) and strip the title/notes from this file and export it as a "No Title Notes" version using the below commands
      gr_edit .note.text = {}
      gr_edit .title.text = {}

      Since there are hundreds of such graphs, I am seeking an option to loop through all .gph files and create "No Title Notes" graph versions instead of working with each graph individually.

      Here is the sample code to help you understand on what I am trying to accomplish.

      Code:
      sysuse auto
      
      // Current graph generation method I am using
      // Graph 1 - with title and notes
      twoway(scatter price mpg), ///
      title("Price vs mpg") ///
      note("Source: auto dataset from Stata")
      graph save "Price vs mpg - With Title Notes", replace
      graph export "Price vs mpg - With Title Notes.png", replace
      
      
      // Graph 1 - without title and notes
      twoway(scatter price mpg)
      graph export "Price vs mpg - No Title Notes.png", replace
      
      
      // Graph 2 - with title and notes
      twoway(scatter weight length), ///
      title("Weight vs Length") ///
      note("Source: auto dataset from Stata")
      graph save "Weight vs Length - With Title Notes", replace
      graph export "Weight vs Length - With Title Notes.png", replace
      
      // Graph 2 - without title and notes
      twoway(scatter weight length)
      graph export "Weight vs Length - No Title Notes.png", replace
      
      //////////////////////////////////////////////////
      
      // Alternative method that I would like to use in the future to generate "No Title Notes" versions
      // By using the .gph file generated for the "With Title Notes" version graphs
      graph use "Price vs mpg - With Title Notes.gph"
      gr_edit .note.text = {}
      gr_edit .title.text = {}
      graph export "Price vs mpg - No Title Notes.png", replace
      
      graph use "Weight vs Length - With Title Notes.gph"
      gr_edit .note.text = {}
      gr_edit .title.text = {}
      graph export "Weight vs Length - No Title Notes.png", replace

      So, going back to the original question, is there a way to loop through all .gph files in a folder instead of exporting each file separately? I have hundreds of more complex graphs in multiple folders with a variety of style elements applied to the graphs. Thank you for your time.

      Comment


      • #4
        I'll attempt a fix for this, but why even bother making graphs with and without titles and notes? You mentioned yourself that it's a duplicate of code, so why even put yourself through the headache?

        Is it REALLY worth the effort and trouble all for presentation purposes? Again I'll attempt a fix, but I don't think this is worth it

        Comment


        • #5
          Thank you, Jared. I wish I could do away with the No Title Notes version, but they both really do need to be generated.

          Comment


          • #6
            Well I guess my thing is why? According to who? Are you deciding this, or is this something being dictated to you?

            Anyways, when I finish my final paper in a few minutes, I'll give it a look. I'll likely use a counter of some kind (if i is odd, with title and notes, if i is even, without title or notes)

            Comment


            • #7
              There's a bit of an inconsistency between what you suggest in post #1 and what you suggest in post #3, but I think the following will point you in a useful direction to accomplish what you outline in post #3. The key is using the community contributed filelist command available from SSC to create a dataset of directory and file names for your .gph files, and then stashing that dataset into a separate frame and retrieving the pairs of directory and file names one pair at a time.
              Code:
              * ssc install filelist
              local path ~/Downloads/graphs
              
              sysuse auto
              
              // Graph 1 - with title and notes
              twoway(scatter price mpg), ///
              title("Price vs mpg") ///
              note("Source: auto dataset from Stata")
              graph save "`path'/dir1/Price vs mpg - With Title Notes", replace 
              graph export "`path'/dir1/Price vs mpg - With Title Notes.png", replace
              
              // Graph 2 - with title and notes
              twoway(scatter weight length), ///
              title("Weight vs Length") ///
              note("Source: auto dataset from Stata")
              graph save "`path'/dir2/Weight vs Length - With Title Notes", replace
              graph export "`path'/dir2/Weight vs Length - With Title Notes.png", replace
              
              graph close
              
              filelist, directory("`path'") pattern("*.gph")
              list
              
              local ngr `c(N)'
              frame copy default graphs, replace
              
              forvalues g=1/`ngr' {
                  frame graphs {
                      local dn = dirname[`g']
                      local fn = filename[`g']
                  }
                  graph use "`dn'/`fn'"
                  gr_edit .note.text = {}
                  gr_edit .title.text = {}
                  local nfn : subinstr local fn " With " " No "
                  local nfn : subinstr local nfn ".gph" ".png"
                  graph export "`dn'/`nfn'", replace
               
              }
              
              graph close
              Code:
              . list
              
                   +---------------------------------------------------------------------------+
                   | dirname                   filename                                  fsize |
                   |---------------------------------------------------------------------------|
                1. | ~/Downloads/graphs/dir1   Price vs mpg - With Title Notes.gph       5,767 |
                2. | ~/Downloads/graphs/dir2   Weight vs Length - With Title Notes.gph   5,803 |
                   +---------------------------------------------------------------------------+
              
              . 
              . local ngr `c(N)'
              
              . frame copy default graphs, replace
              (note: frame graphs not found)
              
              . 
              . forvalues g=1/`ngr' {
                2.     frame graphs {
                3.         local dn = dirname[`g']
                4.         local fn = filename[`g']
                5.     }
                6.     graph use "`dn'/`fn'"
                7.     gr_edit .note.text = {}
                8.     gr_edit .title.text = {}
                9.     local nfn : subinstr local fn " With " " No "
               10.     local nfn : subinstr local nfn ".gph" ".png"
               11.     graph export "`dn'/`nfn'", replace
               12.  
              . }
              file ~/Downloads/graphs/dir1/Price vs mpg - No Title Notes.png saved as PNG format
              file ~/Downloads/graphs/dir2/Weight vs Length - No Title Notes.png saved as PNG format
              
              .

              Comment


              • #8
                If you want to get an arbitrary list of files, using the macro `dir` function is your friend (see `help macro`). This code should do what you want, although I haven't tested it.

                Code:
                foreach dir in $dir1 $dir2 $dir3 {
                
                  loc all_graphs : dir "`dir'" files "*.gph"
                
                  foreach graph of local all_graphs {
                
                   graph use "`dir'/`graph'"
                   gr_edit .note.text = {}
                   gr_edit .title.text = {}
                
                   *we need to strip the ".gph" to get the final file name
                   loc graphname : subinstr local graph ".gph" ""
                
                   graph export "`dir'/`graphname' - no title.png", replace
                
                  }
                }
                Edit: William Lisowski posted a good solution while I was editing; my only note is that you could potentially skip the "filelist" and use the "dir" macro function, thus avoiding the extra step of the frames. I like the extra `subinstr` to swap "with title" for "no title".
                Last edited by Kye Lippold; 06 Dec 2022, 14:04.

                Comment


                • #9
                  Thank you, William Lisowski and Kye Lippold. Both your solutions work well.

                  Comment


                  • #10
                    I second #2 in wanting to avoid fiddling with the Graph Editor.
                    Here's some code that puts everything in loops instead.

                    Code:
                    sysuse auto, clear
                    
                    local title_gr1 Price vs mpg
                    local note_gr1 Source: auto dataset from Stata
                    local vars_gr1 price mpg
                    local fn_gr1 Price vs mpg
                    
                    local title_gr2 Weight vs Length
                    local note_gr2 Source: auto dataset from Stata
                    local vars_gr2 weight length
                    local fn_gr2 Weight vs Length
                    
                    local with_no With No
                    
                    forval i = 1/2 {
                        foreach version of local with_no {
                            if "`version'" == "With" {
                                    local title_op title("`title_gr`i''")
                                    local note_op note("`note_gr`i''")
                            }
                            else {
                                local title_op
                                local note_op
                            }
                            
                            twoway scatter `vars_gr`i'', `title_op' `note_op'
                            if "`version'" == "With" graph save "`fn_gr`i'' - With Title Notes", replace
                            
                            graph export "`fn_gr`i'' - `version' Title Notes.png", replace
                        }
                    }
                    Last edited by Hemanshu Kumar; 06 Dec 2022, 22:18.

                    Comment

                    Working...
                    X