Announcement

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

  • catplot problem


    Hello everyone,

    I have several discrete Variables with 3 categories. Those variables indicate, if respondents agree (1), partially agree (2) or don't agree (3) with different statements.

    I would like to generate stacked bar charts with stata 11.2 (the hight of the bar will be 100%), indicating the percentages of the categories for different groups of respondents.

    Unfortunately, in same cases there appears to be a problem with catplot (2.0.0).

    Example1: If I use a variable (rating) where respondents rated with 1, 2 or 3, everything is fine. I can use catplot and get for each of the groups stacked bars.

    The distribution of the variable rating looks like this:
    group | 1 2 3 | Total
    -----------+---------------------------------+----------
    1 | 1 0 0 | 1
    2 | 0 1 0 | 1
    3 | 0 0 1 | 1
    9 | 2 0 1 | 3
    -----------+---------------------------------+----------
    Total | 3 1 2 | 6

    catplot rating, over(group) ///
    stack asyvars ///
    bar(1, color(gs12)) bar(2, color(red)) bar(3, color(black)) perc(group)

    In the catplot graph the proportion of people rating the statement with a 1 in each group is shown by a grey bar, the proportion of people rating the statement with a 2 is shown by a red bar
    and the the proportion of people rating the statement with a 3 is shown by a black bar (enclosed you will find the graph rating.gph).


    Example 2: But if I use another variable (rating1), where none of the respondents rated the statement with 2, I get a problem with the colors in the graph.

    The distribution of the variable rating1 looks like this:
    group | 1 3 | Total
    -----------+----------------------+----------
    1 | 1 0 | 1
    2 | 0 1 | 1
    3 | 0 1 | 1
    9 | 2 1 | 3
    -----------+----------------------+----------
    Total | 3 3 | 6

    catplot rating1, over(group) ///
    stack asyvars ///
    bar(1, color(gs12)) bar(2, color(red)) bar(3, color(black)) perc(group)

    Now -and this is the problem- catplot slides what is on the right to the left. That's why the proportion of people rating the statement with a 3 is now shown by a RED and not by a Black
    bar in this case. So catplot, does not skip the missing category in the bars. As you can see in the graph rating1.gph enclosed, the number 2 isn't displayed in the legend, but the color is there not correct, too).

    If I compare different graphs (example 1 and 2) with each other, than the colors are not comparable and may be misleading in my paper. I am not 100% sure, but the problem sounds like a problem discussed in 2008 on the stata list. But I can't find a solution to make the colors comparable...

    http://www.stata.com/statalist/archi.../msg00181.html
    http://www.stata.com/statalist/archi.../msg00440.html.

    Any help would be appreciated a lot.

    Regards,
    Simon
    Attached Files

  • #2
    This is really a question about -graph bar- syntax. The bar numbered 2 (e.g.) is the 2nd bar shown, not the bar for values of 2. Also, -graph- does not remember the colours used in a previous graph.

    Comment


    • #3
      I don't see how to do it with catplot, but with graph bar and graph hbar you can include elements with zero weight. Here I include the variable wt which is 1, except for three fake observations where it is 0. These fake observations make sure that all ratings are represented, even if they are not visible.
      Code:
      input ID rating rating1 group wt
      1 3 3 3 1
      2 1 1 1 1
      3 2 3 2 1
      4 3 3 9 1
      5 1 1 9 1
      6 1 1 9 1
      . 1 1 1 0
      . 2 2 1 0
      . 3 3 1 0
      end
      
      graph hbar (sum) wt , ///
      over(rating) over(group) asyvars percent stack ///
      bar(1, fcolor(gs12)) bar(2, fcolor(red)) bar(3, fcolor(black))
      
      graph hbar (sum) wt , ///
      over(rating1) over(group) asyvars percent stack ///
      bar(1, fcolor(gs12)) bar(2, fcolor(red)) bar(3, fcolor(black))

      Comment


      • #4
        Hi everyone,

        thanks a lot! What I am really confused of is, that in rating1.gph, the bar for values of 3 (in the legend) is red, which is supposed to be the color for bars with values of 2.

        @ Nick: But it sounds like, there is no possibility to change that with catplot?
        @Svend: Thanks for the syntax. I will try this soon and reply again.

        Regards, Simon

        Comment


        • #5
          There are several ways to tackle this with -catplot-. You could restructure your data and use a -by()- option to give two graphs with one consistent legend. I am travelling right now and not in a good position to suggest detailed code.

          Comment


          • #6
            Hi Nick,

            ok, thanks a lot for your response. I will test Svends code tomoorow. But it would be really interesting to know how to solve this with -catplot-. If you'd have some time for any suggestions, I would appreciate it a lot.

            Tanks, Simon.

            Comment


            • #7
              Hi Svend,

              I am just testing your code. That's a really nice solution.

              Thanks, Simon

              Comment


              • #8
                I can't see a way of using catplot to mimic Svend's solution exactly. However, as indicated earlier, I have another solution, to reshape and draw a combined graph. Here graphs named g1 and g2 are Svend's and the last graph is my suggestion. The logic is that you clearly want consistent bar colours for comparison of two graphs, so drawing the two graphs as one is another way to approach it.

                Code:
                clear 
                input ID rating rating1 group wt
                1 3 3 3 1
                2 1 1 1 1
                3 2 3 2 1
                4 3 3 9 1
                5 1 1 9 1
                6 1 1 9 1
                . 1 1 1 0
                . 2 2 1 0
                . 3 3 1 0
                end
                
                graph hbar (sum) wt , ///
                over(rating) over(group) asyvars percent stack ///
                bar(1, fcolor(gs12)) bar(2, fcolor(red)) bar(3, fcolor(black)) name(g1, replace) 
                
                graph hbar (sum) wt , ///
                over(rating1) over(group) asyvars percent stack ///
                bar(1, fcolor(gs12)) bar(2, fcolor(red)) bar(3, fcolor(black)) name(g2, replace) 
                
                rename rating rating0 
                gen id = _n 
                reshape long rating, i(id) j(which) 
                
                catplot rating group if wt , asyvars percent(which group) stack by(which)  /// 
                bar(1, fcolor(gs12)) bar(2, fcolor(red)) bar(3, fcolor(black)) name(g3, replace)

                Comment


                • #9
                  Thank's a lot for your suggestion, Nick. It works fine!

                  Comment

                  Working...
                  X