Announcement

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

  • Stacked Bar Graph

    Hello,

    I am in need of some assistance. I am trying to create a stacked bar graph to observe the expenditure per age group while also observing what kind of health coverage they have. I have attached my current bar graph and code. I would like to take out the zero or figure out an easier way to do this. Any help is appreciated.

    Code: graph bar (mean) TotalEXP [pweight = weight], over(Medicare) over(Medicaid) over(Age) stack blabel(bar) title(Figure 1: Average Health Expenditures by Age)










    Click image for larger version

Name:	Screen Shot 2021-12-18 at 5.41.59 PM.png
Views:	1
Size:	183.8 KB
ID:	1641499



  • #2
    Seems your sample is divided into four groups by the status of Medicare and Medicaid, and you'd like to show how the health expenditure of the four groups changes with age. Below is an example plotting average wage by age, sample being divided into four groups by unionization (non-union vs union) and education (non-college vs college). I first stack all four wages, but don' think it's a good solution as it's inconvenient to compare among the four. Then, I plot lines by the four groups and information there seems much clearer.

    Code:
        sysuse nlsw88, clear
    
    * Similar to your figure
        graph bar (mean) wage, over(collgrad) over(union) over(age) stack blabel(bar)
        
    * Rearrange data
        collapse wage if !mi(union), by(collgrad union age)
        egen j = group(union collgrad)
        drop union collgrad
        reshape wide wage, i(age) j(j)
        drop if age == 46
    
        graph bar (mean) wage1 wage2 wage3 wage4, over(age) stack    ///
        leg(order(1 "non-union, non-college" 2 "non-union, college" 3 "union, non-collge" 4 "union, collge"))
        
        twoway line wage1 age, lc(maroon) lp(dash)  || line wage2 age, lc(navy) lp(dash) || ///
               line wage3 age, lc(maroon) lp(solid) || line wage4 age, lc(navy) lp(solid)    ///
               leg(order(1 "non-union, non-college" 2 "non-union, college" 3 "union, non-collge" 4 "union, collge"))

    Comment


    • #3
      Fei Wang gives a very helpful answer, and I would go further. The excuse for stacking can only be that whatever is stacked is additive, so that the sums make sense. So the frequencies of people in and of people not in a scheme do add to a total, but their means on some other variable do not. add to anything with independent meaning.

      In this case the spike at 65 needs some kind of story.

      Comment


      • #4
        Fei Wang Nick Cox Thank you both for the reply. Fei, I have ran your recommended code to my data and it fits well. This will definitely prove useful. However, I would still like to replicate the results using a similar stacked bar chart to the one above. Ideally, I want respondents who only said yes (being binary 1) to having Medicaid and Medicare. Those who said no (binary zero), for now, are not of interest. In other words, I want my stack to only show expenditure for those who are yes to Medicare Medicaid by age.

        Comment


        • #5
          So, is

          Code:
          graph bar (mean) TotalEXP [pweight = weight] if Medicare == 1 & Medicaid == 1, over(Age)  blabel(bar)
          more like what you want?

          Comment


          • #6
            Nick Cox Thank you for the reply. I attached what the code produced vs what I am trying to achieve. Let me know if it makes sense. Thank you in advance for taking the time.
            Click image for larger version

Name:	Screen Shot 2021-12-19 at 6.39.32 PM.png
Views:	1
Size:	97.8 KB
ID:	1641595
            Click image for larger version

Name:	IMG_0231.jpg
Views:	1
Size:	209.9 KB
ID:	1641596

            Comment


            • #7
              The fastest way to get help is to present a data example using dataex as outlined in FAQ Advice #12. You want to have the expenditures of each of these categories by age. See -help collapse- for a way to do this if your data is disaggregated.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float(age medicare medicaid)
              60  6750 2100
              61  3325 4000
              62  6250 4500
              63  6000 1250
              64 10000 5500
              65  6000 7500
              end
              set scheme s1mono
              gr bar medicare medicaid, over(age) stack bar(1, color(red)) bar(2, color(blue)) title("Average health expenditures by age") leg(order(1 "Medicare" 2 "Medicaid"))
              Click image for larger version

Name:	Graph.png
Views:	1
Size:	29.3 KB
ID:	1641604

              Last edited by Andrew Musau; 19 Dec 2021, 19:20.

              Comment


              • #8
                I don't live or work in the United States. I gather that
                • Medicare is a federal program that provides health coverage if you are 65+ or under 65 and have a disability, no matter your income.
                • Medicaid is a state and federal program that provides health coverage if you have a very low income.
                As someone can be 1 on Medicare and 1 on Medicaid. If so, then I cannot follow how it makes no sense to treat expenditure under Medicare and Medicaid as disjoint and additive, as a stacked bar as your design implies.

                Comment


                • #9
                  Thank you both for the feedback.

                  Comment


                  • #10
                    ... I cannot follow how it makes sense ...

                    Comment

                    Working...
                    X