Announcement

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

  • Bar chart multiple categories

    Dear all,
    I have a query regarding the bar chart command in STATA. I have been trying to reproduce a graph similar to the one I have created in excel (attached). I have tried several commands using Stata but I have not came close to the result I am after. The best farthest I have gone is by using the following command: graph hbar, over( prep_ph ) by(gender) bargap(-50) nofill

    The data I need to capture in a bar chart are reported in the table below. Not sure it is possible with STATA though.

    If anyone has any suggestion it would be very appreciated. Thanks, Elena
    tot male female
    Preparation phase (%) mostly men 17.1 25 11.8
    Mostly women 78.7 72.6 82.8
    Equal number 4.2 2.4 5.4
    Maintenance phase (%) mostly men 95.5 97.6 94.0
    Mostly women 3.2 2.4 3.8
    Equal number 1.3 0 2.2
    Harvesting phase (%) mostly men 93.2 94.3 92.4
    Mostly women 5.5 5.7 5.4
    Equal number 1.3 0 2.2
    Attached Files

  • #2
    I am going to suggest something different. In the absence of a data example I worked to get this version. Your variable names are presumably different and you may not need labmask from the Stata Journal. You will need tabplot from the Stata Journal -- or some programming of your own -- to get this graph easily.

    The slogans include

    Lose the legend! Kill the key! (if you can) Legends obliging mental back and forth are at best a necessary evil.

    Show numbers directly so that they are accessible too. Graphs can have table flavour too in a way that is positive and not too busy.

    Make bars with small values easy to see! They can be interesting or important detail.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str17 var1 str12 var2 float toshow
    "Preparation phase" "mostly men"   17.1
    "Preparation phase" "mostly women" 78.7
    "Preparation phase" "equal number"  4.2
    "Maintenamce phase" "mostly men"   95.5
    "Maintenamce phase" "mostly women"  3.2
    "Maintenamce phase" "equal number"  1.3
    "Harvesting phase"  "mostly men"   93.2
    "Harvesting phase"  "mostly women"  5.5
    "Harvesting phase"  "equal number"  1.3
    end
    
    egen phase = seq(), block(3)
    egen which = seq(), to(3)
    
    labmask phase, values(var1)
    labmask which, values(var2)
    
    tabplot which phase [iw=toshow], showval scheme(s1color) xtitle("") ytitle("Gender balance") separate(which) bar1(color(blue)) bar2(color(orange)) bar3(color(black)) subtitle(percent)



    Click image for larger version

Name:	gender_phase.png
Views:	2
Size:	18.9 KB
ID:	1649406


    This is just a two-way version and you can get a three-way version with a by() option, but I drew the line at entering all the data.

    More detail at https://www.statalist.org/forums/for...updated-on-ssc

    and https://www.stata-journal.com/articl...article=gr0066
    Attached Files

    Comment


    • #3
      To reproduce the graph using graph bar, you need a long data layout. The categories are ordered alphabetically, so see Nick's recommendation of labmask to get a different ordering. All the same, I completely agree with Nick that you can do better than this design with the legend.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str21(which description) double(tot male female)
      "Preparation phase (%)" "mostly men"   17.1   25 11.8
      "Preparation phase (%)" "Mostly women" 78.7 72.6 82.8
      "Preparation phase (%)" "Equal number"  4.2  2.4  5.4
      "Maintenance phase (%)" "mostly men"   95.5 97.6   94
      "Maintenance phase (%)" "Mostly women"  3.2  2.4  3.8
      "Maintenance phase (%)" "Equal number"  1.3    0  2.2
      "Harvesting phase (%)"  "mostly men"   93.2 94.3 92.4
      "Harvesting phase (%)"  "Mostly women"  5.5  5.7  5.4
      "Harvesting phase (%)"  "Equal number"  1.3    0  2.2
      end
      
      rename (tot male female) var=
      reshape long var, i( which description ) j(cat) string
      gr bar var, over(desc) over(cat) by(which, row(1) note("") ) asyvars ///
      ytitle(Percent) bar(1, color(navy%33)) bar(2, color(navy%67)) ///
      bar(3, color(navy%99)) leg(row(1)) blab(total) scheme(s1mono)
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	46.8 KB
ID:	1649410

      Comment


      • #4
        Thank you Nick, that is exactly what I was looking for, it's perfect! If you do not mind I have an additional question. Can I do the same but showing mean values and standard deviation instead of using percentages?
        Elena

        Comment


        • #5
          #4 Sure there are ways to plot means and SDs, but we need a data example please.

          Here is a three-way version of #2, building on Andrew Musau's work.

          Code:
          clear
          input str21(which description) double(total male female)
          "Preparation phase" "mostly men"   17.1   25 11.8
          "Preparation phase" "mostly women" 78.7 72.6 82.8
          "Preparation phase" "equal number"  4.2  2.4  5.4
          "Maintenance phase" "mostly men"   95.5 97.6   94
          "Maintenance phase" "mostly women"  3.2  2.4  3.8
          "Maintenance phase" "equal number"  1.3    0  2.2
          "Harvesting phase"  "mostly men"   93.2 94.3 92.4
          "Harvesting phase"  "mostly women"  5.5  5.7  5.4
          "Harvesting phase"  "equal number"  1.3    0  2.2
          end
          
          egen phase = seq(), block(3) 
          labmask phase, values(which)
          egen desc = seq(), to(3) 
          labmask desc, values(description) 
          
          rename (total male female) toshow= 
          reshape long toshow, i(which description) j(flavour) string 
          
          tabplot desc flavour [iw=toshow], by(phase, row(1) note("%")) showval scheme(s1color) xtitle("Whatever explains this axis") ytitle("Gender balance") separate(desc) bar1(color(blue)) bar2(color(orange)) bar3(color(black))
          Click image for larger version

Name:	gender_phase2.png
Views:	1
Size:	25.1 KB
ID:	1649427

          Comment

          Working...
          X