Announcement

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

  • A correction of a code that creates a graph that is offset from the X-axis of the years

    Hello STATA people,

    I have this data at hand:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 Month float(Unemployment_Rate Inflation_Rate)
    "2000-01"  9.7  .3
    "2000-02"  9.7  .4
    "2000-03"  9.7  .4
    "2000-04"  9.6  .2
    "2000-05"  9.6  .2
    "2000-06"  9.5  .4
    "2000-07"  9.5  .2
    "2000-08"  9.4  .1
    "2000-09"  9.4  .6
    "2000-10"  9.3  .2
    "2000-11"  9.3  .3
    "2000-12"  9.3  .4
    "2001-01"  9.3 -.2
    "2001-02"  9.2  .4
    "2001-03"  9.2  .6
    "2001-04"  9.3  .7
    "2001-05"  9.3  .6
    "2001-06"  9.3  .2
    "2001-07"  9.3 -.2
    "2001-08"  9.3   0
    "2001-09"  9.3  .3
    "2001-10"  9.4  .2
    "2001-11"  9.4   0
    "2001-12"  9.5  .5
    "2002-01"  9.5  .2
    "2002-02"  9.5  .2
    "2002-03"  9.5  .5
    "2002-04"  9.6  .5
    "2002-05"  9.6  .2
    "2002-06"  9.6 -.1
    "2002-07"  9.7 -.1
    "2002-08"  9.7  .1
    "2002-09"  9.8  .3
    "2002-10"  9.7  .3
    "2002-11"  9.8   0
    "2002-12"  9.8  .5
    "2003-01"  9.8  .1
    "2003-02"  9.8  .4
    "2003-03"  9.8  .6
    "2003-04"  9.8  .2
    "2003-05"  9.8 -.1
    "2003-06"  9.9  .1
    "2003-07"  9.9 -.1
    "2003-08"  9.9  .1
    "2003-09"  9.9  .4
    "2003-10"  9.9  .2
    "2003-11"  9.9  .1
    "2003-12"   10  .3
    "2004-01" 10.1 -.1
    "2004-02"   10  .2
    "2004-03" 10.1  .6
    "2004-04"   10  .5
    "2004-05"   10  .4
    "2004-06"   10  .1
    "2004-07"  9.9 -.2
    "2004-08"  9.9  .1
    "2004-09"   10  .1
    "2004-10"   10  .4
    "2004-11"   10 -.1
    "2004-12"  9.9  .3
    "2005-01"  9.8 -.5
    "2005-02"  9.8  .4
    "2005-03"  9.8  .7
    "2005-04"  9.8  .5
    "2005-05"  9.8  .2
    "2005-06"  9.8  .1
    "2005-07"  9.7 -.1
    "2005-08"  9.6  .2
    "2005-09"  9.6  .5
    "2005-10"  9.6  .3
    "2005-11"  9.5 -.2
    "2005-12"  9.4  .3
    "2006-01"  9.3 -.3
    "2006-02"  9.2  .3
    "2006-03"  9.1  .5
    "2006-04"    9  .7
    "2006-05"  8.9  .3
    "2006-06"  8.7  .1
    "2006-07"  8.7 -.1
    "2006-08"  8.6  .1
    "2006-09"  8.5   0
    "2006-10"  8.4  .1
    "2006-11"  8.3  .1
    "2006-12"  8.2  .3
    "2007-01"    8 -.4
    "2007-02"  7.9  .3
    "2007-03"  7.8  .6
    "2007-04"  7.7  .6
    "2007-05"  7.6  .3
    "2007-06"  7.5  .1
    "2007-07"  7.5 -.2
    "2007-08"  7.5  .1
    "2007-09"  7.4  .4
    "2007-10"  7.4  .5
    "2007-11"  7.3  .6
    "2007-12"  7.3  .4
    "2008-01"  7.2 -.1
    "2008-02"  7.1  .4
    "2008-03"  7.1  .9
    "2008-04"  7.1  .4
    end
    As you can see, it is about monthly observations from 2000 to 2019 of the Unemployment and Inflation rates for a given economy.

    The idea is to draw a line graph which shows the yearly evolution of both variables, with the monthly observations as ticks in each year.

    When I run this code:

    gen mdate = monthly(substr(Month, 1, 7), "YM")
    local tickpos = ym(2000, 12) + 0.5
    local ticks `tickpos'
    forval y = 2000/2019 {
    local labelpos = `tickpos' + 6
    local labels `labels' `labelpos' "`y'"
    local tickpos = ym(`y', 12) + 0.5
    local ticks `ticks' `tickpos'
    }

    line Unemployment_Rate Inflation_Rate mdate, lc(red blue) yla(, ang(h)) ytitle(%, orient(horiz)) xla(`labels', noticks) xticks(`ticks', tlength(*3)) xtitle("") legend(ring(0) pos(11) col(1)) xsc(r(. `=ym(2025, 6)')) xli(`ticks', lc(gs12))

    I get a line graph that is offset from the X axis of the years, and I do want to adjust that.

    Any help please?

    With many thanks!

  • #2
    Thanks for the data example. You want to center the year label at the midpoint of the interval, that is, either in June or July.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 Month float(Unemployment_Rate Inflation_Rate)
    "2000-01"  9.7  .3
    "2000-02"  9.7  .4
    "2000-03"  9.7  .4
    "2000-04"  9.6  .2
    "2000-05"  9.6  .2
    "2000-06"  9.5  .4
    "2000-07"  9.5  .2
    "2000-08"  9.4  .1
    "2000-09"  9.4  .6
    "2000-10"  9.3  .2
    "2000-11"  9.3  .3
    "2000-12"  9.3  .4
    "2001-01"  9.3 -.2
    "2001-02"  9.2  .4
    "2001-03"  9.2  .6
    "2001-04"  9.3  .7
    "2001-05"  9.3  .6
    "2001-06"  9.3  .2
    "2001-07"  9.3 -.2
    "2001-08"  9.3   0
    "2001-09"  9.3  .3
    "2001-10"  9.4  .2
    "2001-11"  9.4   0
    "2001-12"  9.5  .5
    "2002-01"  9.5  .2
    "2002-02"  9.5  .2
    "2002-03"  9.5  .5
    "2002-04"  9.6  .5
    "2002-05"  9.6  .2
    "2002-06"  9.6 -.1
    "2002-07"  9.7 -.1
    "2002-08"  9.7  .1
    "2002-09"  9.8  .3
    "2002-10"  9.7  .3
    "2002-11"  9.8   0
    "2002-12"  9.8  .5
    "2003-01"  9.8  .1
    "2003-02"  9.8  .4
    "2003-03"  9.8  .6
    "2003-04"  9.8  .2
    "2003-05"  9.8 -.1
    "2003-06"  9.9  .1
    "2003-07"  9.9 -.1
    "2003-08"  9.9  .1
    "2003-09"  9.9  .4
    "2003-10"  9.9  .2
    "2003-11"  9.9  .1
    "2003-12"   10  .3
    "2004-01" 10.1 -.1
    "2004-02"   10  .2
    "2004-03" 10.1  .6
    "2004-04"   10  .5
    "2004-05"   10  .4
    "2004-06"   10  .1
    "2004-07"  9.9 -.2
    "2004-08"  9.9  .1
    "2004-09"   10  .1
    "2004-10"   10  .4
    "2004-11"   10 -.1
    "2004-12"  9.9  .3
    "2005-01"  9.8 -.5
    "2005-02"  9.8  .4
    "2005-03"  9.8  .7
    "2005-04"  9.8  .5
    "2005-05"  9.8  .2
    "2005-06"  9.8  .1
    "2005-07"  9.7 -.1
    "2005-08"  9.6  .2
    "2005-09"  9.6  .5
    "2005-10"  9.6  .3
    "2005-11"  9.5 -.2
    "2005-12"  9.4  .3
    "2006-01"  9.3 -.3
    "2006-02"  9.2  .3
    "2006-03"  9.1  .5
    "2006-04"    9  .7
    "2006-05"  8.9  .3
    "2006-06"  8.7  .1
    "2006-07"  8.7 -.1
    "2006-08"  8.6  .1
    "2006-09"  8.5   0
    "2006-10"  8.4  .1
    "2006-11"  8.3  .1
    "2006-12"  8.2  .3
    "2007-01"    8 -.4
    "2007-02"  7.9  .3
    "2007-03"  7.8  .6
    "2007-04"  7.7  .6
    "2007-05"  7.6  .3
    "2007-06"  7.5  .1
    "2007-07"  7.5 -.2
    "2007-08"  7.5  .1
    "2007-09"  7.4  .4
    "2007-10"  7.4  .5
    "2007-11"  7.3  .6
    "2007-12"  7.3  .4
    "2008-01"  7.2 -.1
    "2008-02"  7.1  .4
    "2008-03"  7.1  .9
    "2008-04"  7.1  .4
    end
    
    label variable Unemployment_Rate "Unemployment" 
    label variable Inflation_Rate "Inflation"
    
    gen mdate = monthly(substr(Month, 1, 7), "YM")
    format mdate %tm
    
    local labs
    local tick `=ym(1999, 6)'
    forval y = 2000/2008{
        local tick= `tick'+12
        local labs `labs' `tick' "`y'"
    }
    levelsof mdate if substr(Month,-2,.)=="12", local(months)
    
    line Unemployment_Rate Inflation_Rate mdate, lc(red blue) ///
    yla(, ang(h)) ytitle(%, orient(horiz)) xla(`labs', tlength(0) nogrid) ///
    xline(`months', lc(gs12) lw(vthin) lp(solid)) xtitle("") ///
    xtick(`months', tlength(*4)) legend(pos(11) col(1))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	46.3 KB
ID:	1783339

    Comment


    • #3
      This is in similar spirit to the helpful answer by Andrew Musau

      I have suggestion on several levels, some very small.

      See https://www.statalist.org/forums/help #18 and #12 for the spelling "Stata" not "STATA" and advice to tell us the version of Stata you are using, which at least in recent posts from you was some versions old.

      Give all your code in one block using CODE delimiters, not in separate blocks with interspersed text that must be edited out to run it.

      More important than those points in this case is the advice (also #12) to show the graphs that you are asking about as .png attachments so that we can see what the problem is.

      See

      Code:
      help dataex
      for the option count(): you are relying on the detault of 100 observations and giving us an incomplete data example. That detail could be important if a design would not work well with your fuller dataset.

      Your data example starts at January 2000, so the first tick should be just after December 1999, not a year later.

      Not done here, but using variable labels would permit you to have spaces not underscores and to add units to each variable description.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str7 Month float(Unemployment_Rate Inflation_Rate)
      "2000-01"  9.7  .3
      "2000-02"  9.7  .4
      "2000-03"  9.7  .4
      "2000-04"  9.6  .2
      "2000-05"  9.6  .2
      "2000-06"  9.5  .4
      "2000-07"  9.5  .2
      "2000-08"  9.4  .1
      "2000-09"  9.4  .6
      "2000-10"  9.3  .2
      "2000-11"  9.3  .3
      "2000-12"  9.3  .4
      "2001-01"  9.3 -.2
      "2001-02"  9.2  .4
      "2001-03"  9.2  .6
      "2001-04"  9.3  .7
      "2001-05"  9.3  .6
      "2001-06"  9.3  .2
      "2001-07"  9.3 -.2
      "2001-08"  9.3   0
      "2001-09"  9.3  .3
      "2001-10"  9.4  .2
      "2001-11"  9.4   0
      "2001-12"  9.5  .5
      "2002-01"  9.5  .2
      "2002-02"  9.5  .2
      "2002-03"  9.5  .5
      "2002-04"  9.6  .5
      "2002-05"  9.6  .2
      "2002-06"  9.6 -.1
      "2002-07"  9.7 -.1
      "2002-08"  9.7  .1
      "2002-09"  9.8  .3
      "2002-10"  9.7  .3
      "2002-11"  9.8   0
      "2002-12"  9.8  .5
      "2003-01"  9.8  .1
      "2003-02"  9.8  .4
      "2003-03"  9.8  .6
      "2003-04"  9.8  .2
      "2003-05"  9.8 -.1
      "2003-06"  9.9  .1
      "2003-07"  9.9 -.1
      "2003-08"  9.9  .1
      "2003-09"  9.9  .4
      "2003-10"  9.9  .2
      "2003-11"  9.9  .1
      "2003-12"   10  .3
      "2004-01" 10.1 -.1
      "2004-02"   10  .2
      "2004-03" 10.1  .6
      "2004-04"   10  .5
      "2004-05"   10  .4
      "2004-06"   10  .1
      "2004-07"  9.9 -.2
      "2004-08"  9.9  .1
      "2004-09"   10  .1
      "2004-10"   10  .4
      "2004-11"   10 -.1
      "2004-12"  9.9  .3
      "2005-01"  9.8 -.5
      "2005-02"  9.8  .4
      "2005-03"  9.8  .7
      "2005-04"  9.8  .5
      "2005-05"  9.8  .2
      "2005-06"  9.8  .1
      "2005-07"  9.7 -.1
      "2005-08"  9.6  .2
      "2005-09"  9.6  .5
      "2005-10"  9.6  .3
      "2005-11"  9.5 -.2
      "2005-12"  9.4  .3
      "2006-01"  9.3 -.3
      "2006-02"  9.2  .3
      "2006-03"  9.1  .5
      "2006-04"    9  .7
      "2006-05"  8.9  .3
      "2006-06"  8.7  .1
      "2006-07"  8.7 -.1
      "2006-08"  8.6  .1
      "2006-09"  8.5   0
      "2006-10"  8.4  .1
      "2006-11"  8.3  .1
      "2006-12"  8.2  .3
      "2007-01"    8 -.4
      "2007-02"  7.9  .3
      "2007-03"  7.8  .6
      "2007-04"  7.7  .6
      "2007-05"  7.6  .3
      "2007-06"  7.5  .1
      "2007-07"  7.5 -.2
      "2007-08"  7.5  .1
      "2007-09"  7.4  .4
      "2007-10"  7.4  .5
      "2007-11"  7.3  .6
      "2007-12"  7.3  .4
      "2008-01"  7.2 -.1
      "2008-02"  7.1  .4
      "2008-03"  7.1  .9
      "2008-04"  7.1  .4
      end
      
      gen mdate = monthly(Month, "YM")
      local tickpos = ym(1999, 12) + 0.5
      local ticks `tickpos'
      forval y = 2000/2007 {
      local labelpos = `tickpos' + 6
      local labels `labels' `labelpos' "`y'"
      local tickpos = ym(`y', 12) + 0.5
      local ticks `ticks' `tickpos'
      }
      
      multiline Unemployment_Rate Inflation_Rate mdate, lc(red blue) yla(, ang(h)) ytitle(%, orient(horiz)) xla(`labels', noticks) xticks(`ticks', tlength(*3)) xtitle("") xli(`ticks', lc(gs12)) separate by(legend(off))
      Click image for larger version

Name:	azuz3.png
Views:	1
Size:	86.8 KB
ID:	1783345

      Comment


      • #4
        Nick Cox & Andrew Musau Thanks for the help! I did like the two options, I ended up using @Nick Cox's solution for a better graph and a better reading of the figures.

        Comment


        • #5
          multiline is from SSC, as I should have documented in #3.

          Comment

          Working...
          X