Announcement

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

  • Date variable in x axis of graph bar command can't be altered

    I am using a command graph bar A B C D E, over(Date) stack percent. However I couldn't edit Date such as limit the number of Date labels. Is there a way to sort this issue?
    Attached Files

  • #2
    Good Lord man. Surely something... wrong, is afoot here. I am sure there's a solution, but first I'm definitely going to need your sample data via dataex as well as the precise syntax you used to obtain this, all in code delimiters

    Comment


    • #3
      The problem here was addressed in https://www.stata-journal.com/articl...article=gr0086

      In essence graph bar serves as a snare and a distraction for what you want to do. Its main idea is that you have a categorical variable, so that you (should) care about each category, and want to see every category labelled. Not so in your case.

      There are work-arounds for some versions of this problem, for which see the paper, but the best work-around for your problem is not to use graph bar at all. You want twoway bar and rbar or twoway area and rarea. Then you can put as many -- or more importantly as few -- date labels on the x axis as you want and assign whatever format you wish.

      I agree with Jared Greathouse that a data example with dataex -- such as we explicitly request in the FAQ Advice -- would have helped here.

      Token code with a reproducible example follows.

      Code:
      clear
      set obs 730
      set seed 2803
      set scheme s1color 
      
      gen total = 0 
      tokenize 0.4 0.3 0.2 0.1 0.1 
      local vars A B C D E 
      
      forval i = 1/5 { 
          gettoken v vars : vars
          
          gen `v' = ``i'' + rnormal(0, 0.01)
          
          replace total = total + `v'
      }
      
      gen date = mdy(12, 31, 2016) + _n
      format date %td 
      
      * start here 
      
      local vars A B C D E 
      
      forval j = 1/5 { 
          gettoken v vars : vars
          gen y`j' = 100 * `v' / total 
      }
      
      gen Y2 = y1 + y2 
      gen Y3 = Y2 + y3 
      gen Y4 = Y3 + y4 
      gen Y5 = 100 
      
      twoway bar y1 date, base(0) || rbar y1 Y2 date || rbar Y2 Y3 date || rbar Y3 Y4 date || rbar Y4 Y5 date, ///
      legend(order(5 "E" 4 "D" 3 "C" 2 "B" 1 "A") col(1) pos(3)) yla(, ang(h)) ytitle(% of total)

      Comment

      Working...
      X