Announcement

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

  • Twoway bar overlays bars...

    Hi!

    I'm working in Stata 15 trying to create a twoway bar graph with the prevalence of obesity on the y-axis and agegroups on X-axis. The problem arises when I would like to have four different years per age group. As you can see from image - the bars overlap such that 2017 hides the other years.



    Click image for larger version

Name:	Graph.png
Views:	1
Size:	87.2 KB
ID:	1507233

    My syntax looks like below. I have also tried syntax from this forum, but the same issue occurs.

    Code:
    twoway (bar PropOB100 Age if year==1999, bcolor(maroon) barw(0.85)) ///
           (bar PropOB100 Age if year==2005, bcolor(green) barw(0.85)) ///
           (bar PropOB100 Age if year==2011, bcolor(orange) barw(0.85)) ///
           (bar PropOB100 Age if year==2017, bcolor(ltblue) barw(0.85)) ///
           (rcap UL_PropOB100 LL_PropOB100 Age), ///
           legend(order(1 "1999" 2 "2005" 3 "2011" 4 "2017") rows(1)) ///
           xlabel( 1 "18-24" 2 "25-29" 3 "30-34" 4 "35-39" 5 "40-44" 6 "45-49" 7 "50-54" 8 "55-59" 9 "60-64" 10 "65-69" 11 "70+") ///
           xtitle("Age") ///
           ylabel(10 15 20 25 30 35 40, nogrid) yscale(range(10 40)) ///
           ytitle("Prevalence of Obesity (%)")
    Kind regards
    Jannie

    Data looks like below:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float Age int year float(PropOB100 UL_PropOB100 LL_PropOB100)
     1 1999  13.17658  21.05773  7.948086
     1 2005 11.623165  15.07769  8.877385
     1 2011 11.668415  16.13067  8.318133
     1 2017 22.425257   30.4586 16.022507
     2 1999  19.39137  25.26686 14.614967
     2 2005 21.315594 26.809494 16.690784
     2 2011  25.78692 29.398335 22.477903
     2 2017  20.97302 25.118103 17.353456
     3 1999 23.135847  29.03269 18.130724
     3 2005  28.99342 34.968296  23.66789
     3 2011 26.953384  30.90038 23.340256
     3 2017 26.911236 31.977936  22.38316
     4 1999 19.963625  24.04661 16.423998
     4 2005  30.60505 35.755623  25.89733
     4 2011 31.232634 36.082886  26.76138
     4 2017 34.455215  39.22816  29.97655
     5 1999  20.86615  25.44563 16.923758
     5 2005 25.207075  28.99692  21.76075
     5 2011  33.73074  38.65309  29.13751
     5 2017   36.5879   41.3974  32.03175
     6 1999  19.79532 24.334633  15.92455
     6 2005  25.62487 29.749763  21.89367
     6 2011 31.366955 37.132996  26.12421
     6 2017  38.81377  45.70695 32.340916
     7 1999 24.086357 28.284763  20.33443
     7 2005  24.31568  29.64821 19.674046
     7 2011  28.43865   32.9217  24.34451
     7 2017 35.051098  40.99437  29.53813
     8 1999 23.921154  30.39058  18.46362
     8 2005  26.60072  31.81101 21.968874
     8 2011 32.129158 36.176197  28.33387
     8 2017 33.545105  38.55017 28.884417
     9 1999  25.58697   33.5093 19.002365
     9 2005  28.75425 35.585022  22.77117
     9 2011  28.82813  35.00919  23.34635
     9 2017  33.55486  39.09665  28.43188
    10 1999 18.511532 24.576674 13.671854
    10 2005   25.4658 34.930893 17.861412
    10 2011  30.76999 35.505802 26.407217
    10 2017  27.48402 34.345863  21.54324
    11 1999 14.925941 19.391964 11.343714
    11 2005 15.567016  19.01545 12.646297
    11 2011 18.220095  22.78926 14.396216
    11 2017  22.37479   26.9338  18.39321
    end
    label values Age agecat
    label def agecat 1 "18-24", modify
    label def agecat 2 "25-29", modify
    label def agecat 3 "30-34", modify
    label def agecat 4 "35-39", modify
    label def agecat 5 "40-44", modify
    label def agecat 6 "45-49", modify
    label def agecat 7 "50-54", modify
    label def agecat 8 "55-59", modify
    label def agecat 9 "60-64", modify
    label def agecat 10 "65-69", modify
    label def agecat 11 "70+", modify

  • #2
    I think you could just use graph bar over year and over age instead of overlapping twoway bars. Is this what you are looking for?
    Code:
    graph bar PropOB100, over(year) asyvars over(Age) bar(1, color(maroon)) bar(2, color(green))  bar(3, color(orange)) bar(4, color(ltblue)) legend(rows(1))

    Comment


    • #3
      Thanks for the suggestion!
      But can I still add CIs using graph bar? (called UL_PropOB100 LL_PropOB100).
      Best,
      Jannie

      Comment


      • #4
        No; graph bar is a dead end for showing confidence intervals too.

        With your design, even if you use transparency

        1. Change with time (surely a major feature of interest?) is not very clear.

        2, Bars don't start at zero or any meaningful base, often thought a very bad idea.

        3. A melange of colours is at best distracting,

        Here is my suggestion:


        Code:
        egen Year = group(year), label 
        
        set scheme s1color 
        
        scatter PropOB100 Year, mc(blue) by(Age, row(2) note("") legend(off) subtitle("Age group", pos(12))) ///
        || rcap UL_PropOB100 LL_PropOB100 Year, lc(blue) xla(, valuelabel) ///
        ylabel(10(5)45, ang(h)) ytitle("Prevalence of Obesity (%)")  xtitle("") ///
        subtitle(, fcolor(ltblue*0.2))
        Click image for larger version

Name:	obesity_age.png
Views:	1
Size:	44.2 KB
ID:	1507252

        Comment


        • #5

          Sorry for my suggestion in #2, I didn't take into account that you wanted to include error bars.

          Although I share Nick's preference for clean graphs and agree with the point made that the change over time is not clear in the bar graph you aim for, I think it is still valuable to post a workaround. The clue is to create a new variable for the x-axis which combines year and age, with a gap of one in between the age groups, see also https://stats.idre.ucla.edu/stata/fa...th-error-bars/. Using the data example above:
          Code:
          egen year1 = group(year)
          gen ageyear = year1 if Age==1
          replace ageyear = year1+5 if Age==2
          replace ageyear = year1+10 if Age==3
          replace ageyear = year1+15 if Age==4
          replace ageyear = year1+20 if Age==5
          replace ageyear = year1+25 if Age==6
          replace ageyear = year1+30 if Age==7
          replace ageyear = year1+35 if Age==8
          replace ageyear = year1+40 if Age==9
          replace ageyear = year1+45 if Age==10
          replace ageyear = year1+50 if Age==11
          
          twoway (bar PropOB100 ageyear if year==1999, bcolor(maroon) barw(0.85)) ///
                 (bar PropOB100 ageyear if year==2005, bcolor(green)  barw(0.85)) ///
                 (bar PropOB100 ageyear if year==2011, bcolor(orange) barw(0.85)) ///
                 (bar PropOB100 ageyear if year==2017, bcolor(ltblue) barw(0.85)) ///
                 (rcap UL_PropOB100 LL_PropOB100 ageyear), ///
                 legend(row(1) order(1 "1999" 2 "2005" 3 "2011" 4 "2017") ) ///
                 xlabel( 2.5 "18-24" 7.5 "25-29" 12.5 "30-34" 17.5 "35-39" 22.5 "40-44" ///
                 27.5 "45-49" 32.5 "50-54" 37.5 "55-59" 42.5 "60-64" 47.5 "65-69" 52.5 "70+", noticks) ///
                 xtitle("Age") ytitle("Prevalence of Obesity (%)") ///
                 ylabel(10 15 20 25 30 35 40, nogrid) yscale(range(10 40))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	83.3 KB
ID:	1507315

          Comment


          • #6
            It's Jannie's graph to draw, but @Wouter is right that offsets can be used to put everything in one panel. I can't buy the bars starting at 8% or whatever, or the hard to spot lower confidence limits, or the colour melange, but I build on the offset idea to suggest the code below. In passing, note that

            Code:
            egen year1 = group(year)
            gen ageyear = year1 if Age==1
            replace ageyear = year1+5 if Age==2
            replace ageyear = year1+10 if Age==3
            replace ageyear = year1+15 if Age==4
            replace ageyear = year1+20 if Age==5
            replace ageyear = year1+25 if Age==6
            replace ageyear = year1+30 if Age==7
            replace ageyear = year1+35 if Age==8
            replace ageyear = year1+40 if Age==9
            replace ageyear = year1+45 if Age==10
            replace ageyear = year1+50 if Age==11

            reduces to

            Code:
            egen year1 = group(year)
            gen ageyear = year1 + 5 * (Age - 1)
            Here is my second suggestion. Given infinite time, I might play with the marker size. Artists aren't impartial, but I still prefer the graph in #4.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float Age int year float(PropOB100 UL_PropOB100 LL_PropOB100)
             1 1999  13.17658  21.05773  7.948086
             1 2005 11.623165  15.07769  8.877385
             1 2011 11.668415  16.13067  8.318133
             1 2017 22.425257   30.4586 16.022507
             2 1999  19.39137  25.26686 14.614967
             2 2005 21.315594 26.809494 16.690784
             2 2011  25.78692 29.398335 22.477903
             2 2017  20.97302 25.118103 17.353456
             3 1999 23.135847  29.03269 18.130724
             3 2005  28.99342 34.968296  23.66789
             3 2011 26.953384  30.90038 23.340256
             3 2017 26.911236 31.977936  22.38316
             4 1999 19.963625  24.04661 16.423998
             4 2005  30.60505 35.755623  25.89733
             4 2011 31.232634 36.082886  26.76138
             4 2017 34.455215  39.22816  29.97655
             5 1999  20.86615  25.44563 16.923758
             5 2005 25.207075  28.99692  21.76075
             5 2011  33.73074  38.65309  29.13751
             5 2017   36.5879   41.3974  32.03175
             6 1999  19.79532 24.334633  15.92455
             6 2005  25.62487 29.749763  21.89367
             6 2011 31.366955 37.132996  26.12421
             6 2017  38.81377  45.70695 32.340916
             7 1999 24.086357 28.284763  20.33443
             7 2005  24.31568  29.64821 19.674046
             7 2011  28.43865   32.9217  24.34451
             7 2017 35.051098  40.99437  29.53813
             8 1999 23.921154  30.39058  18.46362
             8 2005  26.60072  31.81101 21.968874
             8 2011 32.129158 36.176197  28.33387
             8 2017 33.545105  38.55017 28.884417
             9 1999  25.58697   33.5093 19.002365
             9 2005  28.75425 35.585022  22.77117
             9 2011  28.82813  35.00919  23.34635
             9 2017  33.55486  39.09665  28.43188
            10 1999 18.511532 24.576674 13.671854
            10 2005   25.4658 34.930893 17.861412
            10 2011  30.76999 35.505802 26.407217
            10 2017  27.48402 34.345863  21.54324
            11 1999 14.925941 19.391964 11.343714
            11 2005 15.567016  19.01545 12.646297
            11 2011 18.220095  22.78926 14.396216
            11 2017  22.37479   26.9338  18.39321
            end
            label values Age agecat
            label def agecat 1 "18-24", modify
            label def agecat 2 "25-29", modify
            label def agecat 3 "30-34", modify
            label def agecat 4 "35-39", modify
            label def agecat 5 "40-44", modify
            label def agecat 6 "45-49", modify
            label def agecat 7 "50-54", modify
            label def agecat 8 "55-59", modify
            label def agecat 9 "60-64", modify
            label def agecat 10 "65-69", modify
            label def agecat 11 "70+", modify
            
            egen Year = group(year), label
            
            set scheme s1color
            
            egen year1 = group(year)
            clonevar ageyear = Age
            replace ageyear = ageyear + (year1 - 2.5) / 6
            
            scatter PropOB100 ageyear, mc(blue)  ///
            || rcap UL_PropOB100 LL_PropOB100 ageyear, lc(blue) xla(, valuelabel) ///
            ylabel(10(5)45, ang(h)) ytitle("Prevalence of Obesity (%)")  xtitle("Age group") ///
            xla(1/11, tlength(0) valuelabel)  xtick(1.5(1)10.5, grid glc(gs8) tlength(0)) ///
            legend(off) aspect(0.5) subtitle(years 1999 2005 2011 2017)
            Click image for larger version

Name:	age_obesity.png
Views:	1
Size:	35.3 KB
ID:	1507333

            Comment


            • #7
              Thanks for both of your suggestions! I think they both look very good. Will see what looks better once I add the figure to manuscript!
              Kind regards
              Jannie

              Comment

              Working...
              X