Announcement

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

  • addplot option with xtline

    I have panel data, such as the following:
    Code:
    clear
    input float(year panelid yvar yvar2)
    1999 1 3 2
    2000 1 5 2
    2001 1 6 3
    2002 1 5 3
    1999 2 4 8
    2000 2 8 8
    2001 2 9 8
    2002 2 9 8
    1999 3 2 9
    2000 3 1 9
    2001 3 3 7
    2002 3 4 6
    end
    I use xtline to produce graphs of selected variables over time. I only include one panel using the following syntax:
    xtline yvar if panelid==1
    This works fine. I want to add another variable on a different scale, so with a different Y-axis. I add the addplot options below:
    xtline yvar if panelid==1, addplot(tsline yvar2 if panelid==1, yaxis(2))
    Now Stata draws an xtline graph for each panel but only plots data for the first panelid, and I only want one graph of one panel.

    What am I missing that will keep Stata from drawing the other (plotless) graphs?

  • #2
    Please provide a reproducible example with all required commands. Thank you.
    Code:
    . xtline yvar if panelid==1
    must specify panelvar; use xtset
    r(459);

    Comment


    • #3
      Add the following just before xtline:
      xtset panelid year

      Comment


      • #4
        Is this what you want,
        Code:
        twoway (line yvar year if panelid == 1) (line yvar2 year if panelid == 1, yaxis(2))

        Comment


        • #5
          Eric's solution would work if I only wanted one panel, but I want to keep the structure of xtline, meaning multiple graphs for each panel in the same output.

          Comment


          • #6
            Code:
            twoway (line yvar year if panelid == 1) (line yvar2 year if panelid == 1, yaxis(2)), by(panelid)

            Comment


            • #7
              Eric's last attempt reproduces the original problem, as the by option overrides the if options and draws a graph for each and every panelid.

              Comment


              • #8
                Then I don't understand what you want.

                Comment


                • #9
                  I'm looking to draw an xtline graph including multiple selected panels, each including another variable drawn with a second Y-axis. In other words, I want a result between your first attempt, which only allows one panel, and your second attempt, that includes every panel. In truth, my data include over 150 panels, not just three as given in the example, but say I wanted to use the example data above and draw a graph including panels 1 and 2, but not 3, or in reality just a handful out of 150+ panels.

                  Drawing the xtline graph by itself is fine; the if option keeps the number of graphs drawn to the selected few. Including addplots or using your second attempt blows up (for lack of a better term) the if options—even if they are included in both commands—and draws a graph for each and every panel.

                  Is this a bug that cannot take the if option into account along with the addplots option/combining two twoway graphs (whichever method you prefer, but I'd prefer the first), or am I missing an additional setting that applies to both the original command and the option?

                  Comment


                  • #10
                    I'm just commenting to re-up this post, in the hopes of getting some help.

                    Comment


                    • #11
                      Back at #2 Friedrich asked for a reproducible example with all required commands. So please bring together one such, as few people want to read 9 previous postings to work out where the question lies.

                      Posting the graphs themselves would be enormously helpful too. Long verbal descriptions are highly inefficient.

                      Comment


                      • #12
                        In other words, I want a result between your first attempt, which only allows one panel, and your second attempt, that includes every panel. In truth, my data include over 150 panels, not just three as given in the example, [...] or in reality just a handful out of 150+ panels.
                        It seems to me that Eric show you exactly the way to go in #6. If you just want a handful out of 150+ panels you can adjust the by variable to meet your criteria.
                        ...but say I wanted to use the example data above and draw a graph including panels 1 and 2, but not 3,
                        Is this what you want?
                        Code:
                         g panelid2=panelid if panelid<3
                        twoway (line yvar year) (line yvar2 year, yaxis(2)), by(panelid2)

                        Comment

                        Working...
                        X