Announcement

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

  • Bar graph

    Dear all,
    I wish to create a bar graph with 3 variables (inv_rate5 inv_rate10 inv_rate100), with a timeline in the 1903-1971 year range. I want to show year label every 5 years. I followed the suggestion provided by Cox (2021) to create a local to relabel the year values, but it does not seem to work in my case:

    local call
    forvalues j = 1903/1971 {
    local show = `j'
    if mod(`j', 5) local call `call' `j' "`show'"
    else local call `call' `j' " "
    }
    graph bar inv_rate5 inv_rate10 inv_rate100, stack over(anno, relabel(`call') label(angle(45) labsize(tiny)))



    References
    Cox, N. J. (2021). Stata tip 140: Shorter or fewer category labels with graph bar. The Stata Journal, 21(1), 263–271. https://doi.org/10.1177/1536867X211000032
    Attached Files

  • #2
    Your modifications of my code for your case broke the code. The original had

    Code:
    if mod(`j', 2)
    which is true when year (in this case) is odd and false when year is even.

    But

    Code:
    if mod(`j', 5)
    is true if year is not a multiple of 5 and false when it is, so the following code will omit labels for only 1 year out of 5.

    Further, the mapping of labels is for bars labelled 1 up, not in terms of the literal values of your year variable. So you are remapping labels for bars 1903 to 1971, which has no effect because there are no such bars in the graph.

    Consider this example:

    Code:
    clear
    set obs 69
    gen anno = 1902 + _n
    gen whatever = runiformint(0, 20)
    
    local call
    forvalues j = 1/69 {
    local show = `j' + 1902
    if mod(`j' - 3, 5) == 0 local call `call' `j' "`show'"
    else local call `call' `j' " "
    }
    
    set scheme s1color
    graph bar (asis) whatever, over(anno, relabel(`call') label(ang(v)))
    Click image for larger version

Name:	betterbar.png
Views:	1
Size:	23.0 KB
ID:	1696697




    I don't know what your variables are and whether stacking them makes sense, but suggest that twoway is easier here.
    Last edited by Nick Cox; 10 Jan 2023, 12:09.

    Comment


    • #3
      Stata Tip has come up to No.148 and Speaking Stata has come up to gr0092. So after Thirty three Stata Tips (2006), Seventy six Stata Tips (2009), and One Hundred Nineteen Stata Tips (2014), will Stata prepare to press a new edition of One Hundred Forty Eight (or more) Stata Tips in 2023?

      Comment


      • #4
        Thank you for your help!
        Indeed, I tried to use twoway (bar), but I need to: i) stack the variables one on top of the other, without overlapping; ii) allow the same variable to have both positive and negative values.
        As far as I understood, I could solve issue i) by transforming variables into cumulative sums, but not issue ii).

        Comment


        • #5
          I can't see that stacking variables that can be negative will work well even if makes any sense. You need either a line chart or offsets.

          This script shows some technique.

          Code:
          clear
          set obs 11
          set seed 314159265
          gen year = 2011 + _n
          gen y1 = runiformint(1, 10)
          gen y2 = runiformint(-4, 5)
          
          gen year1 = year - 0.2 
          gen year2 = year + 0.2 
          
          set scheme s1color 
          twoway bar y1 year1, barw(0.4) || bar y2 year2, barw(0.4)

          Comment

          Working...
          X