Announcement

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

  • Creating stacked bar chart

    Hi,

    I would be grateful for any guidance on how to create a diverging stacked bar chart. I have reviewed previous examples posted on the forum but still haven't quite figured this out.

    I have four variables that are Likert-scaled (1-Strongly disagree...6-Strongly agree).

    I would like to create a stacked bar chart for each of these variables with counts (1-50) as the Y-axis values.

    I would like the graph to look like this:

    I would be grateful for any advice regarding code I can use to create such a graph.

    Many thanks,
    Sarah

  • #2
    slideplot (SSC) gets there you much of the way, but doesn't support a central bar for a neutral category that spans zero. So, I think you may need to program this yourself using twoway rbar repeatedly.

    I think they are oversold myself. They have all the disadvantages of stacked bars and only one advantage, of giving an overview of the general tendency of each group. Despite my respect for Richard Heiberger and Naomi Robbins, who are articulate about their use, I have never strongly wanted to code up this idea.

    Comment


    • #3
      Here's the essence of one recipe.

      Code:
      sysuse auto, clear
      
      * reduced dataset of percents
      contract foreign rep78 if !missing(foreign, rep78)
      bysort foreign : egen total = total(_freq)
      gen pc = 100 * _freq / total
      
      * initialise to sum of negative categories and half any neutral category
      egen start = total(pc * (rep78 == 1) + pc * (rep78 == 2) + 0.5 * pc * (rep78 == 3)) , by(foreign)
      * negate start
      replace start = -start
      
      * bar starts and ends
      bysort foreign : gen end = start + sum(pc)
      bysort foreign : replace start = start + sum(pc[_n-1]) if _n > 1
      
      list
      
      twoway rbar start end foreign if rep78 == 1, barw(0.5) bfcolor(red*0.6) blcolor(red) ///
      || rbar start end foreign if rep78 == 2, barw(0.5) bfcolor(red*0.3) blcolor(red)     ///
      || rbar start end foreign if rep78 == 3, barw(0.5) bfcolor(white) blcolor(gs12)      ///
      || rbar start end foreign if rep78 == 4, barw(0.5) bfcolor(blue*0.3) blcolor(blue)   ///
      || rbar start end foreign if rep78 == 5, barw(0.5) bfcolor(blue*0.6) blcolor(blue)   ///
      legend(order(5 "5"   4 "4" 3 "3"  2 "2" 1 "1") col(1) pos(3)) xla(0 1, valuelabel)   ///
      yli(0, lc(gs12) lw(thin)) ytitle(%) ytitle(Repair record (%))
      Click image for larger version

Name:	diverging.png
Views:	1
Size:	17.6 KB
ID:	1484621


      I'd recommend more strongly something like this using tabplot (Stata Journal). See e.g. https://www.statalist.org/forums/for...updated-on-ssc for some discussion.

      Code:
      sysuse auto, clear
      tabplot rep78 foreign , showval percent(foreign) bfcolor(none) yreverse subtitle(percent) aspect(1)
      Click image for larger version

Name:	diverging2.png
Views:	1
Size:	17.8 KB
ID:	1484631

      Last edited by Nick Cox; 20 Feb 2019, 05:11.

      Comment


      • #4

        Hi Nick, Thank you so much!
        I still have a lot of problems with this, even for a single variables stacked bar graph. I played around with the code you provided in #3:

        tabplot rep78 foreign , showval percent(foreign) bfcolor(none) yreverse subtitle(percent) aspect(1)
        Here is one of the codes I used for two variables (both categorical variables)

        Code:
         tabplot RWreqclisite RWreqcosite, showval percent(RWreqclisite) bfcolor (none) yreverse subtitle(percent) aspect(1)
        What keeps coming out is jumbled. What I am going for is frequencies as the y value and the "stacks" representing the percentages of the frequencies. I am not sure what to do to show these two variables and the stacked responses. At some point, I want to produce a graph like the one I initially posted about. However, at this point, I can't seem to figure out the basics (i.e., a stacked bar graph that displays two variables and their responses in a stacked form)
        Last edited by Sarah Soroui; 20 Feb 2019, 23:51.

        Comment


        • #5
          You won't get stacked displays from tabplot. It isn't designed to produce them.

          As for the rest of #4. sorry, but there is almost nothing I can discuss. You don't present example data and you don't show the graph you got and your problem report doesn't go beyond "jumbled".

          If you want frequencies, not percents, then the code of #3 can be simplified. But I don't understand how you can have both unless the numbers in each group are equal.

          Code:
          sysuse auto, clear
          
          * reduced dataset of percents
          contract foreign rep78 if !missing(foreign, rep78)
          
          * initialise to sum of negative categories and half any neutral category
          egen start = total(_freq * (rep78 == 1) + _freq * (rep78 == 2) + 0.5 * _freq * (rep78 == 3)) , by(foreign)
          * negate start
          replace start = -start
          
          * bar starts and ends
          bysort foreign : gen end = start + sum(_freq)
          bysort foreign : replace start = start + sum(_freq[_n-1]) if _n > 1
          
          list
          
          twoway rbar start end foreign if rep78 == 1, barw(0.5) bfcolor(red*0.6) blcolor(red) ///
          || rbar start end foreign if rep78 == 2, barw(0.5) bfcolor(red*0.3) blcolor(red)     ///
          || rbar start end foreign if rep78 == 3, barw(0.5) bfcolor(white) blcolor(gs12)      ///
          || rbar start end foreign if rep78 == 4, barw(0.5) bfcolor(blue*0.3) blcolor(blue)   ///
          || rbar start end foreign if rep78 == 5, barw(0.5) bfcolor(blue*0.6) blcolor(blue)   ///
          legend(order(5 "5"   4 "4" 3 "3"  2 "2" 1 "1") col(1) pos(3)) xla(0 1, valuelabel)   ///
          yli(0, lc(gs12) lw(thin)) ytitle(%) ytitle(Repair record (frequency))
          Please read and act on https://www.statalist.org/forums/help#stata to make your questions clearer

          Comment


          • #6
            Note that the code in #3 is for a single ordered response and one categorical predictor. If you have a different data structure, you need to adapt the data or you need different code.

            All this underlines the permanent advice in https://www.statalist.org/forums/help#stata If you don't explain your data clearly, precise advice is sometimes impossible and may be delayed until you do.

            Comment


            • #7
              Following the example of Nick, below is a more elaborate example to graph multiple Likert coded items.
              Note that the dta files are saved in the then current working directory (which you can control by using: cd "your folder". To see what the current working directory is use: pwd).

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input long(age gender education r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17 r18 r19 r20 r21 r22 r23 r24 r25 r26 r27 r28 r29)
              1 0 0 1 1 1 1 1 1 1 2 2 1 2 2 1 1 1 1 1 2 1 2 2 1 1 2 3 2 1 1 2
              1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1
              1 0 0 1 1 1 1 1 1 2 1 2 1 2 2 1 2 1 2 1 2 1 1 1 1 1 1 3 2 1 2 2
              0 0 0 1 1 1 1 1 1 2 1 2 1 1 2 1 1 1 2 1 3 1 1 1 1 1 1 1 3 1 3 1
              1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              1 0 0 2 1 1 2 1 1 2 1 1 1 2 1 1 3 1 2 1 1 1 1 1 2 1 1 2 1 2 2 1
              0 0 0 1 1 1 1 1 1 2 1 2 1 2 1 1 1 1 1 1 2 1 1 2 1 1 2 1 1 1 1 1
              1 0 0 1 1 1 3 1 3 2 1 3 1 2 3 1 1 3 3 1 2 1 1 2 1 2 3 3 1 1 2 1
              1 0 0 1 1 1 1 1 1 1 1 2 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1
              1 0 0 1 1 1 1 1 1 1 1 1 1 2 3 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1
              0 0 0 1 1 1 1 1 1 2 1 2 1 2 2 3 2 2 2 1 3 1 1 2 2 2 2 1 1 2 2 1
              0 0 0 1 1 1 1 1 1 2 1 2 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1
              0 1 0 1 1 1 1 1 1 2 1 1 1 1 2 2 3 1 2 1 1 1 1 1 1 2 2 3 3 2 2 2
              1 1 0 1 1 3 3 1 1 3 1 1 1 3 2 2 3 2 3 1 3 1 1 1 2 2 3 2 3 2 2 1
              1 1 0 1 1 1 2 1 1 2 1 1 1 1 2 1 1 1 1 1 2 1 1 1 2 1 1 1 1 2 1 1
              1 0 0 1 1 1 1 1 1 1 1 2 1 1 2 1 3 2 2 1 2 1 1 2 1 2 1 1 2 2 1 1
              0 0 0 1 1 1 2 1 1 2 1 2 1 1 3 1 2 1 2 1 3 1 1 1 2 2 1 1 1 2 2 1
              0 0 1 5 4 5 3 3 5 3 1 3 3 2 3 3 3 3 3 3 3 3 3 3 3 4 3 3 3 3 4 3
              1 1 0 2 2 1 2 1 1 3 2 2 1 2 2 1 2 1 3 1 1 1 1 1 2 2 1 2 2 2 2 2
              0 1 0 1 1 1 1 1 2 1 1 2 1 2 3 3 1 3 1 2 2 2 2 1 2 2 2 2 1 1 2 1
              0 1 0 2 1 1 2 2 2 3 1 2 1 1 2 2 1 2 2 1 2 2 2 1 2 4 2 2 3 2 4 2
              0 1 0 2 1 2 4 1 1 5 3 1 2 2 1 1 2 2 2 1 1 2 2 3 2 2 3 1 4 3 5 3
              0 1 0 1 1 3 2 3 1 3 4 4 2 2 3 1 1 1 3 1 3 1 2 3 2 1 2 4 1 2 2 2
              0 0 1 3 3 3 3 3 3 3 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
              1 1 0 2 2 1 1 1 1 2 1 1 3 1 1 1 1 1 2 1 1 2 1 1 2 2 2 3 3 2 2 1
              1 1 0 1 2 2 2 2 2 2 1 1 1 2 3 2 1 2 3 1 1 2 2 1 2 3 2 4 3 1 2 2
              0 1 1 2 2 1 2 1 1 2 2 2 1 2 2 1 3 1 3 1 3 1 2 2 2 2 1 2 1 2 2 2
              1 1 0 1 1 1 2 1 1 2 1 1 1 1 1 1 2 1 3 1 2 1 1 1 2 3 2 1 1 2 2 1
              0 1 0 2 2 2 2 3 2 3 2 2 2 2 2 2 2 3 4 2 2 2 3 2 2 1 2 4 2 2 3 2
              0 1 1 4 3 3 2 2 2 3 3 4 1 3 2 1 2 3 3 2 2 2 2 2 2 3 3 3 3 3 4 2
              0 1 0 3 3 3 3 3 3 3 4 3 2 3 3 3 3 2 4 2 3 3 3 3 4 4 3 4 4 2 3 2
              0 1 0 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              1 1 0 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 2 1 1 1 1 1 2 2 1 1 2 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 2 1 1 4 3 3 1 2 2 1 2 1
              0 1 0 2 2 2 4 5 3 4 2 3 2 3 4 2 3 2 3 2 4 2 3 1 3 3 4 4 4 3 3 4
              1 1 0 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 3 1 1 1 1
              0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 1 0 1 1 1 2 2 1 3 2 2 2 1 2 1 2 1 3 1 1 1 3 1 2 2 2 3 1 2 1 2
              0 1 1 3 3 3 3 3 3 5 3 3 3 3 4 4 3 4 3 3 3 3 3 3 3 3 3 5 5 3 5 4
              0 1 1 5 4 5 5 3 5 4 5 5 5 5 5 5 5 3 5 5 5 4 5 5 5 5 5 4 5 5 5 5
              0 0 0 1 1 2 2 2 3 2 2 2 2 3 2 1 2 2 2 1 1 1 1 2 2 1 2 3 3 1 1 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 0 1 3 2 3 1 1 2 1 1 2 2 3 3 1 3 3 3 2 3 1 2 1 2 1 3 3 3 1 3 3
              1 0 0 1 1 1 1 2 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 2 1 2 2 3 2 1 1 2
              0 1 0 2 1 2 1 1 1 4 2 1 1 3 1 2 1 1 2 2 3 1 3 1 2 2 3 4 4 3 2 1
              0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 1 0 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 2 1 1 1 1 3 1 1 1 2 1 1 1 1
              0 1 1 2 2 1 2 1 2 3 2 2 1 4 2 1 3 2 2 1 2 2 3 1 2 2 1 3 2 1 2 2
              0 0 0 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 4 1 1 1 1
              0 1 0 2 2 2 4 4 3 3 3 4 3 3 2 3 4 3 2 2 2 3 2 3 3 3 3 4 4 4 4 3
              0 0 0 2 1 1 2 2 1 1 1 2 1 3 1 3 3 1 2 1 2 1 1 2 2 2 1 3 3 1 2 2
              0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1
              0 0 0 2 1 2 3 1 1 3 3 2 2 1 1 2 1 1 2 1 1 2 1 1 2 1 2 3 2 2 2 2
              0 1 1 1 1 2 2 1 1 2 1 2 1 1 2 1 2 2 2 1 3 1 2 1 1 1 2 5 3 2 2 2
              0 1 1 2 1 2 2 1 1 2 2 2 1 3 2 1 3 2 3 1 1 2 1 1 2 1 3 3 3 2 2 2
              0 0 1 2 3 3 5 5 4 3 3 2 3 1 2 2 4 2 2 2 2 3 2 4 2 3 2 4 3 4 3 3
              0 1 1 2 2 2 3 4 2 2 2 4 2 3 3 5 4 1 3 1 2 2 2 3 3 4 3 3 1 3 2 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 0 0 2 2 2 2 1 2 3 3 1 2 1 3 2 1 2 3 1 2 3 1 1 2 1 2 3 3 2 3 2
              1 0 0 1 1 1 1 1 1 1 1 3 1 3 3 4 3 1 3 1 4 3 1 3 1 3 3 4 3 1 3 2
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 1 0 4 3 3 4 3 3 4 2 3 4 3 3 4 3 4 5 3 2 3 4 3 4 3 4 4 5 5 4 4
              0 0 0 1 1 2 2 2 1 3 2 1 1 1 2 2 3 2 1 1 1 2 1 2 2 1 1 1 2 2 2 2
              1 0 0 1 2 1 1 1 1 2 3 1 2 3 2 1 1 3 3 1 1 1 3 1 2 1 2 3 3 3 3 2
              0 1 0 3 4 4 4 1 5 4 2 1 4 4 4 2 3 4 4 3 5 1 2 3 4 4 3 4 3 3 4 2
              0 0 0 2 2 2 4 3 2 2 2 2 2 4 2 1 4 3 4 1 1 2 2 1 3 2 3 4 3 3 4 1
              0 1 1 3 3 3 4 4 4 4 2 3 3 1 3 4 3 2 4 1 1 3 4 1 3 4 4 2 3 4 4 4
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 1 0 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 3 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 2 1 1 1 1 2 1 2 1 2 1 1
              1 1 0 3 1 1 3 1 1 3 2 2 1 1 1 3 2 1 3 1 3 1 1 1 2 2 3 3 2 3 3 2
              1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 0 1 1 2 2 2 1 2 2 1 1 3 2 2 2 1 1 3 1 1 1 1 2 2 2 2 4 3 2 1 1
              0 1 0 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1
              1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 3 1 1 1 3 2 1 1 1
              0 1 0 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 1 0 1 1 1 1 1 1 2 1 2 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1
              1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1
              0 1 0 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 2 1 1 1 2 1 1 1 1 2 1 1
              0 1 0 2 2 3 4 3 2 3 3 3 3 4 3 4 3 3 3 2 4 2 4 2 3 3 3 5 3 3 3 3
              0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 3 1 1 1 1
              0 1 0 1 1 2 4 3 2 3 3 3 2 2 2 4 4 1 3 1 1 2 3 2 3 1 1 5 2 3 3 3
              0 1 0 1 1 1 2 3 4 3 1 1 2 2 1 4 2 1 3 1 3 1 2 1 3 4 3 5 4 2 2 3
              0 0 0 1 1 1 1 1 1 1 1 1 1 1 3 3 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1
              0 0 0 3 3 2 2 1 5 2 2 3 2 3 4 3 2 3 2 2 2 1 2 3 3 3 1 2 2 3 3 1
              0 0 0 4 3 4 5 3 5 4 4 4 4 2 3 4 4 5 4 4 5 5 4 1 4 4 5 5 5 4 4 3
              0 1 0 2 2 1 2 1 1 2 1 1 1 2 2 1 1 1 2 1 2 1 1 1 1 2 2 2 2 2 3 2
              0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 0 0 1 1 1 1 1 1 3 1 1 1 1 3 1 1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1
              0 0 0 1 1 1 1 1 2 2 2 2 2 1 3 3 4 1 2 1 1 1 1 3 1 2 3 3 1 1 2 2
              1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1
              0 1 0 1 1 1 3 2 2 3 2 4 2 4 3 4 1 1 4 1 1 1 2 1 3 1 5 3 3 4 3 3
              0 0 0 5 4 5 3 2 2 4 4 2 4 2 4 4 3 4 4 2 4 3 4 3 4 4 4 4 2 4 4 4
              1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
              0 0 1 1 1 1 2 1 1 3 1 1 2 1 1 2 2 1 3 1 1 1 1 3 2 1 2 3 3 2 2 1
              end
              
              notes : The data contains (100 through dataex) responses from 766 people sampled from a general population to the PROMIS Anxiety scale
              notes : composed of 29 Likert-type questions with a common rating scale (1=Never, 2=Rarely, 3=Sometimes, 4=Often, and 5=Always).
              notes : From the r package lordif
              notes : PROMIS Cooperative Group. Unpublished Manual for the Patient-Reported Outcomes Measurement Information System (PROMIS) Version 1.1. October, 2008
              
              label var r1 "I felt fearful"
              label var r1 "I felt fearful"
              label var r2 "I felt frightened"
              label var r3 "It scamaroon me when I felt nervous"
              label var r4 "I felt anxious"
              label var r5 "I felt like I needed help for my anxiety"
              label var r6 "I was concerned about my mental health"
              label var r7 "I felt upset"
              label var r8 "I had a racing or pounding heart"
              label var r9 "I was anxious if my normal routine was disturbed"
              label var r10 "I had sudden feelings of panic"
              label var r11 "I was easily startled"
              label var r12 "I had trouble paying attention"
              label var r13 "I avoided public places or activities"
              label var r14 "I felt fidgety"
              label var r15 "I felt something awful would happen"
              label var r16 "I felt worried"
              label var r17 "I felt terrified"
              label var r18 "I worried about other people's reactions to me"
              label var r19 "I found it hard to focus on anything other than my anxiety"
              label var r20 "My worries overwhelmed me"
              label var r21 "I had twitching or trembling muscles"
              label var r22 "I felt nervous"
              label var r23 "I felt indecisive"
              label var r24 "Many situations made me worry"
              label var r25 "I had difficulty sleeping"
              label var r26 "I had trouble relaxing"
              label var r27 "I felt uneasy"
              label var r28 "I felt tense"
              label var r29 "I had difficulty calming down"
              
              label var age "Binary 0=younger than 65 and 1=65 and olde"
              label var gender "Binary 0=Male and 1=Female"
              label var education "Binary 0=some college or higher and 1=high school or lower"
              
              save Anxiety , replace
              
              * CODE TO CREATE THE STACKED BARS OF ITEM RESPONSES (as %)
              forvalue i = 1(1)29 {
              * Load the data (repeatedly)
              use Anxiety , clear
              contract r`i' if !missing(r`i')
              gen id=_n
              egen total = total(_freq)
              gen pc = 100 * _freq / total
              * initialise to sum of negative categories and half any neutral category
              egen start = total(pc * (id == 1) + pc * (id == 2) + 0.5 * pc * (id == 3))
              * negate start
              replace start = -start
              * bar starts and ends
              gen end = start + sum(pc)
              replace start = start + sum(pc[_n-1]) if _n > 1
              replace id=`i'    // id must be increased to append
              * Rename individual items for the Likert response code
              rename r`i' likert
              save "Anxiety_r`i'.dta" , replace    // Individual files to append
              }
              
              * APPEND ITEM RESPONSE FILES
              use "Anxiety_r1" , clear
              forvalue i = 2(1)29 {
              append using "Anxiety_r`i'.dta"
              }
              * Define labels
              forvalue i = 1(1)29 {
              label define lbl_R `i' "R`i'" , add
              label value id lbl_R  
              }
              
              compress
              save "Anxiety_rStack.dta" , replace
              use "Anxiety_rStack.dta" , clear
              
              * Create the stacked bar graph of Likert responses as % (assuming 1 & 2 are negation codes)
              twoway rbar start end id if likert == 1, barw(0.5) bfcolor(maroon*0.6) blcolor(maroon*.6) ///
              || rbar start end id if likert == 2, barw(0.5) bfcolor(maroon*0.4) blcolor(maroon*.4)     ///
              || rbar start end id if likert == 3, barw(0.5) bfcolor(gs11) blcolor(gs11)          ///
              || rbar start end id if likert == 4, barw(0.5) bfcolor(blue*0.4) blcolor(blue*.4)   ///
              || rbar start end id if likert == 5, barw(0.5) bfcolor(blue*0.6) blcolor(blue*.6)   ///
              graphregion(margin(b-2 t-1)) yline(0, lc(gs12) lw(thin)) yscale(noextend)     ///
              legend(order(5 "5" 4 "4" 3 "3" 2 "2" 1 "1") col(5) pos(6) region(lc(white)) bmargin(b-2) symxsize(*.5) symysize(*.6) size(*.8))    ///
              xlabel(1(1)29, valuelabel labs(*.5)) yla(-100(10)40, labs(*.7) angle(0))  ///
              ytitle(Likert response (%), margin(l-1 r+.5)) xtitle(PROMIS Anxiety scale items, margin(t+2))
              This should result in the following graph:
              Click image for larger version

Name:	GraphLikertResponses.png
Views:	1
Size:	59.9 KB
ID:	1484811
              http://publicationslist.org/eric.melse

              Comment


              • #8
                That's very helpful, as it's easy to imagine that many people have two or more questions of this kind as two or more variables, and that may well be what Sarah has too.

                But the code is a lot of hard work: reading in the data again and again and creating many .dta files all to be appended.

                It seems to me that a reshape long brings the data within the reach of the code recipe in #3.

                Following on from your example data (thanks) I here show a tabplot, and also that one can sort the questions according to whatever criterion you want.

                Code:
                gen id = _n 
                reshape long r, i(id) j(Question) 
                
                tabplot  r Question , yreverse showval bfcolor(none) ytitle(Response) 
                
                egen order = total(r == 1), by(Question)
                egen group = group(order Question)
                
                * -labmask- is from the Stata Journal 
                labmask group, values(Question)
                
                tabplot  r group , yreverse showval bfcolor(none) ytitle(Response) xtitle(Question)
                Click image for larger version

Name:	melse1.png
Views:	1
Size:	41.9 KB
ID:	1484815
                Click image for larger version

Name:	melse2.png
Views:	1
Size:	40.7 KB
ID:	1484816


                Naturally looking at the marginal distributions alone is at most part of the story, but my prejudices against the diverging design aren't shaken here.

                Comment


                • #9
                  The post below is a duplicate from here.

                  For those users who visit this post and might be unaware, note that Nick published the module floatplot (29 May 2021, but now in version 1.0.3, 31 October 2022):
                  to draw floating or sliding stacked bar plot - floatplot produces a floating or sliding stacked bar plot showing percents (or optionally proportions or frequencies) of categories of a numeric outcome variable numvar by zero, one, or two other categorical variables. The plot is most helpful if categories of an outcome variable have a natural or conventional pre-defined order. Although there is no formal check, the design of the plot tacitly assumes a modest number of distinct categories, say between 2 and 9. The commmand is a wrapper for a call to twoway rbar for showing bars and twoway scatter for showing text.
                  It can be installed by using this code:
                  Code:
                  ssc install floatplot , replace
                  The examples in the help demonstrate how to create a floating or sliding stacked bar plot for frequencies, proportions, or percents. Very useful indeed to visualize the so-called Likert scale response variables also over categories.
                  http://publicationslist.org/eric.melse

                  Comment

                  Working...
                  X