Announcement

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

  • Adding date labels to Graph bar with the over() option

    Hello,

    I'm struggeling to add the formated date labels to my x-axis when using the graph bar (count) command.

    My dates look like this:

    Value
    123 2020m1
    124 2020m2
    125 2020m3

    and so on...

    And my graph code is as followes:

    graph bar (count) , over(date, label(angle(45)))

    I would like to add in tlabel(,format(%tm)) to get my x-axis to say '2020m1 2020m2...' instead of '123, 124..'. But that only gives me an error message saying 'tlabels(,format(%tm)) not allowed, xaxis1 does not exist'

    Are there any other ways of doing this?

  • #2
    You're going to be better off in my view with something like


    Code:
    bysort date: gen count = _N
    
    twoway bar count date
    and then being more selective about axis labels.If you show the results of

    Code:
    su date 
    you'll get more tailored advice.

    Comment


    • #3
      Thank you, Nick!


      My final aim is to get a graph that looks something like the image below.

      The reason I prefer the code:

      graph bar (count) , over(date)

      is that I know how to add the numbers above the bars and I also don't get large gaps during periods with no recruitment.



      The only other way I can think of changing the x-axis is by categorising the date variable. Which seem like a less elegant solution.



      What I want:
      Click image for larger version

Name:	Example total accrual.png
Views:	1
Size:	73.1 KB
ID:	1697736



      What I curretly have:
      Click image for larger version

Name:	Example total accrual 2.png
Views:	1
Size:	13.1 KB
ID:	1697737

      Last edited by Maria Elstad; 18 Jan 2023, 06:43.

      Comment


      • #4
        Code:
        dataex date
        provide essential information would be helpful here.

        Comment


        • #5
          Here I fake the counts but am realistic about dates from October 2007 to September 2009.

          Major points:

          1. Showing the number too is easy with twoway.

          2. Vertical x axis labels are not ideal. Here I assume month initials are J F M .... O N D but choose your own by all means. For various tricks and tips on labels and ticks see https://www.stata-journal.com/articl...article=gr0079 and https://www.stata-journal.com/articl...article=gr0030

          Code:
          clear
          set obs 24
          gen date = ym(2007, 9) + _n
          set seed 314159265
          gen count = runiformint(1, 13)
          
          set scheme s1color
          twoway bar count date || scatter count date, mlabel(count) ms(i) mlabpos(12)
          
          local M O N D J F M A M J J A S O N D J F M A M J J A S
          
          forval  i = 1/24 {
              local d = `i' + ym(2007, 9)
              local text = word("`M'", `i')
              local call `call' `d' "`text'"
          }
          
          local T1 = ym(2007, 12) + 0.5
          local T2 = `T1' + 12
          local L1 = ym(2007, 11)
          local L2 = ym(2008, 6) + 0.5
          local L3 = ym(2009, 5)
           
          twoway bar count date || scatter count date, mlabel(count) ms(i) mlabpos(12) mlabc(black) ///
           xmla(`call', tlc(none) labsize(medsmall)) xtick(`T1' `T2', tlength(*6)) xlabel(`L1' "2007" `L2' "2008" `L3' "2009", tlc(none) tlength(*3.5)) legend(off) xtitle("") yla(, ang(h)) xli(`T1' `T2', lc(gs8) lw(thin))

          Comment


          • #6
            Now that is a good looking graph, thank you, Nick!

            Comment


            • #7
              The example raises the question of how to automate axis labels like JFMAMJJASOND for months of the year whenever you decide that they are good choices, possibly for monthly data for between 1 and 5 or so years.

              The code would need some extra twists if you are using a language in which some months have different initial letters and be of limited use if they all have different initial letters.

              This script moves in that direction. Automating ticks and labels for mid- and end year might also be done, but doing it by hand is also good, if not for the soul, then for cementing understanding of some useful options.

              Code:
              clear
              set obs 24
              gen date = ym(2007, 9) + _n
              set seed 314159265
              gen count = runiformint(1, 13)
              
              set scheme s1color
              twoway bar count date || scatter count date, mlabel(count) ms(i) mlabpos(12)
              
              * !!! next three commands are new in this script
              gen text = substr(strofreal(date, "%tmMon"), 1, 1)
              
              levelsof date, local(levels)
              
              * labmask is from the Stata Journal
              labmask date, values(text)
              
              local T1 = ym(2007, 12) + 0.5
              local T2 = `T1' + 12
              local L1 = ym(2007, 11)
              local L2 = ym(2008, 6) + 0.5
              local L3 = ym(2009, 5)
               
               * !!! xmla() call different
              twoway bar count date || scatter count date, mlabel(count) ms(i) mlabpos(12) mlabc(black) ///
               xmla(`levels', valuelabel tlc(none) labsize(medsmall)) xtick(`T1' `T2', tlength(*6)) xlabel(`L1' "2007" `L2' "2008" `L3' "2009", tlc(none) tlength(*3.5)) legend(off) xtitle("") yla(, ang(h)) xli(`T1' `T2', lc(gs8) lw(thin))

              Note: for monthly data over several years I might switch to showing just Jan Apr Jul Oct, say.
              Last edited by Nick Cox; 19 Jan 2023, 04:14.

              Comment

              Working...
              X