Announcement

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

  • stata: bar stacked percent graph

    Hello everyone!
    I don't know how to write code when I am trying to graph a stcked bar graph using the following data:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float group str6 wage1 float(mean_wage2 mean_wage3 mean_wage4)
    1 "230" 1.2 30 1000
    2 "180" 1.1 35 2000
    3 "290"  .9 25  300
    4 "405"   1 40  400
    end
    What I am trying to do is to draw a graph like this:
    ( it's a little ugly hhh)
    Click image for larger version

Name:	statalist.png
Views:	1
Size:	18.7 KB
ID:	1644282


    It'd be very nice if you could help me with this !


  • #2
    Code:
    destring(wage1), replace
    graph bar *wage* ,over(group) percentage stack

    Comment


    • #3
      The stacked design showing percents of a total is very popular despite major limitations, including

      1. It can be hard to see very small amounts, including zeros.

      2. It can be hard to compare amounts or percents for categories across bars, except the top and bottom categories which have a baseline above and below respectively.

      3. It is hard to show numeric or other annotation cleanly and clearly.

      4. A legend is usually required, which is at best a necessary evil.

      An alternative design is implemented in tabplot from the Stata Journal. A fairly quick but also slightly detailed overview can be seen at https://www.statalist.org/forums/for...updated-on-ssc

      Øyvind Snilsberg helpfully pointed out that you need to destring one of your variables before you can use it.

      This sample code goes a little beyond that.

      Code:
      clear
      input float group str6 wage1 float(mean_wage2 mean_wage3 mean_wage4)
      1 "230" 1.2 30 1000
      2 "180" 1.1 35 2000
      3 "290"  .9 25  300
      4 "405"   1 40  400
      end
      
      destring wage1, replace 
      rename mean_* * 
      reshape long wage, i(group) j(which)
      
      tabplot which group [iw=wage], showval scheme(s1color) blcolor(blue) bfcolor(blue*0.2) name(G1, replace) 
      
      
      tabplot which group [iw=wage], showval scheme(s1color) percent(group) blcolor(blue) bfcolor(blue*0.2) name(G2, replace) subtitle(% for group)

      Click image for larger version

Name:	G1.png
Views:	1
Size:	13.2 KB
ID:	1644293


      Click image for larger version

Name:	olivia_G2.png
Views:	1
Size:	13.5 KB
ID:	1644294


      Many details can be changed, including separate colours for row or column entries.

      This command was discussed at length in 2016 (see https://www.stata-journal.com/articl...article=gr0066 ), but if interested install the 2020 update.

      SJ-20-3 gr0066_2 . . . . . . . . . . . . . . . . Software update for tabplot
      (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
      Q3/20 SJ 20(3):757--758
      added new options frame() and frameopts() allowing framing
      of bars and so-called thermometer plots or charts

      SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
      (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
      Q3/17 SJ 17(3):779
      added options for reversing axis scales; improved handling of
      axis labels containing quotation marks

      SJ-16-2 gr0066 . . . . . . Speaking Stata: Multiple bar charts in table form
      (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
      Q2/16 SJ 16(2):491--510
      provides multiple bar charts in table form representing
      contingency tables for one, two, or three categorical variables



      Comment


      • #4
        Originally posted by Øyvind Snilsberg View Post
        Code:
        destring(wage1), replace
        graph bar *wage* ,over(group) percentage stack
        It works! Thanks a lot !

        Comment


        • #5
          Originally posted by Nick Cox View Post
          The stacked design showing percents of a total is very popular despite major limitations, including

          1. It can be hard to see very small amounts, including zeros.

          2. It can be hard to compare amounts or percents for categories across bars, except the top and bottom categories which have a baseline above and below respectively.

          3. It is hard to show numeric or other annotation cleanly and clearly.

          4. A legend is usually required, which is at best a necessary evil.

          An alternative design is implemented in tabplot from the Stata Journal. A fairly quick but also slightly detailed overview can be seen at https://www.statalist.org/forums/for...updated-on-ssc

          Øyvind Snilsberg helpfully pointed out that you need to destring one of your variables before you can use it.

          This sample code goes a little beyond that.

          Code:
          clear
          input float group str6 wage1 float(mean_wage2 mean_wage3 mean_wage4)
          1 "230" 1.2 30 1000
          2 "180" 1.1 35 2000
          3 "290" .9 25 300
          4 "405" 1 40 400
          end
          
          destring wage1, replace
          rename mean_* *
          reshape long wage, i(group) j(which)
          
          tabplot which group [iw=wage], showval scheme(s1color) blcolor(blue) bfcolor(blue*0.2) name(G1, replace)
          
          
          tabplot which group [iw=wage], showval scheme(s1color) percent(group) blcolor(blue) bfcolor(blue*0.2) name(G2, replace) subtitle(% for group)

          [ATTACH=CONFIG]n1644293[/ATTACH]

          [ATTACH=CONFIG]n1644294[/ATTACH]

          Many details can be changed, including separate colours for row or column entries.

          This command was discussed at length in 2016 (see https://www.stata-journal.com/articl...article=gr0066 ), but if interested install the 2020 update.

          SJ-20-3 gr0066_2 . . . . . . . . . . . . . . . . Software update for tabplot
          (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
          Q3/20 SJ 20(3):757--758
          added new options frame() and frameopts() allowing framing
          of bars and so-called thermometer plots or charts

          SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
          (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
          Q3/17 SJ 17(3):779
          added options for reversing axis scales; improved handling of
          axis labels containing quotation marks

          SJ-16-2 gr0066 . . . . . . Speaking Stata: Multiple bar charts in table form
          (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
          Q2/16 SJ 16(2):491--510
          provides multiple bar charts in table form representing
          contingency tables for one, two, or three categorical variables


          That is amazing.
          Thanks for your instuctive guide!

          Comment


          • #6
            Originally posted by Nick Cox View Post
            The stacked design showing percents of a total is very popular despite major limitations, including

            1. It can be hard to see very small amounts, including zeros.

            2. It can be hard to compare amounts or percents for categories across bars, except the top and bottom categories which have a baseline above and below respectively.

            3. It is hard to show numeric or other annotation cleanly and clearly.

            4. A legend is usually required, which is at best a necessary evil.

            An alternative design is implemented in tabplot from the Stata Journal. A fairly quick but also slightly detailed overview can be seen at https://www.statalist.org/forums/for...updated-on-ssc

            Øyvind Snilsberg helpfully pointed out that you need to destring one of your variables before you can use it.

            This sample code goes a little beyond that.

            Code:
            clear
            input float group str6 wage1 float(mean_wage2 mean_wage3 mean_wage4)
            1 "230" 1.2 30 1000
            2 "180" 1.1 35 2000
            3 "290" .9 25 300
            4 "405" 1 40 400
            end
            
            destring wage1, replace
            rename mean_* *
            reshape long wage, i(group) j(which)
            
            tabplot which group [iw=wage], showval scheme(s1color) blcolor(blue) bfcolor(blue*0.2) name(G1, replace)
            
            
            tabplot which group [iw=wage], showval scheme(s1color) percent(group) blcolor(blue) bfcolor(blue*0.2) name(G2, replace) subtitle(% for group)

            [ATTACH=CONFIG]n1644293[/ATTACH]

            [ATTACH=CONFIG]n1644294[/ATTACH]

            Many details can be changed, including separate colours for row or column entries.

            This command was discussed at length in 2016 (see https://www.stata-journal.com/articl...article=gr0066 ), but if interested install the 2020 update.

            SJ-20-3 gr0066_2 . . . . . . . . . . . . . . . . Software update for tabplot
            (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
            Q3/20 SJ 20(3):757--758
            added new options frame() and frameopts() allowing framing
            of bars and so-called thermometer plots or charts

            SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
            (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
            Q3/17 SJ 17(3):779
            added options for reversing axis scales; improved handling of
            axis labels containing quotation marks

            SJ-16-2 gr0066 . . . . . . Speaking Stata: Multiple bar charts in table form
            (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
            Q2/16 SJ 16(2):491--510
            provides multiple bar charts in table form representing
            contingency tables for one, two, or three categorical variables


            Could you please tell me what to do to keep the decimal places that I want? Thanks ^-^

            Comment


            • #7
              What are the "decimal places that I want"? A display format can be specified along with showval().

              Comment

              Working...
              X