Announcement

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

  • Question on bar graph (percentages of answer options) by group for more than 3 variables

    Hi all

    I am fairly new to Stata and the forum, I hope the way I described my problem is ok! Thank you so much in advance for reading and helping out

    I have a battery of questions administered to parents (response options on a 7-point Likert scale) in 3 rounds (so at 3 different time points). I would like to display the percentages of each answer option across each round for all the questions in the same bar graph (for descriptive purposes and to give an overview). My problem is that I could not find a command that would fit all of my questions in one graph!

    My data is stored in a long format, and I have the following variables:

    Code:
     
    ID, wave, recruitment_pathway, question1, question2, question3, question4, question5, question6, question7, question8, question9, question10
    To illustrate how I would like the graph to look like, I manually generated an example. Please see below. Ideally I would like to have two separate graphs like the one below, separated by the binary variable recruitment_pathway.



    I first tried to generate this graph by using the graph command, but this did not allow me to enter multiple variables.
    Code:
    graph hbar (count) if recruitment_pathway==1, over (question1) over(wave, label(labsize(vsmall))) percent stack asyvars bar(1, fcolor(dknavy) lcolor(dknavy)) bar(2, fcolor(emidblue) lcolor(emidblue)) bar(3, fcolor(eltblue) lcolor(eltblue)) bar(4, fcolor(teal) lcolor(teal)) bar(5, fcolor(erose) lcolor(erose)) bar(6, fcolor(lavender) lcolor(lavender)) bar(7, fcolor(purple) lcolor(purple)) legend(on colfirst cols(2) size(vsmall) color(black))  
    
    graph hbar (count) if recruitment_pathway==2, over (question1) over(wave, label(labsize(vsmall))) percent stack asyvars bar(1, fcolor(dknavy) lcolor(dknavy)) bar(2, fcolor(emidblue) lcolor(emidblue)) bar(3, fcolor(eltblue) lcolor(eltblue)) bar(4, fcolor(teal) lcolor(teal)) bar(5, fcolor(erose) lcolor(erose)) bar(6, fcolor(lavender) lcolor(lavender)) bar(7, fcolor(purple) lcolor(purple)) legend(on colfirst cols(2) size(vsmall) color(black))
    I then used catplot with the code below, but from my understanding, it also does not allow to enter more than 3 variables, and I want to include the 10 questions.
    Code:
    ssc desc catplot 
    ssc install catplot
    catplot question1 wave, percent(wave) by(recruitment_pathway) recast(hbar) asyvars stack legend(size(vsmall) color(black)) bar(1, fcolor(dknavy) lcolor(dknavy)) bar(2, fcolor(emidblue) lcolor(emidblue)) bar(3, fcolor(eltblue) lcolor(eltblue)) bar(4, fcolor(teal) lcolor(teal)) bar(5, fcolor(erose) lcolor(erose)) bar(6, fcolor(lavender) lcolor(lavender)) bar(7, fcolor(purple) lcolor(purple))
    Do you have any suggestions for automatically creating a graph like the one above? Thank you so much, I really appreciate it!

    Laura

  • #2
    Your image isn't visible to me. In essence you need to post .png as explained in https://www.statalist.org/forums/help#stata

    I suspect you can make progress by restructuring your data so 10 questions and their answers become two variables, but you still need more than one graph to show combinations of wave and recruitment pathway. Again those last two could be mapped to one composite variable.

    If someone doesn't give a better answer I think we need a data example to make concrete progress. See the link above.

    Comment


    • #3
      Hi Nick,

      thanks so much for the fast response! I included the example graph as a png now below, I hope you can see it now.

      This graph is manually created and is how I would like my graph to look like! To separate per recruitment pathway, I would just like to create two graphs similar to the one below that can be viewed next to each other. I hope that makes sense!

      Click image for larger version

Name:	GraphStatalist.png
Views:	1
Size:	26.7 KB
ID:	1621143


      Also, please see a data example below.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float wave byte recruitment_pathway long(question1 question2 question3 question4 question5 question6 question7 question8 question9 question10)
      1 1 5 6 7 7 6 6 1 6 2 4
      1 1 5 6 6 6 6 5 1 5 5 5
      1 1 5 6 5 7 5 6 1 7 3 7
      1 1 5 6 6 6 6 6 2 4 4 5
      1 1 6 6 6 6 6 6 1 6 2 5
      1 1 5 7 7 6 7 7 1 7 1 6
      1 1 6 6 2 6 6 6 1 6 5 6
      1 1 5 5 2 7 7 7 1 7 5 5
      1 1 6 6 6 7 7 7 1 7 6 7
      1 1 6 7 6 6 6 7 1 6 4 4
      1 1 7 7 7 6 5 7 1 6 3 6
      1 1 6 6 5 6 6 6 1 4 4 4
      1 1 6 6 6 7 5 6 1 7 2 5
      1 1 6 6 5 7 6 7 1 7 3 5
      1 1 6 7 7 5 3 6 1 5 3 5
      1 1 6 6 6 7 7 6 1 6 6 6
      1 1 6 6 6 7 6 7 1 6 2 6
      1 1 7 6 5 7 6 5 1 5 4 5
      1 1 6 7 7 7 5 6 1 5 6 5
      1 1 6 7 7 7 7 7 1 6 5 6
      1 1 7 7 7 7 7 7 1 7 5 7
      1 1 7 6 7 6 6 6 1 6 6 7
      1 1 . . . . . . . . . .
      1 1 . . . . . . . . . .
      1 1 6 7 7 7 6 6 1 7 2 6
      1 2 7 7 7 7 7 7 1 7 2 7
      1 2 . . . . . . . . . .
      1 2 . . . . . . . . . .
      1 1 6 7 7 5 5 6 1 6 4 5
      1 1 6 7 6 5 5 7 1 6 5 5
      1 1 1 5 6 6 6 6 1 7 5 5
      1 1 6 6 6 4 6 6 1 5 4 6
      1 2 . . . . . . . . . .
      1 2 . . . . . . . . . .
      1 1 6 6 6 6 5 5 1 6 5 3
      1 1 6 6 6 7 6 6 1 6 5 6
      1 2 . . . . . . . . . .
      1 1 1 7 7 7 6 7 1 7 2 6
      1 1 6 6 6 6 5 6 1 5 4 6
      1 1 6 7 7 7 7 7 1 7 5 7
      1 1 . . . . . . . . . .
      1 1 5 6 6 7 7 6 1 7 2 5
      1 1 7 7 6 7 6 7 1 6 5 4
      1 2 6 6 6 6 5 6 2 6 4 5
      1 2 5 7 7 7 7 7 1 7 4 5
      1 1 6 6 6 6 6 6 1 6 3 5
      1 1 6 6 2 7 6 6 1 7 2 5
      1 1 . . . . . . . . . .
      1 1 5 6 7 6 5 6 1 5 3 5
      1 1 . . . . . . . . . .
      1 2 4 3 5 7 6 6 1 6 2 6
      1 2 . . . . . . . . . .
      1 2 6 6 6 7 6 6 1 6 4 5
      1 2 7 5 7 7 4 6 1 5 3 6
      1 2 . . . . . . . . . .
      1 2 7 7 7 7 7 7 1 7 7 7
      1 2 7 5 7 7 7 7 1 6 6 5
      1 2 6 5 6 6 5 5 1 6 2 6
      1 2 . . . . . . . . . .
      1 2 . . . . . . . . . .
      1 2 5 7 7 7 7 7 1 7 6 7
      1 2 . . . . . . . . . .
      1 2 6 7 1 7 7 6 1 7 4 7
      1 2 . . . . . . . . . .
      1 2 . . . . . . . . . .
      1 2 5 5 6 6 6 7 1 5 4 2
      1 2 7 6 6 6 6 6 1 6 6 6
      1 2 6 5 5 6 5 5 1 6 5 5
      1 2 7 6 5 6 7 7 1 6 5 5
      1 2 6 6 6 6 6 6 1 7 2 4
      1 2 . . . . . . . . . .
      1 2 . . . . . . . . . .
      1 2 6 6 7 5 7 7 1 7 5 7
      1 2 6 6 6 5 5 6 1 6 2 4
      1 2 7 2 7 7 7 7 5 7 4 7
      1 2 . . . . . . . . . .
      1 2 . . . . . . . . . .
      1 2 7 5 6 7 7 6 1 6 3 4
      1 2 7 5 6 7 7 7 1 6 5 7
      1 2 5 6 6 6 6 5 2 6 3 6
      1 2 7 7 7 7 6 7 5 7 5 6
      1 2 7 7 7 6 6 6 1 7 3 3
      1 2 . . . . . . . . . .
      1 2 6 6 7 7 7 7 1 7 6 7
      1 2 7 7 7 7 6 7 1 7 5 4
      1 2 6 6 7 5 6 7 1 6 1 6
      1 2 6 5 6 5 5 7 1 6 3 3
      1 2 7 5 6 7 7 6 1 5 6 5
      1 2 7 7 7 7 7 7 1 6 5 6
      1 2 6 5 6 6 6 6 4 6 6 6
      1 2 6 5 4 7 6 6 1 5 4 6
      1 2 2 3 7 7 7 7 1 7 1 5
      1 2 . . . . . . . . . .
      1 2 . . . . . . . . . .
      1 2 7 7 7 7 5 7 1 7 3 7
      1 2 6 6 6 7 7 6 2 7 5 7
      1 2 7 7 7 6 5 6 1 5 5 5
      1 2 6 6 6 7 6 6 1 7 3 6
      1 2 6 6 6 6 5 6 1 2 2 2
      1 1 7 7 7 7 7 7 1 4 2 3
      end
      label values wave waves
      label def waves 1 "Wave 1", modify
      label values question1 Agreement
      label values question2 Agreement
      label values question3 Agreement
      label values question4 Agreement
      label values question5 Agreement
      label values question6 Agreement
      label values question7 Agreement
      label values question8 Agreement
      label values question9 Agreement
      label values question10 Agreement
      label def Agreement 1 "Strongly disagree", modify
      label def Agreement 2 "Disagree", modify
      label def Agreement 4 "Neither agree nor disagree", modify
      label def Agreement 5 "Somewhat agree", modify
      label def Agreement 6 "Agree", modify
      label def Agreement 7 "Strongly agree", modify
      label def Agreement 3 "Somewhat disagree", modify
      Thank you so much again!

      Laura

      Comment


      • #4
        Thanks for the graph and the example data.

        The title referring to Question Three seems at odds with the labels on the left-hand side.

        A bigger deal -- and as some readers may recall a hobby-horse of mine -- is that the stacked bar design, although popular and in principle encoding all the detail, is in practice often hard to decode.

        Thus although I wrote catplot (mentioned in #1) I haven't touched the code since 2010 and tend to recommend a different design.

        As in #2 I worry on your behalf about the scope to show results for several questions (here identified by number only; that's a burden for the reader) AND different waves AND two recruitment pathways.

        This example shows some of the possibilities -- including ordering the questions differently -- and there are yet more, including the possibility of different colours for each answer.

        Code:
        gen long id = _n 
        reshape long question, i(id) j(which) 
        rename question answer 
        set scheme s1color 
        
        * ssc install myaxis 
        * https://www.statalist.org/forums/forum/general-stata-discussion/general/1598767-myaxis-available-from-ssc-reorder-categorical-variables-especially-for-later-table-or-graph-use
        myaxis xaxis=which, sort(mean answer) descending 
        
        label def recruitment 1 "Pathway 1" 2 "Pathway 2"
        label val recruitment recruitment 
        
        * install from Stata Journal 
        * https://www.statalist.org/forums/forum/general-stata-discussion/general/1335274-tabplot-updated-on-ssc
        tabplot answer xaxis if wave == 1, by(recruitment, note("Wave 1", size(medium) pos(11))) subtitle(, fcolor(green*0.1)) barw(0.7) bfcolor(none) showval ytitle("") xtitle("Question") yreverse
        Click image for larger version

Name:	laura.png
Views:	1
Size:	34.2 KB
ID:	1621148

        Comment


        • #5
          Hi Nick,

          thanks so much for your help!

          The title referring to Question Three seems at odds with the labels on the left-hand side.
          Thats true, the title was intended as a personal reference and will be adapted later on (I do have several questions with sub-questions, and questions 1-10 are sub questions of the third main question). I am sorry I was so vague, I tried to simplify by just naming the questions 1-10. Also, each of the questions 1-10 are linked to a theoretical construct, so they will be re-named in the end!

          A bigger deal -- and as some readers may recall a hobby-horse of mine -- is that the stacked bar design, although popular and in principle encoding all the detail, is in practice often hard to decode.
          I absolutely agree that the stacked design makes it hard to decode. The graph was just meant to give an indication of the „bigger picture“ and provide an overview of the questions as there are so many. At this stage, I was not prioritising to provide the specific values of the proportions. I thought to then later on go into more detail for the most relevant ones.

          As in #2 I worry on your behalf about the scope to show results for several questions (here identified by number only; that's a burden for the reader) AND different waves AND two recruitment pathways.
          Again I agree. The questions will be renamed according to the theoretical construct they link to, but still there will be quite a lot of results considering the waves and recruitment pathway. This was the best way I could come up with to present all in one graph, as I was instructed to do

          This example shows some of the possibilities -- including ordering the questions differently -- and there are yet more, including the possibility of different colours for each answer.
          Thank you for the example code! I tried it out and it worked really well of course. This will be a good way to provide a bit more detail for each of the waves.

          Thanks again,

          Laura


          Comment

          Working...
          X