Announcement

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

  • Dates as Bar Graph Labels

    My question is two part. Does the problem with formatting dates on bar charts still exist and if so why isn't my workaround working?

    First, I generate a bar graph over a variable that is a date (months). The date labels appear as integers, in my case running from 606 (July, 2010) to 648 (January, 2014). Here is the command

    graph bar (mean) h0 (mean) myfailure, over(OBS_DATE, label(alternate labsize(tiny) format(%tm) ))

    I read this message by Nick
    http://www.stata.com/statalist/archi.../msg01119.html

    Is it still the case that there isn't any direct way to get the dates to format correctly on bar graphs?

    If that is true then my understanding is that the work around is something like this

    graph bar (mean) h0, over(OBS_DATE, relabel(1 "07/10" 2 "08/10" ... 42 "12/13" 43 "01/14") label(alternate labsize(tiny) format(%tm) ))

    notice the ellipse in the arguments for the relabel command. When I type full list in directly it works. However, it ends up being very long. Also it changes each month, so I would like to automate the generation of some sort of local so I can write

    graph bar (mean) h0, over(OBS_DATE, relabel(`my_date_labels') label(alternate labsize(tiny) format(%tm) ))

    I feel like I am close but the nesting quotes is killing me. I run the following

    forvalues i = 606/648 {
    local tmp: display %tmNN/YY `i'
    local tmp1 `""`tmp'""'
    display `"`tmp1'"'
    local j = `i' - 605
    display "`j'"
    local tmp2 `j' " " `"`tmp4'"' " "
    display `tmp2'
    local my_date_labels `my_date_labels' `tmp2'
    display `my_date_labels'
    }

    and my_date_labels looks like 1 "07/10" 2 "08/10" ... 42 "12/13" 43 "01/14" which I think is what I want
    but then the graph command produces

    option labels() incorrectly specified
    expects, # "label" # label ... r(198);

    Are there extra quotes I can't see because they are getting stripped off by the Display command? Something else wrong?

    Any help is appreciated,
    Caleb

  • #2
    My mind boggles somewhat at the thought of a bar chart with 43 labels, but in principle you could just define value labels containing the date format you want and graph bar respects value labels. You don't have to use a loop for that. labmask (SJ, SSC) is a helper program that defines a set of value labels that are the values of another variable. Here is a dopey example:

    Code:
     
    clear 
    set obs 43 
    gen t = 606 + _n 
    expand 2 
    gen y = 42 
    gen label = string(t, "%tmNN/YY") 
    labmask t, values(label) 
    graph hbar (mean) y, over(t)

    Comment


    • #3
      Ok thanks, I hadn't thought of that approach. And yea, the labels end up very tiny.

      Comment


      • #4

        I'd probably use a line chart and label (say) every January and July.

        Comment


        • #5
          I recognize this relates to a very old post. Since I used the solution proposed by Nick Cox in his April 29, 2014 post to solve my issue, I chose to continue the existing thread.

          There seems an issue with the graph command regarding properly labeling the x-axis.

          I have two-variable dataset with the following variables (the actual dataset has many more variables, but I condensed it for the post):

          STAYS, which represents monthly stays for various VRBO rentals; and,
          date_monthly, which is a time-formatted variable for the month and year.

          Observations: 1,682
          Variables: 2 1 Apr 2024 10:05

          Variable Storage Display Value
          name type format label Variable label
          -------------------------------------------------------------
          STAYS byte %10.0g Stays
          date_monthly int %tm..
          -------------------------------------------------------------
          Sorted by:


          A snippet of the actual data (this data pertains to VRBO rentals at specific properties) follows.

          +------------------+
          | STAYS date_m~y |
          |------------------|
          1. | . Jan 2022 |
          2. | . Feb 2022 |
          3. | . Mar 2022 |
          4. | . Apr 2022 |
          5. | . May 2022 |
          |------------------|
          6. | . Jun 2022 |
          7. | . Jul 2022 |
          8. | . Aug 2022 |
          9. | . Sep 2022 |
          10. | 4 Oct 2022 |
          |------------------|
          11. | 3 Nov 2022 |
          12. | 8 Dec 2022 |
          13. | 5 Jan 2023 |
          14. | 4 Feb 2023 |
          15. | 4 Mar 2023 |
          |------------------|


          Note: the property identification is not provided below but the date_monthly will repeat for each property. I included the snippet to demonstrate the date formatting.

          The code:
          graph bar (sum) STAYS, over(date_monthly, label(angle(forty_five) labsize(vsmall))) blabel(bar)

          yields:

          Click image for larger version

Name:	graph 1.png
Views:	1
Size:	120.3 KB
ID:	1748622

          I confirmed that the x-axis labels are the correct monthly index variables since Jan 1960.
          I reviewed the Stata manual regarding the graph command. There does not appear to be a formatting option for the x-axis for this command. I could not find anything on point within the forum.
          The issue was solved using the label command suggested by Nick Cox, but it seems that there is a problem with Stata not inheriting the data format for the relevant variable. Wanted to check with everyone on the forum before proceeding further.

          Thanks!

          JP Anderson

          Comment


          • #6
            #5

            2014 does not count as "very old" in Stata terms. One of the long-term benefits of using Stata is that while new commands are added routinely the old commands don't get changed much without good reason.

            graph bar in an updated Stata 18 (still) doesn't respect an assigned monthly date display format. You're free to regard that as a limitation or even as a bug.

            I would say that the intent of graph bar remains to show distinct values for different categories, as explained in a user's data using string values or value labels attached to numeric variables.

            That said, my personal view is that the graph you display is not acceptable any way and displaying every bar with an intelligible monthly date too makes it a worse mess. For even a few years' worth of monthly data showing every date as such is usually not necessary and it's all too easy to produce a crowded mess.

            Again, a personal view is that any label orientation other than horizontal is a poor choice, although I have been forced into it myself on occasion.

            The headline advice from me is just "switch to twoway" in any case.

            The data example in #5 is not directly useful to show the problem even when translated to dataex form, as it only shows 6 non-missing values. But we can fake something which shows the graphical issues. I leave entirely on the side the question of whether a line chart would be better (1) for these fake data (2) for your data.

            Code:
            clear
            set obs 36
            gen mdate = 731 + _n
            format mdate %tm
            set seed 2803
            gen y = rpoisson(30)
            
            graph bar (asis) y, over(mdate) name(G1, replace)
            
            twoway bar y mdate, base(0) yla(0(10)40) barw(0.9) name(G2, replace)
            
            twoway bar y mdate, base(0) yla(0(10)40) barw(0.9) xla(737(12)761, tlc(none) format(%tmCY)) xtitle("") xli(731.5(12)767.5, lp(solid)) name(G3, replace)
            
            tokenize "J F M A M J J A S O N D"
            
            forval m = 732/767 {
                local M = month(dofm(`m'))
                label define mdate `m' "``M''", add
            }
            
            label val mdate mdate
            
            twoway bar y mdate, base(0) yla(0(10)40) barw(0.9) xla(737(12)761, tlength(*2.5) tlc(none) format(%tmCY)) xmla(732/767, noticks valuelabel labsize(small)) xtitle("") xli(731.5(12)767.5, lp(solid)) name(G4, replace)
            The first graph reproduces the problem, Monthly dates 731 on are just shown as such.
            Click image for larger version

Name:	mdate_G1.png
Views:	1
Size:	39.2 KB
ID:	1748734




            Note incidentally that graph bar can have no sense that months are intervals that touch, although you could work on the spacing between bars.

            The second graph is close to default for twoway bar. I use barw(0.9) purely as a cosmetic choice. 2021m1 and the like are easy enough to decode but Stata makes other choices possible.
            Click image for larger version

Name:	mdate_G2.png
Views:	1
Size:	45.7 KB
ID:	1748731




            The third graph implements something often seen with monthly data -- it is years that are shown as intervals.

            This approach was written up in

            SJ-7-4 gr0030 . Stata tip 55: Better axis labeling for time points/intervals
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
            Q4/07 SJ 7(4):590--592 (no commands)
            tip on labeling graphs for time points and time intervals

            https://www.stata-journal.com/articl...article=gr0030


            I find Stata's support for monthly dates in graphs a little hard to follow and possibly buggy, but as you see the code above works here.
            Click image for larger version

Name:	mdate_G3.png
Views:	1
Size:	38.9 KB
ID:	1748732




            That approach might seem a little spartan. There is room for single-letter month abbreviations, which aren't provided as a date display format. So, that would need some code assigning value labels.

            The code is a little fiddly. One advantage is that it could be adapted for months in languages other than English. For example, in French people might prefer lower case j f m a m j j a s o n d.
            Click image for larger version

Name:	mdate_G4.png
Views:	1
Size:	41.6 KB
ID:	1748733




            See also


            SJ-21-1 gr0086 Stata tip 140: Shorter or fewer category labels with graph bar
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
            Q1/21 SJ 21(1):263--271 (no commands)
            tip on fixing categorical axis labels

            https://journals.sagepub.com/doi/pdf...6867X211000032


            SJ-19-3 gr0079 . . . . . . . . . Stata tip 132: Tiny tricks and tips on ticks
            . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and V. Wiggins
            Q3/19 SJ 19(3):741--747 (no commands)
            collection of small points on those small ticks, intended to
            explain what is not quite obvious -- or not quite obviously
            useful -- on reading the documentation

            https://journals.sagepub.com/doi/pdf...36867X19874264
            Last edited by Nick Cox; 03 Apr 2024, 07:03.

            Comment


            • #7
              Thank you so much for the quick and comprehensive response, along with the article references.

              I agree that it is dense; I didn't want to spend much time formatting until I knew whether it was an issue inherent to Stata for this command. I would consider it a bug and intend to advise Stata of it (I am using Stata 18.0).
              While not a novice Stata user, it is sometimes easier for me to build the initial command using the Stata interactive command menu and then enrich the command from there.

              For this analysis, I thought the graph bar command was a more parsimonious approach than twoway bar, as I did not see a sum option for twoway bar.

              One presentation approach I am considering is stacking the bar graphs by year so that the months are vertically aligned. This presentation would aid the reader in visualizing seasonality.

              Comment


              • #8
                There isn't a sum option for twoway bar. That's not usually a big problem, You usually just need a single egen call to get what you want to plot as a new variable.

                If you're a long term user of Stata, you may be a long term reader too. If so, you might have picked up that I am pretty negative on stacked bar charts for almost any purpose but I have posted code and papers on graphics for seasonality.

                Code:
                SJ-9-2  gr0037  . . . . . . . .  Stata tip 76: Separating seasonal time series
                        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                        Q2/09   SJ 9(2):321--326                                 (no commands)
                        tip on separating seasonal time series
                
                SJ-6-3  gr0025  . . . . . . . . . . . . Speaking Stata: Graphs for all seasons
                        (help cycleplot, sliceplot if installed)  . . . . . . . . .  N. J. Cox
                        Q3/06   SJ 6(3):397--419
                        illustrates producing graphs showing time-series seasonality

                Comment

                Working...
                X