Announcement

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

  • Creating stacked bar charts

    Hi all,

    I'm using Stata to create a lot of charts which I've never used Stata for before, and I am struggling to create what I need (and I have read the stata guide etc such as https://www.stata.com/support/faqs/g...ked-bar-chart/).

    There are two charts which I'd like a hand with,

    1) A stacked bar chart with %s for multiple variables. So take variables x1 and x2, which are categorical variables which can take values 1-5. I'd like to create the chart which I have shown in the image below.

    Click image for larger version

Name:	stacked_bar.png
Views:	1
Size:	6.4 KB
ID:	1413983


    2) The second is the same type of chart, but assuming that x1 x2 were not separate variables, but different categories of another variable, say y (with values from 1 to 5).

    Any help much appreciated.

    Regards


  • #2
    Here is an answer to your question:

    Code:
    sysuse auto, clear
    
    contract rep78 foreign, nomiss
    bys foreign (rep78) : replace _freq = sum(_freq)
    bys foreign (rep78) : replace _freq = _freq/_freq[_N]*100
    
    twoway bar _freq foreign if rep78 == 5 , barw(.5) || ///
           bar _freq foreign if rep78 == 4 , barw(.5) || ///
           bar _freq foreign if rep78 == 3 , barw(.5) || ///
           bar _freq foreign if rep78 == 2 , barw(.5) || ///
           bar _freq foreign if rep78 == 1 , barw(.5)    ///
           legend(order(1 "Excellent"                    ///
                        2 "Good"                         ///
                        3 "Average"                      ///
                        4 "Fair"                         ///
                        5 "Poor"))                       ///
           ytitle(percent)                               ///
           xlab(0/1, val)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.0 KB
ID:	1414032


    Here is my preferred graph (this requires tabplot by Nick Cox, which you can download from the Stata Journal by typing in Stata search tabplot):

    Code:
    sysuse auto, clear       
    gen rrep78 = 6-rep78
    label define rrep78 1 "Excellent" ///
                        2"Good"      ///
                        3 "Average"   ///
                        4 "Fair"      ///
                        5 "Poor"
    label value rrep78 rrep78
    label var rrep78 "Repair record 1978"
    
    tabplot rrep78 foreign, percent(foreign) showval(format(%3.0f)) name(tab, replace)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.3 KB
ID:	1414033
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Originally posted by Maarten Buis View Post
      Here is an answer to your question:

      Code:
      sysuse auto, clear
      
      contract rep78 foreign, nomiss
      bys foreign (rep78) : replace _freq = sum(_freq)
      bys foreign (rep78) : replace _freq = _freq/_freq[_N]*100
      
      twoway bar _freq foreign if rep78 == 5 , barw(.5) || ///
      bar _freq foreign if rep78 == 4 , barw(.5) || ///
      bar _freq foreign if rep78 == 3 , barw(.5) || ///
      bar _freq foreign if rep78 == 2 , barw(.5) || ///
      bar _freq foreign if rep78 == 1 , barw(.5) ///
      legend(order(1 "Excellent" ///
      2 "Good" ///
      3 "Average" ///
      4 "Fair" ///
      5 "Poor")) ///
      ytitle(percent) ///
      xlab(0/1, val)
      [ATTACH=CONFIG]n1414032[/ATTACH]

      Here is my preferred graph (this requires tabplot by Nick Cox, which you can download from the Stata Journal by typing in Stata search tabplot):

      Code:
      sysuse auto, clear
      gen rrep78 = 6-rep78
      label define rrep78 1 "Excellent" ///
      2"Good" ///
      3 "Average" ///
      4 "Fair" ///
      5 "Poor"
      label value rrep78 rrep78
      label var rrep78 "Repair record 1978"
      
      tabplot rrep78 foreign, percent(foreign) showval(format(%3.0f)) name(tab, replace)
      [ATTACH=CONFIG]n1414033[/ATTACH]
      Thanks very much Maarten,

      I have used your first suggestion which is really neat!

      I have tried to modify the code to answer my 2nd question from the original post, of a bar for different variables (vbls x y z as diff bars), instead of different values of one variable (x1 x2 x3..). However I am having little luck, could you suggest how I would do this using your suggestion?

      Many thanks,
      Alex

      Comment


      • #4
        Alex: Please do read and act on the Statalist FAQ Advice which all members are asked to do before posting.

        http://www.statalist.org/forums/help#realnames explains our strong preference for full real names, typically given and family names.
        (This was also explained to you in detail two years ago: https://www.statalist.org/forums/for...not-usermonths)

        http://www.statalist.org/forums/help#stata explains that you should show a real(istic) data example and exactly what you tried as code and what went wrong.

        "I am having little luck" carries no information whatsoever about that.

        Maarten already had to work at thinking up an example and code. Please don't expect him or anyone else to do it again.

        Data example and code report please...
        Last edited by Nick Cox; 22 Oct 2017, 04:00.

        Comment

        Working...
        X