Announcement

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

  • Question about stacked bar graphs for multiple variables

    Hello. I have trouble generating a bar graph for the following:

    I have the variables A, B, C, D, E

    A, B, C, D, E can all only take a value from the set: (0, 1, 2, 3, 4)

    I would like to make a bar graph where I can display, as stacked, the following:

    1. The variables on the Y axis
    2. The percent of frequencies of the values in X axis
    3. And the frequency of the values represented as bars connected to each other (though as different segments) for each variable

    So in the most crude fashion the graph I have in mind would look like the following:


    Y (axis)

    A 0000111223333444
    B 0011112222333444
    C 0011112233444444
    D 0000011223344444
    E 0000011111222334

    0 20 40 60 80 100 (Percent, X axis)

    Help please?

    Thank you very much!


  • #2
    You'd get most flexibility by restructuring your data. Here I recast your example data (please read the FAQ Advice to learn about dataex (SSC)) .

    To use tabplot you would need to install it first. See e.g. http://www.statalist.org/forums/forum/general-stata-discussion/general/1335274-tabplot-updated-on-ssc

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(A B C D E)
    0 0 0 0 0
    0 0 0 0 0
    0 1 1 0 0
    0 1 1 0 0
    1 1 1 0 0
    1 1 1 1 1
    1 2 2 1 1
    2 2 2 2 1
    2 2 3 2 1
    3 2 3 3 1
    3 3 4 3 2
    3 3 4 4 2
    3 3 4 4 2
    4 4 4 4 3
    4 4 4 4 3
    4 4 4 4 4
    end
    
    gen id = _n
    rename (A-E) y=
    reshape long y, i(id) j(which) string
    set scheme s1color
    
    graph hbar (count), over(y)  over(which) asyvars stack
    more 
    
    tabplot y which , yasis ytitle(values) xtitle(variables) showval bfcolor(none)
    more 
    
    histogram y, by(which, note("") row(1)) freq discrete xla(0/4) yla(, ang(h)) xtitle("")

    Comment


    • #3
      Hi Nick Cox

      I have a similar scenario. I only need two things different. One is that I need percentages instead of counts. I change "count" for percentage" in yor command but the Y range only from 0 to 10 values (this should be up to 100%) and some variables have values above 10. The whole bar should be up to 100% but is not. The second thing is that I have an extra variables (cross tabulation) which is Gender(male and female). I would like to incorporate this gender variable in the graph. I would like the 2 bars for the each of the y variable - 1 bar is male and the other female- one next to the other. I attached an example of what I want ( I did this in excel) but this graph is not perfect. the gender values are two separate. I want them to be close to each other.

      I would greatly appreciate your help.
      Attached Files

      Comment


      • #4
        Sorry, but Word documents (a) are explicitly advised against in the FAQ Advice (b) are at this point unreadable by me.

        The implication is that any reply will have to come from someone else able and willing to read that document.
        Last edited by Nick Cox; 05 Oct 2016, 09:46.

        Comment


        • #5
          I am sorry about that Nick. please see attached. Should I open a new treat?
          Attached Files

          Comment


          • #6
            OK, thanks.

            I can see your graph but I can't see sample data. After almost 200 posts you should (please) be familiar with FAQ Advice by now...

            I see no need for a new thread here.

            Comment


            • #7
              Hi Nick,

              Sorry about that! I was referring to the data you see in #2. Below you can see the data with the extra variable gender. I am also including the code you wrote to create the graph. Again I just need the the bar graphs by gender. Thank you! Hopefully I am not forgetting anything.



              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float(A B C D E gender)
              0 0 0 0 0 1
              0 0 0 0 0 0
              0 1 1 0 0 0
              0 1 1 0 0 0
              1 1 1 0 0 1
              1 1 1 1 1 1
              1 2 2 1 1 1
              2 2 2 2 1 1
              2 2 3 2 1 1
              3 2 3 3 1 0
              3 3 4 3 2 0
              3 3 4 4 2 1
              3 3 4 4 2 1
              4 4 4 4 3 1
              4 4 4 4 3 0
              4 4 4 4 4 1
              end
              label values gender gender
              label def gender 0 "Female", modify
              label def gender 1 "MAle", modify
              Code:
               
               gen id = _n rename (A-E) y= reshape long y, i(id) j(which) string set scheme s1color  graph hbar (count), over(y)  over(which) asyvars stack more

              Comment


              • #8
                Thanks for the further detail but I am still guessing wildly as the data in #7 don't seem to relate closely to the data in #5.

                But clearly you can't expect separation by gender if your command makes no reference to that variable.

                Perhaps

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(yA yB yC yD yE gender)
                0 0 0 0 0 1
                0 0 0 0 0 0
                0 1 1 0 0 0
                0 1 1 0 0 0
                1 1 1 0 0 1
                1 1 1 1 1 1
                1 2 2 1 1 1
                2 2 2 2 1 1
                2 2 3 2 1 1
                3 2 3 3 1 0
                3 3 4 3 2 0
                3 3 4 4 2 1
                3 3 4 4 2 1
                4 4 4 4 3 1
                4 4 4 4 3 0
                4 4 4 4 4 1
                end
                label values gender gender
                label def gender 0 "Female" 1 "Male"
                gen id = _n
                reshape long y, i(id) j(which) string
                set scheme s1color 
                graph bar (count),  over(y)  over(gender) over(which) percent asyvars stack
                more

                Comment


                • #9
                  Thank you so much! this is exactly what I needed. This will save me a lot of time.

                  Comment

                  Working...
                  X