Announcement

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

  • Stacked Bar Graph using Panel Data: Graph per country where Y axis = % stacked to 100% and X axis = year

    I have 3 variables on "% of population" data that all add up to a 100% per year per country. However, these values are only present every 5 years for some countries, and for some every 2 years or so. What command should I use so I can create a stacked bar graph wherein there is a separate graph per country and Y axis = % stacked to 100% and X axis = year?

  • #2
    I made up a data example in its absence.

    Here are two approaches of several. Many details are at choice.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 country float(year A B C)
    "Freedonia" 2002 10 50 40
    "Freedonia" 2004 12 48 40
    "Freedonia" 2006 14 46 40
    "Freedonia" 2008 16 44 40
    "Freedonia" 2010 18 42 40
    "Sylvania"  2000 25 30 45
    "Sylvania"  2005 20 30 50
    "Sylvania"  2010 15 30 55
    end
    
    capture set scheme stcolor 
    
    local myopts yla(0(25)100) ytitle(Percent of whatever) 
    
    graph bar (asis) A B C, stack nofill over(year) over(country) legend(order(3 2 1)) ///
    name(G1, replace) `myopts'
    
    gen AB = A + B 
    gen all = 100 
    
    twoway bar A year || rbar A AB year || rbar AB all year, ///
    by(country, note("")) legend(order(3 "C" 2 "B" 1 "A")) ytitle(Percent of whatever) ///
    `myopts' name(G2, replace) xtitle("")
    Click image for larger version

Name:	country1.png
Views:	1
Size:	30.5 KB
ID:	1775217
    Attached Files

    Comment


    • #3
      Thank you so much Nick. However, I have around 30+ countries to do it for so everything appears really small using the codes above.
      Is it possible to generate separate jpeg files for each country?

      Comment


      • #4
        Naturally: just loop over countries. https://www.stata.com/support/faqs/d...-with-foreach/

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str9 country float(year A B C)
        "Freedonia" 2002 10 50 40
        "Freedonia" 2004 12 48 40
        "Freedonia" 2006 14 46 40
        "Freedonia" 2008 16 44 40
        "Freedonia" 2010 18 42 40
        "Sylvania"  2000 25 30 45
        "Sylvania"  2005 20 30 50
        "Sylvania"  2010 15 30 55
        end  
        
        capture set scheme stcolor  
        local myopts yla(0(25)100) ytitle(Percent of whatever)  
        egen group = group(country), label  
        su group, meanonly  
        local max = r(max)  
        
        forval j = 1/`max' {  
             local this : label (group) `j'  
             graph bar (asis) A B C if group == `j', stack over(year) subtitle("`this'") legend(order(3 2 1)) ///
             name(G`j', replace) `myopts'  
        }
        Last edited by Nick Cox; 03 Apr 2025, 00:36.

        Comment


        • #5
          Creating a portfolio of 30+ different graphs will solve one problem only to create another. There are ways out of the maze.

          1. The stacked bar design for components of a total is popular but not so obvious as a means to convey information easily and effectively. Line graphs of the individual components are often as or more helpful.

          2. If 30 or more series are hard to handle, subsets of say 8 may be manageable. 4 graphs can fit on a page or slide.

          3. A front-and-back plot may be another way to see the details in context.

          Comment

          Working...
          X