Announcement

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

  • Add a new graph type to "graph twoway"

    I wrote a simple program called tsline2 that plots a tsline and formats the graph in a certain way (white background, yline at zero etc). The program works ok. However, I cant combine in with other built-in graphs via "twoway" command.

    twoway (tsline2 y) scatter (z time)

    gives an error: r(198) tsline2 is not a twoway plot type.

    tsline2 y works just fine.

    Is there any way to integrate the tsline2 command into the twoway structure?

  • #2
    If the program is basically a wrapper around existing twoway commands, you can either add passthru options (e.g., if you want to allow specific options it will pass the macro exact at it appears in your syntax) or you can add a string option with the "string asis" type and the macro will expand exactly as the user entered it. I'd personally shy away from programming the aesthetics directly into the graph (e.g., setting the background color) since users may want to alter things a bit and it reduces the places where your code could break down. In terms of examples, I'd say the absolute best place to look would be at the source code for some of Nick Cox's programs. Depending which program of his you are looking at, you'll likely see different implementations of both cases so it would also give you a lot of good ways to think about when/why one approach should be used vs another.

    Comment


    • #3
      Thanks to Billy for his generous commendation. The easiest way for you to work may well be to write your own program to allow an addplot() option. But this is speculation in so far as we cannot see your code.

      Comment


      • #4
        Thank you for your replies. After I added the addplot() option, the problem was fixed. The minimal working example of code is below:

        Code:
        capture program drop tsline2
        program define tsline2
        syntax anything [if], [NOzero addplot(str asis) *]
        
        qui unab varlist: `anything'
        foreach var of local varlist {
            local n_list n `n_list'
        }
        
        capture qui macro list _nozero
        if _rc != 0 local zero_line "yline(0, lpattern(dash) lcolor(black))"
        
        tsline `anything' `if', cmissing(`n_list') graphregion(color(white)) legend(region(lwidth(none))) `zero_line' `options' || `addplot' `if'
        
        end
        However, the addplot() option does not entirely address my issue. My intention was to get tsline2 to work with graph twoway. Any ideas how I could get STATA to treat tsline2 as a graph twoway function?

        Comment


        • #5
          Sergiy Radyakin has a presentation from 2009 that explains how Stata goes about determining whether a given graph an acceptable twoway graph and how to create custom graph commands that will be allowed by twoway: (http://www.stata.com/meeting/dcconf09/dc09_radyakin.pdf). I haven't tried it, so I don't know if everything is still valid for more recent versions of Stata. The relevant discussion begins at the bottom of slide 11.
          Stata/MP 14.1 (64-bit x86-64)
          Revision 19 May 2016
          Win 8.1

          Comment


          • #6
            Please tell me what to do? I was trying to run a time series line graph by using tsset & tsline command in stata 15, but every time r(198) error is showing up. its saying twoway is not a valid subcommand.
            Please help me to run the graph. the graph is not working.

            Comment


            • #7
              Sayantani Ghosh In order to debug your code, we need to see it please. Copy and paste the full command that failed into a reply.

              Comment

              Working...
              X