Announcement

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

  • Order of bars in -gr bar-

    I am trying to reorder the bars in -gr hbar-, and what I'm trying from the documentation is not working for me. Here is an example using our favorite dataset:

    Code:
    sysuse auto, clear
    
    *To create a string variable:
    gen temp=substr(make,1,2)
    
    * just to reduce the number of bars:
    keep if temp<"D"
    
    * this works fine:
    gr hbar (count), over(temp)
    
    * suppose I want the "B" bars first.
    * this is what the helpfile says to do if you want them in a different order:
    gen order=1 if temp=="BM"
    replace order=2 if temp=="Bu"
    replace order=3 if temp=="AM"
    replace order=4 if temp=="Au"
    replace order=5 if temp=="Ca"
    replace order=6 if temp=="Ch"
    
    gr hbar (count), over(temp, sort(order))
    Error message:
    "variable mean not found"

    Can someone suggest how I can obtain the bars in the order I'd like?

    Many thanks,
    Laura

  • #2
    That looks like a bug in the official code to me.

    Meanwhile, this is one way to do it:

    Code:
     
    ssc inst catplot 
    catplot temp, var1opts(sort(order))

    Comment


    • #3
      Thank you, Nick! I knew you'd know what to do.

      Comment


      • #4
        This would work too:

        Code:
         
        gen one = 1 
        
        gr hbar (count) one, over(temp, sort(order))

        Comment


        • #5
          In co-reference to a post on the list dated 21 Aug 2011, subject "Advanced features for bar chart and histogram in Stata", may I suggest the use of the transcendent -egen- command.

          I wanted to highlight the average value for two years for a group of countries, along with the -gr hbar- plot of the separate country values.

          An excerpt of the code I came up with, thanks to this - and the above mentioned post.

          This generates a separate variable for the group average, and a variable for the rest of the values for each year:

          Code:
          separate [a], by(x==[value for the group average]) veryshortlabel
          separate [b], by(x==[value for the group average]) veryshortlabel
          Re-iterating, this will generate the following variables
          • [a0] : all values for year 1, apart from the group average
          • [b0] : all values for year 2, apart from the group average
          • [a1] : the group average for year 1
          • [b1] : the group average for year 2
          I wanted the bars to be sorted by the values of year 2, so I generate the order variable like this:

          Code:
          egen order = group(b)
          And subsequently, I plotted the graph:

          Code:
          gr hbar a0 a1 b0 b1,over(x, sort(order)) ///
          bar(1, color(bluishgray)) bar(2, color(navy)) bar(3, color(orange_red)) bar(4, color(red))

          Comment

          Working...
          X