Announcement

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

  • How to use dual Y axis in panel data display

    I have a panel data, 10 regions (name), 1-12 months (months), and two other statistical variables



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long name float months int var1 double var2
    1 720 107 7.49
    1 721  62  4.5
    1 722  42 4.12
    1 723  41 4.54
    1 724  38 4.27
    1 725  38 4.53
    1 726  45 4.07
    1 727  38 3.92
    1 728  31 3.71
    1 729  60 5.45
    1 730  50 4.65
    1 731  51 4.88
    2 720 105 7.31
    2 721  60 4.49
    2 722  42 4.12
    2 723  41 4.71
    2 724  35 4.35
    2 725  39 4.58
    2 726  43 4.11
    2 727  35 3.91
    2 728  26  3.7
    2 729  57 5.45
    2 730  48 4.71
    2 731  52 5.03
    3 720 104 7.33
    3 721  65 4.73
    3 722  45  4.2
    3 723  42 4.57
    3 724  36 4.38
    3 725  39 4.55
    3 726  46 4.22
    3 727  38 4.08
    3 728  33 3.94
    3 729  58 5.49
    3 730  48 4.71
    3 731  53  5.1
    4 720 108 7.49
    4 721  60 4.38
    4 722  42 3.94
    4 723  39 4.31
    4 724  36 4.13
    4 725  34 4.31
    4 726  42    4
    4 727  35 3.64
    4 728  28 3.65
    4 729  54 4.97
    4 730  45 4.32
    4 731  56 4.95
    5 720 105 7.63
    5 721  61 4.64
    5 722  41 4.13
    5 723  33 4.32
    5 724  35  4.4
    5 725  33 4.57
    5 726  37 4.13
    5 727  28 3.76
    5 728  27 3.87
    5 729  54 5.28
    5 730  45 4.76
    5 731  50 5.06
    6 720 109 7.66
    6 721  61 4.53
    6 722  44 4.09
    6 723  39 4.49
    6 724  37 4.34
    6 725  37 4.48
    6 726  42 4.06
    6 727  37 3.94
    6 728  31 3.85
    6 729  56 5.25
    6 730  45  4.5
    6 731  52 5.12
    7 720 100  7.5
    7 721  62 4.59
    7 722  44 4.33
    7 723  40 4.84
    7 724  34 4.48
    7 725  35 4.79
    7 726  38 4.13
    7 727  28 3.93
    7 728  25 3.97
    7 729  53 5.53
    7 730  49 4.75
    7 731  56 5.22
    8 720 119 8.03
    8 721  62 4.54
    8 722  45 4.34
    8 723  43 4.66
    8 724  38  4.5
    8 725  40 4.72
    8 726  48 4.31
    8 727  36  3.9
    8 728  32 3.85
    8 729  59 5.37
    8 730  49 4.68
    8 731  59 5.31
    9 720  95 7.14
    9 721  61 4.61
    9 722  43  4.4
    9 723  40 4.86
    end
    format %tm months
    label values name lb2
    label def lb2 1 "a", modify
    label def lb2 2 "b", modify
    label def lb2 3 "c", modify
    label def lb2 4 "d", modify
    label def lb2 5 "e", modify
    label def lb2 6 "f", modify
    label def lb2 7 "g", modify
    label def lb2 8 "h", modify
    label def lb2 9 "i", modify
    However, the data of the two variables are quite different. If the same Y-axis is used, the smaller change trend will not be obvious. Can you use the xtline command to generate two Y-axis,

    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	69.4 KB
ID:	1602208

    just like as follows:
    twoway (line var1 months,sort yaxis(1)) (line var2 months,sort yaxis(2)

    Thank you for your help, thank you very much

  • #2
    Try:

    Code:
    tw (line var1 months)(line var2 months, yaxis(2)), by(name)

    Comment


    • #3
      thank you very much, The problem is solved satisfactorily.

      I would like to add one more question. Is the xtline command unable to complete the drawing of this graph? thank you

      Comment


      • #4
        Click image for larger version

Name:	line.jpg
Views:	1
Size:	81.8 KB
ID:	1602231


        Y axis title is incorrect, maybe I can modify it

        Comment


        • #5
          As xtline is a twoway-type graph, it can. You can add additional series using the -addplot()- option, but using the existing time variable may cause problems as the main series and additional series may not be on the same scale. You can overcome this by creating a new time variable.

          Code:
          egen time= group(months)
          xtset name time
          xtline var1, addplot(line var2 time, yaxis(2))
          The remainder is just a labeling issue. For the titles #4

          Code:
          tw (line var1 months, ytitle("Whatever"))(line var2 months, yaxis(2)), by(name, r1title("Whatever2", size(medsmall)))
          Last edited by Andrew Musau; 07 Apr 2021, 17:33.

          Comment


          • #6
            Thank you very much , That's great! it's very kind of you!

            Comment


            • #7
              @Andrew: I had neve seen the r1title option before, though I could have used it several times. So I looked it up in the Graphics manual. Interestingly the manual says:
              { t | b | l | r } {1 | 2} title() options are rarely specified. It is generally better to specify the axis_title_options ytitle() or xtitle(). [They] are included for backward compatibility with previous versions of State.
              Yet I would not know how to produce the same result with ytitle .

              Comment


              • #8
                On a different level I note the longstanding economists' trick of putting things on the same scale by standardising so that values of different variables on some reference date are say 100.

                ###

                On #7 there is a silly and very quick game at users' meetings called version, which is just "which version of Stata did you start with?" and naturally the lowest number called out wins. I guess of the really active members here Rich Goldstein may win outright as he (claims that he) remembers starting with Stata 1.0 but we need the archaeological evidence of his installation media. I can't beat that, which is why I am jealous.

                A little more seriously these extra title options have occasional key uses as with the idiosyncratic graph bar | hbar | dot which deny all knowledge of an x axis. I document in the help of catplot (SSC) use of an appropriate title option for stuff on the left and bottom of a display.

                Comment


                • #9
                  Eric de Souza I do not know if I should call it a bug, but this is what should work but doesn't:

                  Code:
                  tw (line var1 months, ytitle("Whatever"))(line var2 months, yaxis(2) ytitle("Whatever 2", axis(2))), by(name)
                  It is legal syntax as it does not throw out an error, and one can can also write the following for the left-hand ytitle:

                  Code:
                  tw (line var1 months, ytitle("Whatever", axis(1)))(line var2 months, yaxis(2)), by(name)
                  So using -r1title- is just a workaround. In some cases, I need to switch off all the defaults if they are not doing what I want and use the -r- -l- -t- -b- titles

                  Code:
                  help title_options

                  Comment


                  • #10
                    Nick Cox 's claim in #8 is not quite correct (actually, he may be correct in that I may have claimed that but it was not accurate of me to do so) - in the 1980's an 1990's I did a lot of software reviewing (and was the Associate Editor in charge of software at The American Statistician from about 1990 to about 1997; somewhere during the early part of that time Bill Gould (StataCorp) sent me some version (1.0? 1.3?) of Stata - after a quick look I sent it back! Not too long after that I did become an actual user but I do not remember what version that was (I think 2.x but ...)

                    Comment


                    • #11
                      As others have written, history is not what happened but what you can remember.

                      Naturally all competitions of this kind, real or imagined, are won by Bill Gould (StataCorp) who started the whole thing.

                      Mead Over is another very long-term user who may have started on 1.something.

                      Comment


                      • #12
                        @Nick Cox , Rich Goldstein. Wow are doing history here? Cool. I started with version 2.0 in 1998 on an IBM AT with a Hercules graphics card, and for fun I loaded "small Stata" and ran it on my HP 95 MS-DOS calculator. I wonder if anyone else on the list ever ran Stata on an HP 95.

                        Comment


                        • #13
                          Sorry, I am now thinking I should have started a new thread before anyone makes that comment, which would be fair comment.

                          Any way, I can now reveal 2.05 as my own initiation.

                          Comment

                          Working...
                          X