Announcement

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

  • nodraw option not working

    Dear Stata Users,

    I want to draw a graph but not display it. From the manual, I read you can use the option nodraw together with saving() option to achieve this. However, the graph is still displayed. Are there caveats against the use of nodraw option?

    As a work around, I set graphics off then draw the graph with gr tw ... afterwards set graphics on and use graph use to display the graph. The problem is when I set graphics off, all my open graph windows disappear. Is this normal? If so, how can I keep these open graphs open?

    Thanks.
    Victoria

  • #2
    Welcome to the Stata Forum/ Statalist.

    I recommend to share command and output. This is the best way to create an informative post.

    I’m confident you will get a helpful reply, provided the FAQ advices are followed.
    Best regards,

    Marcos

    Comment


    • #3
      In line with Marcos suggestion, here are more details to my problems.

      First, I begin with the snippet of the data

      rid se const1 txt
      1 1 1 .[., .]
      2 1 1 0.600[0.262, 0.878]
      3 1 1 0.833[0.516, 0.979]
      4 1 1 0.931[0.772, 0.992]
      5 1 1 0.885[0.698, 0.976]
      6 1 1 0.800[0.284, 0.995]
      7 1 1 0.849[0.747, 0.914]
      8 1 1 .[., .]
      9 1 1 .[., .]
      10 1 1 0.600[0.262, 0.878]
      11 1 1 0.667[0.349, 0.901]
      12 1 1 0.741[0.537, 0.889]
      13 1 1 0.769[0.564, 0.910]
      14 1 1 0.667[0.223, 0.957]
      15 1 1 0.708[0.592, 0.803]
      16 1 1 .[., .]
      17 1 1 0.779[0.699, 0.842]



      I have a program doing regression and plotting.


      This is the main program

      cap program drop mainprog
      program define mainprog, eclass
      version 14.1
      ....
      qui splotprog // Embed the program to plot s //<================================================= ==================First plot

      qui fplotprog // Embed the program to plot f //<================================================= ==================Series of plots that are combined into 1

      ...

      end


      This is the plotting program

      cap program drop fplotprog
      program define fplotprog, rclass
      version 14.1

      .....

      #delimit ;
      gr tw
      (pci 1 1 1 1,
      xaxis(1 2)
      vertical
      lcolor(none))
      (scatter rid const1,
      xaxis(1 2)
      mlabcolor(black)
      msymbol(none)
      mlabel(txt)
      mlabpo(0))
      ,
      graphregion(margin(r=0 l=0))
      yscale(reverse off noextend axis(1))
      ylabel(none, nogextend nogrid)
      xscale(off fill axis(1))
      xscale(noline axis(2))
      xlabel(none, tlcolor(none) axis(2))
      xtitle(" ", axis(2))
      legend(off)
      fxsize(25)
      saving(setext1, replace)
      nodraw //<================================================= ==================
      ;
      #delimit cr


      /*Combine the graphs*/
      #delimit ;
      graph combine setext1.gph setext1.gph setext1.gph //Just for demo
      ,
      rows(1)
      imargin(0 0 0)
      name(fplot_gname, replace)
      saving(fplot_gname, replace)
      ;
      #delimit cr

      ....

      end


      Finally I run my program

      mainprog varlist

      The graphs setext1 is save and displayed. Sorry, I am not able to upload or paste the graph. While the plot has what I expect, I do not want it displayed. Why is it being displayed?

      Not so display the graph, I change the code in the plotting program as follows

      cap program drop fplotprog
      program define fplotprog, rclass
      version 14.1

      .....
      set graphics off//<================================================= ===added
      #delimit ;
      gr tw
      (pci 1 1 1 1,
      xaxis(1 2)
      vertical
      lcolor(none))
      (scatter rid const1,
      xaxis(1 2)
      mlabcolor(black)
      msymbol(none)
      mlabel(txt)
      mlabpo(0))
      ,
      graphregion(margin(r=0 l=0))
      yscale(reverse off noextend axis(1))
      ylabel(none, nogextend nogrid)
      xscale(off fill axis(1))
      xscale(noline axis(2))
      xlabel(none, tlcolor(none) axis(2))
      xtitle(" ", axis(2))
      legend(off)
      fxsize(25)
      saving(setext1, replace)
      nodraw
      ;
      #delimit cr

      set graphics on //<================================================= ===added
      /*Combine the graphs*/
      #delimit ;
      graph combine setext1.gph setext1.gph setext1.gph //Just for demo
      ,
      rows(1)
      imargin(0 0 0)
      name(fplot_gname, replace)
      saving(fplot_gname, replace)
      ;
      #delimit cr

      ....

      end


      When I do this, the graph setext1 is not diplayed as I desired. However the earlier graph plotted by the first qui splotprog disappears and only the last graph plotted after graph combine is open.

      What I desire is to to draw a graph, save it and not display it. How can I achieve this?

      I hope the code sheds more light into my predicament.

      Thanks,
      Victoria

      Comment


      • #4
        Thanks for supplying code. Please note our explicit request to use CODE delimiters and data as dataex (SSC) output. Your example requires some minor engineering work, as below.

        It seemed to me that this is the core of what you are doing Clearly I can do nothing about code ... you omit, which you're implying is irrelevant. Perhaps so, perhaps not.

        Code:
        clear
        input rid se const1 str42 txt
        1 1 1 ".[., .]"
        2 1 1 "0.600[0.262, 0.878]"
        3 1 1 "0.833[0.516, 0.979]"
        4 1 1 "0.931[0.772, 0.992]"
        5 1 1 "0.885[0.698, 0.976]"
        6 1 1 "0.800[0.284, 0.995]"
        7 1 1 "0.849[0.747, 0.914]"
        8 1 1 ".[., .]"
        9 1 1 ".[., .]"
        10 1 1 "0.600[0.262, 0.878]"
        11 1 1 "0.667[0.349, 0.901]"
        12 1 1 "0.741[0.537, 0.889]"
        13 1 1 "0.769[0.564, 0.910]"
        14 1 1 "0.667[0.223, 0.957]"
        15 1 1 "0.708[0.592, 0.803]"
        16 1 1 ".[., .]"
        17 1 1 "0.779[0.699, 0.842]"
        end
        
        twoway pci 1 1 1 1, xaxis(1 2) vertical lcolor(none) || ///
        scatter rid const1, xaxis(1 2) mlabcolor(black) msymbol(none) mlabel(txt) mlabpo(0) ///
        graphregion(margin(r=0 l=0)) yscale(reverse off noextend axis(1)) ///
        ylabel(none, nogextend nogrid) xscale(off fill axis(1)) ///
        xscale(noline axis(2)) xlabel(none, tlcolor(none) axis(2)) ///
        xtitle(" ", axis(2)) legend(off) fxsize(25) saving(setext1, replace) nodraw
        
        graph combine setext1.gph setext1.gph setext1.gph ///
        , rows(1) imargin(0 0 0) name(fplot_gname, replace) saving(fplot_gname, replace)
        The code runs fine for me. The graph wasn't drawn, as instructed, but the file was saved and graph combine worked fine.

        I don't see any need here to wrap highly specific code up within declared programs, but that's partly a matter of taste and programming correctness. But more crucially, I can't see where you

        Code:
        set graphics off 
        The key here is that to understand your problem, it helps mightily if we can reproduce it; and I failed.

        Comment


        • #5
          Thanks Nick for the quick response. To make things more clear, I attach the dataset I am using to test my code <<ascus.csv>>. I also attach the full code of the program that I am building, <<metadta1.do>>, <<metadta2.do>> and <<metadta3.do>>

          To test the my metadta program, I am run the following commands

          option 1
          /*use nodraw option*/


          run metadta1.do

          /* Relevant line in the do file are:

          116-129

          444-602 ***graph with nodraw option

          */

          import delimited using "ascus.csv", clear

          cap log close
          cap log using "ascus.txt", text replace

          set more off
          metadta tp tn fp fn test, sid(studyid) bubblewt bubbleid noelli keep
          cap log close


          When I execute metadta, a series of graphs are displayed even the ones with nodraw option. I do not want to have these graphs displayed.

          option 2
          /*set graphics on and off*/


          run metadta2.do

          /* Relevant line in the do file are:

          116-129

          at line 446, I set graphics off

          at line 613 I set the graphics on

          */

          import delimited using "ascus.csv", clear

          cap log close
          cap log using "ascus.txt", text replace

          set more off
          metadta tp tn fp fn test, sid(studyid) bubblewt bubbleid noelli keep
          cap log close



          Running this code above draws the first graph which disappears afterwards. The second graph is drawn and left open once the program concludes. I want both graphs to remain open.


          option 3
          /*set graphics on and off, with graph use*/


          Here, I decide not to display the graphs but save them and display at a later point.

          run metadta3.do

          /* Relevant line in the do file are:

          116-129

          154, 171

          */

          import delimited using "ascus.csv", clear

          cap log close
          cap log using "ascus.txt", text replace

          set more off
          metadta tp tn fp fn test, sid(studyid) bubblewt bubbleid noelli keep
          cap log close


          After the regression output, I get the error message when the using graph use as follows

          Drawing forestplot(s)
          file not a Stata .gph file
          r(610);


          The graph itself is a stata file and it is opening in a new stata window with the graph use command.

          Is my problem reproducible?

          Thanks
          Victoria




          Attached Files

          Comment


          • #6
            Thanks on behalf of the list here. Sorry, but this is getting too complicated for me to be tempted to study one data file and three do-files to see what is happening. If you don't get a better answer from someone else, advice given elsewhere to supply a minimal, complete, verifiable example is pertinent. All the words are important but I stress "minimal". https://stackoverflow.com/help/mcve

            You're ignoring the request to use CODE delimiters. They really are a good idea.

            Comment


            • #7
              Apologies for making it even more complex.

              Once more here is the data

              rid studyid const1
              2 Andersson 2005 1
              3 Bergeron 2000 1
              4 Del Mistro 2010 1
              5 Kulasingam 2002 1
              6 Lytwyn 2000 1
              7 Manos 1999 1
              8 Monsonego 2008 1
              9 Morin 2001 1
              10 Silverloo 2009 1
              11 Solomon 2001 1
              15 Andersson 2005 1
              16 Bergeron 2000 1
              17 Del Mistro 2010 1
              18 Kulasingam 2002 1
              19 Lytwyn 2000 1
              20 Manos 1999 1
              21 Monsonego 2008 1
              22 Morin 2001 1
              23 Silverloo 2009 1
              24 Solomon 2001 1
              1 HC2 1
              12 Subgroup 1
              13 1
              14 RepC 1
              25 Subgroup 1
              26 1
              27 Overall 1


              Here is the code

              gr tw ///
              (pci 1 1 1 1, ///
              xaxis(1 2) ///
              vertical ///
              lcolor(none)) ///
              (scatter rid const1 if se , ///
              xaxis(1 2) ///
              mlabcolor(black) ///
              msymbol(none) ///
              mlabel(studyid) ///
              mlabposition(3) ///
              mlabgap(*0)) ///
              , ///
              graphregion(margin(r=0 l=0)) ///
              yscale(reverse off noextend axis(1)) ///
              ylabel(none, nogextend grid) ///
              xlabel(1(1)2, nogextend labc(bg) tlcolor(none) axis(1)) ///
              xscale(noline axis(1)) ///
              xscale(noline axis(2)) ///
              xlabel(none, tlcolor(none) axis(2)) ///
              xtitle("Study", axis(2)) ///
              legend(off) ///
              fxsize(40) ///
              nodraw ///
              saving(sid, replace)


              Output:
              (file sid.gph saved)

              and the attached graph is displayed. I do not want it displayed.

              Attached Files

              Comment


              • #8
                When I run the following, which I think is essentially identical to what you show in post #7, the graph is not displayed.
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input byte rid str10 studyid int const1 byte se
                 2 "Andersson"  2005 1
                 3 "Bergeron"   2000 1
                 4 "DelMistro"  2010 1
                 5 "Kulasingam" 2002 1
                 6 "Lytwyn"     2000 1
                 7 "Manos"      1999 1
                 8 "Monsonego"  2008 1
                 9 "Morin"      2001 1
                10 "Silverloo"  2009 1
                11 "Solomon"    2001 1
                15 "Andersson"  2005 1
                16 "Bergeron"   2000 1
                17 "DelMistro"  2010 1
                18 "Kulasingam" 2002 1
                19 "Lytwyn"     2000 1
                20 "Manos"      1999 1
                21 "Monsonego"  2008 1
                22 "Morin"      2001 1
                23 "Silverloo"  2009 1
                24 "Solomon"    2001 1
                 1 "HC2"           1 .
                12 "Subgroup"      1 .
                13 "1"             . .
                14 "RepC"          1 .
                25 "Subgroup"      1 .
                26 "1"             . .
                27 "Overall"       1 .
                end
                
                gr tw ///
                (pci 1 1 1 1, ///
                xaxis(1 2) ///
                vertical ///
                lcolor(none)) ///
                (scatter rid const1 if se , ///
                xaxis(1 2) ///
                mlabcolor(black) ///
                msymbol(none) ///
                mlabel(studyid) ///
                mlabposition(3) ///
                mlabgap(*0)) ///
                , ///
                graphregion(margin(r=0 l=0)) ///
                yscale(reverse off noextend axis(1)) /// 
                ylabel(none, nogextend grid) ///
                xlabel(1(1)2, nogextend labc(bg) tlcolor(none) axis(1)) ///
                xscale(noline axis(1)) ///
                xscale(noline axis(2)) ///
                xlabel(none, tlcolor(none) axis(2)) ///
                xtitle("Study", axis(2)) ///
                legend(off) ///
                fxsize(40) ///
                nodraw ///
                saving(sid, replace) 
                
                about
                Code:
                . gr tw ///
                > (pci 1 1 1 1, ///
                > xaxis(1 2) ///
                > vertical ///
                > lcolor(none)) ///
                > (scatter rid const1 if se , ///
                > xaxis(1 2) ///
                > mlabcolor(black) ///
                > msymbol(none) ///
                > mlabel(studyid) ///
                > mlabposition(3) ///
                > mlabgap(*0)) ///
                > , ///
                > graphregion(margin(r=0 l=0)) ///
                > yscale(reverse off noextend axis(1)) /// 
                > ylabel(none, nogextend grid) ///
                > xlabel(1(1)2, nogextend labc(bg) tlcolor(none) axis(1)) ///
                > xscale(noline axis(1)) ///
                > xscale(noline axis(2)) ///
                > xlabel(none, tlcolor(none) axis(2)) ///
                > xtitle("Study", axis(2)) ///
                > legend(off) ///
                > fxsize(40) ///
                > nodraw ///
                > saving(sid, replace) 
                (file sid.gph saved)
                
                . 
                . about
                
                Stata/SE 15.0 for Mac (64-bit Intel)
                Revision 25 Sep 2017
                Copyright 1985-2017 StataCorp LLC
                
                Total physical memory:    8.01 GB

                Comment


                • #9
                  Hi Victoria,

                  By downloading and running your files in #5, I can certainly replicate your issue with "nodraw". By simplifying the code bit-by-bit, it looks to me as if it's something to do with the ROC subroutine. If you type:
                  Code:
                  import delimited using "ascus.csv", clear
                  metadta tp tn fp fn test, sid(studyid) bubblewt bubbleid noelli keep nosroc
                  ...then no graphs are displayed apart from the final forest plot. (I'm running Stata 14.2)

                  I'm now a bit stumped, though, as I can't see what "sroc" (and its subroutines) can be doing that effectively forces subsequent graphs to be drawn.

                  On a different subject, I notice that you are using extracts of code from metan.ado to produce your forestplots, presumably since your models have some idiosyncrasies that you wish to control yourself. You may or may not be aware that I have written a program called forestplot.ado which enables you to plot forest plots from a user-created dataset, rather than through metan itself. I wonder if you might find it useful. You can find it within the ipdmetan package via SSC.

                  Thanks,

                  David.

                  Comment


                  • #10
                    Thanks David for your response and time.

                    I have looked at your ado, saw its for ipd but also includes ad data. I will try it out.

                    Yes, the gist in metadta is like metan but decided to code from scratch. I used metan as a template for metaprop and metaprop_one.Anyway,

                    I have tried the options that you suggest the first time the graphs appear and then disappear.

                    I do not see anything wrong in "sroc" that is triggering this undesired behaviour.

                    Regards and thanks,
                    Victoria

                    Comment


                    • #11
                      William, I guess the problem is then personalised.

                      I still have the graph in #8 displayed.

                      I am using stata 14.2

                      Stata/SE 14.2 for Windows (64-bit x86-64)
                      Revision 10 Oct 2017
                      Copyright 1985-2015 StataCorp LLC

                      Total physical memory: 8274392 KB
                      Available physical memory: 4369808 KB


                      Thanks for your time

                      Comment


                      • #12
                        To the curious ones:

                        I set graphics off, drew the required graphs, then set graphics on and used graph combine to construct the forestplots. Afterwards, I draw the sroc curve. This way both plots remain open after the call the code executes and the other graphs are not displayed. Not ideal solution though

                        Comment

                        Working...
                        X