Announcement

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

  • Place date year on a second line on x-axis

    My x-axis data is currently in date (week) format, so my current code as part of a twoway graph is as follows to format the date to months and have 7 ticks:
    , tlabel(#7, format(%twm))

    The data is over two years, so I would like to please have a second line for my x-axis with 2020 below the first 4 dates, and 2021 below the 3 last dates.
    This seems to be possible when working with categorical variables on the x-axis
    e.g. xlabel(1 `" "PGM activity" "in previous conflict" "' 2 `" "no PGM activity" "in previous conflict" "', labsize(small))

    But I cannot figure out an equivalent for date data.

    Any help would be greatly appreciated!

  • #2
    Each week has a distinct SIF value, e.g., 2021w1 is

    Code:
    di tw(2021w1)
    Res.:


    . di tw(2021w1)
    3172
    So you need something like

    Code:
    xlabel(3172 `" "PGM activity" "in previous conflict" "' 3173 `" "no PGM activity" "in previous conflict" "', labsize(small))
    or more directly,

    Code:
     xlabel(`tw(2021w1)' `" "PGM activity" "in previous conflict" "' `tw(2021w2)' `" "no PGM activity" "in previous conflict" "', labsize(small))

    Comment


    • #3
      I wouldn't use Stata's week display format at all unless you know that your weeks are Stata weeks, that is week 1 starts on 1 January (always), week 2 starts on 8 January (always), with each week thereafter being 7 days except that week 52 is 8 or 9 days, depending.

      Almost everybody's weeks start on a particular day of the week, or equivalently end on a different particular day, and weeks are thus usually best recorded in terms of that daily date. For more discussion than you probably want, see these results from

      Code:
      search week, sj
      which will yield clickable licks.

      SJ-12-4 dm0065_1 . . . . . Stata tip 111: More on working with weeks, erratum
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q4/12 SJ 12(4):765 (no commands)
      lists previously omitted key reference

      SJ-12-3 dm0065 . . . . . . . . . . Stata tip 111: More on working with weeks
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q3/12 SJ 12(3):565--569 (no commands)
      discusses how to convert data presented in yearly and weekly
      form to daily dates and how to aggregate such data to months
      or longer intervals

      SJ-10-4 dm0052 . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q4/10 SJ 10(4):682--685 (no commands)
      tip on Stata's solution for weeks and on how to set up
      your own alternatives given different definitions of the
      week

      Comment


      • #4
        One way to do do that is to use minor axis labels for week numbers and the major axis labels for the years. You can get the that Stata uses for a particular week with the yw() function (see: help datetime##s5). You can remove the year from the display using format (see help datetime_display_formats). To execute a function directly you can surround it with `= and '. In the example below I use local macros, so you have to copy the code in the do-file editor and run it in one go. You cannot run it step by step, because the locals would disappear at the end of each step.

        Code:
        //create some example data
        clear
        set scheme s1color
        set obs 104
        gen week = cond(_n <= 52, _n, _n - 52)
        gen year = cond(_n <= 52, 2019, 2020)
        gen date = yw(year,week)
        format date %tw
        
        gen y = 2 + (date - 3120) + .5*(date - 3120)^2 + 100*rnormal()
        
        // this is the x-labels you don't want
        twoway scatter y date
        
        // remove the year
        format date %twww
        twoway scatter y date
        
        // collect the minor (weeks) and major (years) labels
        local year =  `"`=yw(2019,26)' "2019" `=yw(2020,26)' "2020""'
        local week = `"`=yw(2019,1)' `=yw(2019,13)' `=yw(2019,26)' `=yw(2019,39)' `=yw(2020,1)' "'
        local week = `"`week' `=yw(2020,13)' `=yw(2020,26)' `=yw(2020,39)' `=yw(2020,52)' "'    
        
        // use those to get the graph we want
        twoway scatter y date, xmlab(`week') xlab(`year', labgap(*2)) ///
               xtitle(week) ylab(,angle(0))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	63.0 KB
ID:	1611814

        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment

        Working...
        X