Announcement

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

  • Bar graph over dates, format of dates

    I have a time series and would like to make a bar graph over dates, is it possible to specify the label format of the dates?


    Code:
    webuse turksales, clear
    graph bar sales if t>tq(1999q1), over(t)
    I know I could write the dates to string, and graph over the string variable, but then the order is alphabetical. Is it possible to specify the display order of the bars?

    Thanks for reading.

  • #2
    over() has a sort() suboption.

    Comment


    • #3
      I would use twoway bar for greater flexibility here. For example, with more than a few bars you won't have either the space or the inclination to label them all by dates, and graph bar assumes that evey category should be explained.

      But one way round is to copy a formatted date to a string variable and then to copy those format strings to value labels. Here labmask is from the Stata Journal and must be installed before use.

      Code:
      webuse turksales, clear
      
      twoway bar sales t if t>tq(1999q1), barw(0.8) name(G1, replace) 
      
      gen T = string(t, "%tqCCYY-q") 
      labmask t, values(T) 
      
      graph bar (asis) sales if t>tq(1999q1), over(t) name(G2, replace)

      Comment


      • #4
        Here's a variant of Nick's approach (developed before I saw his) that does not require labmask.
        Code:
        webuse turksales, clear
        levelsof t, local(times)
        foreach time of local times {
            label define T `time' `"`= strofreal(`time',"%tq")'"', add
        }
        label values t T
        graph bar sales if t>tq(1999q1), over(t)

        Comment


        • #5
          Friedrich - thank you, that works great

          Nick - one odd thing I see, when I do this

          Code:
           webuse turksales, clear
          twoway bar sales t if t>tq(1999q1), barw(0.8) name(G1, replace)
          I get missing bars, as in the dates are correctly placed on the xaxis but there are just no bars for 1999q1 and 1999q2. After I go through the rest of the steps and then plot again, then all the bars are placed correctly in graph G2. Otherwise this also works great, thank you (for anyone else reading the command -labmask- can be installed by doing "ssc install labutil")

          Comment


          • #6
            Working backwards,

            1. The better source for labmask is, as indicated in #3, the Stata Journal, not least because the paper

            . search labmask, sj

            Search of official help files, FAQs, Examples, SJs, and STBs

            SJ-8-2 gr0034 . . . . . . . . . . Speaking Stata: Between tables and graphs
            (help labmask, seqvar if installed) . . . . . . . . . . . . N. J. Cox
            Q2/08 SJ 8(2):269--289
            outlines techniques for producing table-like graphs
            contains pertinent discussion.

            2. With twoway bar the axis range is chosen using a general algorithm. If you want to reach in and remove superfluous time points, you'd find xsc() and/or xla() options suitable for that.

            Fond though I am of graphics, if your real problem is to show three numbers, I suggest that a little table should suffice!

            Comment


            • #7
              Ah, forgot about the search sj command, thanks.

              For the displayed xaxis range, 1999q2 should still be plotted right, based on the if condition? I think showing that time period is completely correct, but just the actual bar of data is missing, is this a mistake in the graph output?

              (my actual graphing program will be using stacked bar chart for a rolling window of about 10 days, so probably not in favor of a table, but still agree with you if it was just a few days)

              Comment


              • #8
                You probably need base(0) in the syntax. I haven't checked the example again, but I'd suspect user syntax problem (mine) rather than programmer bug.

                Comment

                Working...
                X