Announcement

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

  • Help on Bar Chart

    Hi,

    I am trying to plot a bar chart. The objective is to add the group mean (group_mean in the data example) as a separate bar to the bar chart. I am not able to do it. Any help in this regard would be greatly appreciated.

    Data example is given below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    
    clear
    input str2 stcode double revenue_receipts2016 float group_mean
    "AP" 98984.48640000001 104597.78
    "AS" 49219.80470000001 104597.78
    "BR"    105584.9852855 104597.78
    "CT"        53685.2495 104597.78
    "GJ"       109841.8115 104597.78
    "HR" 52496.81762860001 104597.78
    "JH" 47053.93024569999 104597.78
    "KA"       133213.7889 104597.78
    "KL" 75611.72177560002 104597.78
    "MH"       204693.1427 104597.78
    "MP"       123306.7901 104597.78
    "OD"        74299.3894 104597.78
    "PB"         47985.418 104597.78
    "RJ"       109025.9969 104597.78
    "TN"       140231.1346 104597.78
    "TS" 82817.95989999999 104597.78
    "UP"       256875.1511 104597.78
    "WB"    117832.4503021 104597.78
    end

    This is the syntax I am using:


    Code:
    graph bar (asis) revenue_receipts2016 , over(stcode, sort(1))  blabel(bar, orientation(vertical)) title()
    Thank you in advance.

  • #2
    Try to use @Nick Cox's Stata Tip: https://journals.sagepub.com/doi/pdf...867X1101100310
    Code:
    insobs 1
    replace stcode="Mean" in l
    replace revenue_receipts2016=104597.8 in l
    gen sepa_it=1 in l
    replace sepa_it=0 if sepa_it==.
    separate revenue_receipts2016, by(sepa_it)
    graph bar (asis) revenue_receipts20160 revenue_receipts20161, over(stcode, sort(1)) blabel(bar, orientation(vertical)) title() legend(label(1 "Group_mean") label(2 "Revenue_receipts2016")) nofill
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	84.5 KB
ID:	1697275

    Comment


    • #3
      Note that in codes from 2 to 4 lines, the "l" is not number "1" but lower character of "L" which means "last".
      Sorry in the attached graph above, the legend label was wrongly assigned, navy color label should be Revenue_receipts2016, and maroon color label should be Group_mean.
      If you want to put the Mean bar into other bars:
      Code:
      graph bar (asis) revenue_receipts20160 revenue_receipts20161, over(stcode, sort(revenue_receipts2016)) blabel(bar, orientation(vertical)) title() legend(label(2 "Group_mean") label(1 "Revenue_receipts2016")) nofill
      Hope that there are more easy solutions.
      Last edited by Chen Samulsion; 14 Jan 2023, 09:08.

      Comment


      • #4
        Chen Samulsion gave great help. I'd recommend just showing a vertical line at the mean level in this case.

        Comment


        • #5
          Thank you, Chen Samulsion, for providing the solution. That works fine for me.
          Nick Cox, Yes, that would be better. But the Department wants a separate bar for the group mean.

          And among the states, two bars have to be highlighted separately; namely, OD and the group mean. I use the following code to colour the bar for the "OD" state.

          Code:
          separate revenue_receipts2016, by(stcode  == "OD")
          graph bar (asis) revenue_receipts20160 revenue_receipts20161, nofill over(stcode, sort(1))  blabel(bar, orientation(vertical)) title()
          But the code Chen Samulsion uses
          Code:
          separate revenue_receipts2016, by(sepa_it)
          creates two identical variables, namely, revenue_receipts20160 and revenue_receipts20161, which the previous code already created. But these can be renamed.
          Thus, we have four variables. However, when I am plotting in Stata, it produces an entirely different chart.

          Is there any way to get around this problem?

          Thank you.


          Comment


          • #6
            If you want to highlight the mean and also OD distinctly, then the argument to separate must yield three distinct values. Some sample code.

            Code:
            insobs 1
            replace stcode="Mean" in L 
            replace revenue_receipts2016=104597.8 in L 
            gen sepa_it = cond(_n == _N, 2, stcode == "OD") 
            
            separate revenue_receipts2016, by(sepa_it)
            graph hbar (asis) revenue_receipts2016?, over(stcode, sort(revenue_receipts2016)) blabel(bar)  title() legend(off)  nofill ysc(r(0 275000))

            Comment


            • #7
              Thank you, Nick Cox. That worked fine.

              Comment

              Working...
              X