Announcement

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

  • Bar chart for complete sample AND one subgroup

    Dear All,

    I don´t know if I will be able to describe my problem correctly, but I will give it a try.

    I am trying to produce a Bar chart with the following characteristics: First bars for the percentages of the category calculated for the complete sample, and right next to those, bars for the percentages of the category for just one subgroup of the sample. The resulting graph would have 6 bars, which would follow this order:

    Bar 1 - percentage of category 1 for whole sample,
    Bar 2 - percentage of category 1 for subgroup 1,

    Bar 3 - percentage of category 2 for whole sample,
    Bar 4 - percentage of category 2 for subgroup 1,

    Bar 5 - percentage of category 3 for whole sample,
    Bar 6 - percentage of category 3 for subgroup 1,

    I would also need to place the percentage labels on top of each bar. Can anyone help me? Sorry if the specification of my question is incorrect in any form.

    Best regards,

    Paolo Moncagatta

  • #2
    Without a data example or more information about what sort of variables you are working with I can only guess, but here's one method (which requires installing -statplot- from SSC):

    Code:
    clear
    set obs 1000
    g likert = int(1+runiform()*7)
    fre likert
    
    g subgroup = rbinomial(1, .4)
    
    
     ta likert, g(l_)
    
     ta likert,
     
    preserve
    collapse  l_*
    g subgroup = "All"
    sa one, replace
    restore
    
    collapse l_* if subgroup ==1
    g subgroup = "Group 1"
    
    append using one
    
    l
    
    **for percents
    
    foreach j of varlist l_? {
        replace `j' = `j' * 100
        lab var `j' `"Category `j'"'
        }
        
    
     //this doesnt sort correctly and I dont know how to reorder the bars easily....
    graph bar (mean) l_?,  over(subgroup)    blabel(bar, format(%3.1f) size(small) color(gs6)) ytit(Percent %)
     
     //so instead I use -statplot- from SSC
    statplot l_?, recast(bar) over(subgroup, label(labsize(tiny))) varopts(label(labsize(vsmall))) blabel(bar, format(%3.1f) size(small) color(gs6)) ytit(Percent %)
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Dear Eric,

      Thank you so much for your kind reply even if I was not able to provide a data example of my problem.
      Your solution was of great help. I only have one small additional issue which I haven´t been able to solve: how can I produce different coloured bars for the two different "subgroups"?

      Thanks again, best regards,

      Paolo Moncagatta

      Comment


      • #4
        In truth, what would be ideal for me, would be to find a way to use -statplot- but remove the subgroup labels (on the X axis) and use a legend instead. Is there a way to do this?
        Thanks so much in advance,

        Paolo

        Comment


        • #5
          Sure, change it to something like:


          Code:
          statplot l_?, recast(bar) over(subgroup,  label(labsize(zero))) varopts( label(labsize(vsmall))) blabel(bar, format(%3.1f) size(small) color(gs6)) ytit(Percent %)  /// 
          bargap(10)  asyvars showyvars ///
          bar(1, bfcolor(red) blw(thick))  ///
          bar(2, bcolor(dkgreen*.3))
          Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

          Comment


          • #6
            Excellent Eric, thank you so much!

            Paolo

            Comment

            Working...
            X