Announcement

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

  • Drawing a 2 Y-axis graph with an X variable in string format

    Hello Stata community;

    I dispose of this data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 date float(unemployment employment)
    "Q1 2008"   5  71.50778
    "Q2 2008" 5.3  71.25295
    "Q3 2008"   6  70.73177
    "Q4 2008" 6.9  69.96442
    "Q1 2009" 8.3  68.65476
    "Q2 2009" 9.3   67.8991
    "Q3 2009" 9.6 67.302895
    "Q4 2009" 9.9   66.5618
    "Q1 2010" 9.8  66.73094
    "Q2 2010" 9.6  66.79226
    "Q3 2010" 9.5 66.724525
    "Q4 2010" 9.5  66.44049
    "Q1 2011"   9 66.655556
    "Q2 2011" 9.1  66.49724
    "Q3 2011"   9  66.58442
    "Q4 2011" 8.6 66.779106
    "Q1 2012" 8.3  66.96114
    end
    My goal is to draw a 2 Y axish graph (my two Y variables are unemployment & employment) using the X variable date. The problem is that this date variable is in string format.
    I've tried this code: graph twoway (scatter unemployment date, connect(direct) msymbol(circle) msize(large) mcolor(green) yaxis(1) ytitle("Unemployment", axis(1))) (scatter employment date, connect(direct) msymbol(diamond) msize(small) mcolor(black) yaxis(2) ytitle("Employment", axis(2))), but I guess it didn't work out because the date variable is in string format.

    Should I change the date variable format? If so, how? Or, is there another way to get to my goal?

    Thanks for your help.

  • #2
    Your date is a string variable; but that is not a format in Stata terms. Either way, dates are documented in detail at

    Code:
    help datetime
    which should be consulted here. Once you have a numeric date variable, you have a choice of graphical representation. I recommend multiline from SSC over any abomination with two y axes, hard to get right and all too easy to make cryptic or confusing.


    More at https://www.statalist.org/forums/for...ailable-on-ssc

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 date float(unemployment employment)
    "Q1 2008"   5  71.50778
    "Q2 2008" 5.3  71.25295
    "Q3 2008"   6  70.73177
    "Q4 2008" 6.9  69.96442
    "Q1 2009" 8.3  68.65476
    "Q2 2009" 9.3   67.8991
    "Q3 2009" 9.6 67.302895
    "Q4 2009" 9.9   66.5618
    "Q1 2010" 9.8  66.73094
    "Q2 2010" 9.6  66.79226
    "Q3 2010" 9.5 66.724525
    "Q4 2010" 9.5  66.44049
    "Q1 2011"   9 66.655556
    "Q2 2011" 9.1  66.49724
    "Q3 2011"   9  66.58442
    "Q4 2011" 8.6 66.779106
    "Q1 2012" 8.3  66.96114
    end
    
    gen qdate = quarterly(substr(date, 2, .), "QY")
    * ssc install multiline
    
    set scheme s1color
    multiline u e qdate, xla(192(4)208, grid glc(gs12) format(%tq)) yla(#6) recast(connected) xtitle("")

    Comment


    • #3
      The graph doesn't show up well in what I can see. Here it is again.
      Click image for larger version

Name:	employunemplay.png
Views:	1
Size:	30.1 KB
ID:	1668472

      Comment


      • #4
        Thanks very much for the help, I appreciate it.
        Yet, my goal is to have the two lines on the same graph actually, because I think it's better for comparative and interpretation reasons.

        Comment


        • #5
          Well, in that case I solved about half of your problem. Good luck with the other half.

          Wainer, H. 1991. Double Y-axis graphs. Chance 4(1): 50-51 is pertinent.

          Comment


          • #6
            Thanks very much

            Comment

            Working...
            X