Announcement

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

  • How to let the Stata run multiple graphs at once?

    Hi all,

    Normally, when I run the regression to draw the graph in Stata, if I run two graphs, the latter one will replace the former one. I am wondering if there is any way to let the latter one not replace the former one.

    Many thanks and cheers.

  • #2
    Code:
    foreach x in gap diff {
    
    scatter y x, name(`x')
    }
    EDIT: This was just an example. But the fix is in specifying the name of the graph
    Last edited by Jared Greathouse; 31 Jan 2022, 21:12.

    Comment


    • #3
      Originally posted by Jared Greathouse View Post
      Code:
      foreach x in gap diff {
      
      scatter y x, name(`x')
      }
      EDIT: This was just an example. But the fix is in specifying the name of the graph
      Thanks a lot Jared Greathouse , but I havent caught the idea, for example, my two graphs are as below, could yoiu please tell me to set it up that they can draw the graphs that the latter wont replace the former ?


      Code:
      event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///
          title("ABCD - Developed") xlabel(-3(1)4))    
          
      
      event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///
          title("ABCD- Developing") xlabel(-3(1)4))
      Many thanks in advance.

      Comment


      • #4
        Hi all, from youtube, I saw that we can do something relating to the name function as described here . In particular, if we want to draw three graphs, we should add the extension name (g1) , name (g2),... after the main code

        Code
        Originally, we have the code

        Code:
        webuse auto
        
        graph matrix mpg weight displ
        graph twoway scatter mpg weight
        graph twoway (lfitci mpg weight) (scatter mpg weight)
        These code will only draw a picture.

        However, if we add the extension as below, we will have the three graphs

        Code:
        webuse auto
        
        graph matrix mpg weight displ
        graph twoway scatter mpg weight, name(g1)
        graph twoway (lfitci mpg weight) (scatter mpg weight), name(g2)
        However, when I try to mimic this way, my code announce "error"

        Code:
        . event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///
        >         title("ABCD - Developing") xlabel(-3(1)4)), name(g1) 
        invalid 'name'
        I am wondering what I should do to sort it out?

        Comment


        • #5
          Too many commas, Your code should end

          Code:
           
           xlabel(-3(1)4)) name(g1) 
          as name() is an option just like the other options you are using.

          Comment


          • #6
            Yes, the "name" option is the relevant one here. Me personally, I would also break up your command with three slashes. When I have long code for graphs, I'll usually do something like

            Code:
            scatter y x, mcol(black) || /// plotted data 
            line y x, lcol(red), // fitted data
            ti() /// title
            xlab()
            /* ... and so on so you're organized
            about what option does what */

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Too many commas, Your code should end

              Code:
              xlabel(-3(1)4)) name(g1) 
              as name() is an option just like the other options you are using.
              Hi Nick Cox, thank you fo much for your help, but I face the same problem when I delete the ',' already

              Code:
              . event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///
              >         title("ABCA ") xlabel(-3(1)4)) name(g1)
              option name() not allowed
              Could you please tell me how to sort it out? Warm regards.

              Comment


              • #8
                Originally posted by Jared Greathouse View Post
                Yes, the "name" option is the relevant one here. Me personally, I would also break up your command with three slashes. When I have long code for graphs, I'll usually do something like

                Code:
                scatter y x, mcol(black) || /// plotted data
                line y x, lcol(red), // fitted data
                ti() /// title
                xlab()
                /* ... and so on so you're organized
                about what option does what */
                Thanks Jared Greathouse, I follow your suggestion and chop the code to three roiws but the error still exists, could you please help me to have a look then? Thanks in advance

                Code:
                event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///
                    title("ABCDA ") ///
                    xlabel(-3(1)4)) name(g1)
                option name() not allowed

                Comment


                • #9
                  event_plot is from SSC and the author doesn't appear to be a member here. I have not used it, but the likely answer is documented in the help


                  graph_opt(string): additional twoway options for the graph overall (e.g. title, xlabel).
                  I would try therefore

                  Code:
                  graph_opt(name(g1))
                  and you may need to put some other options within the same overall option. If that doesn't work, you may need to contact the author directly.

                  Comment


                  • #10
                    Another suggestion I'd make (I know because I've had to do the same thing) is they may have
                    Code:
                    graph close all
                    at the top of their program syntax. If so, you'll just need to go in and delete that part, but I'm not at my laptop right now, but I suspect that's the issue.

                    Comment

                    Working...
                    X