Announcement

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

  • Multiple hbar graphs in one

    Hi Statlisters

    I am afraid that this might be simple, but I cannot find a previous post or the answer anywhere.

    I am using Stata SE 17.0

    I asked groups of people (variable: category) various questions before and after implementing an intervention (info). All questions were out of ten (outcome).

    Here is an example of my data (it is not all of it, so just some of the categories shown in the graphs below - but should work to just show what I am doing)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(unique round) byte outcome float(category info volgorde)
     1 2 10 1 7 7
     2 2  4 4 7 7
     2 1  5 4 7 7
     3 1  9 3 7 7
     3 2 10 3 7 7
     4 2 10 3 7 7
     5 1 10 4 7 7
     5 2  8 4 7 7
     6 1  5 3 7 7
     7 1  3 4 7 7
     7 2  5 4 7 7
     8 1 10 3 7 7
     8 2  8 3 7 7
     9 1  7 3 7 7
    10 1 10 3 7 7
    11 2 10 4 7 7
    11 1 10 4 7 7
    12 1  1 4 7 7
    12 2  8 4 7 7
    13 2  9 3 7 7
    13 1 10 3 7 7
    14 1  5 4 7 7
    14 2 10 4 7 7
    15 2  2 3 7 7
    16 2  6 3 7 7
    16 1 10 3 7 7
    17 1  8 3 7 7
    18 1  7 3 7 7
    19 1  8 4 7 7
    19 2  9 4 7 7
    20 1  5 3 7 7
    21 1  8 1 7 7
    22 1  7 1 7 7
    23 1  6 1 7 7
    24 2  2 1 7 7
    24 1  8 1 7 7
    25 1  5 1 7 7
    25 2  4 1 7 7
    26 2  8 1 7 7
    26 1  4 1 7 7
    27 1  5 1 7 7
    28 2 10 1 7 7
    28 1  6 1 7 7
    29 2  9 1 7 7
    30 2  9 1 7 7
    30 1 10 1 7 7
     1 2 10 1 1 1
     2 2  4 4 1 1
     2 1  9 4 1 1
     3 1  0 3 1 1
     3 2 10 3 1 1
     4 2 10 3 1 1
     5 1 10 4 1 1
     5 2  9 4 1 1
     6 1  5 3 1 1
     7 1  1 4 1 1
     7 2  6 4 1 1
     8 1 10 3 1 1
     8 2 10 3 1 1
     9 1 10 3 1 1
    10 1 10 3 1 1
    11 2 10 4 1 1
    11 1  0 4 1 1
    12 1  0 4 1 1
    12 2 10 4 1 1
    13 2  7 3 1 1
    13 1  7 3 1 1
    14 1  7 4 1 1
    14 2 10 4 1 1
    15 2 10 3 1 1
    16 2  5 3 1 1
    16 1  7 3 1 1
    17 1  9 3 1 1
    18 1  7 3 1 1
    19 1 10 4 1 1
    19 2 10 4 1 1
    20 1  0 3 1 1
    21 1  9 1 1 1
    22 1  8 1 1 1
    23 1  1 1 1 1
    24 2  3 1 1 1
    24 1  2 1 1 1
    25 1  9 1 1 1
    25 2  4 1 1 1
    26 2  5 1 1 1
    26 1 10 1 1 1
    27 1  8 1 1 1
    28 2  7 1 1 1
    28 1  5 1 1 1
    29 2  5 1 1 1
    30 2  7 1 1 1
    30 1  9 1 1 1
     1 2 10 1 2 2
     2 2  1 4 2 2
     2 1  5 4 2 2
     3 1 10 3 2 2
     3 2 10 3 2 2
     4 2  8 3 2 2
     5 1  5 4 2 2
     5 2  8 4 2 2
    end
    label values round roundl
    label def roundl 1 "Baseline (before implementing program)", modify
    label def roundl 2 "Endline (after program was implemented in half the ECD centres)", modify
    label values category category_lab
    label def category_lab 1 "CHICS delayed", modify
    label def category_lab 3 "CHICS implemented but meetings not attended", modify
    label def category_lab 4 "CHICS implemented and attended meetings", modify
    label values info infol
    label def infol 1 "Parents stand together & support", modify
    label def infol 2 "We can fix service delivery together", modify
    label def infol 7 "Life satisfaction", modify

    I am creating a horizontal graph, showing the means of various outcomes before and after an intervention. I want to show the means for various different groups. One option is to create the bar for each group and use grc1leg to join to one graph. But then the leftmost graph is a bit more squeezed in. Like this:

    set scheme tab2
    splitvallabels info
    local relabel `r(relabel)'
    graph hbar outcome if category == 1, over(round, gap(0)) asyvars bar(1, color(black)) bar(2, color(gs8)) over(info, sort(volgorde) relabel(`r(relabel)') label(labsize(vsmall))) ytitle("") ylabel(, nogrid) blabel(bar, size(vsmall) format(%4.1f)) title("Non-implementing", size(small))
    graph save "nonimplementing", replace

    graph hbar outcome if category == 2, over(round, gap(0)) asyvars bar(1, color(black)) bar(2, color(gs8)) over(info, sort(volgorde) relabel(`r(relabel)') label(labsize(vsmall))) ytitle("") ylabel(, nogrid) blabel(bar, size(vsmall) format(%4.1f)) title("Implementing", size(small))
    graph save "implementing", replace

    graph hbar outcome if category == 3, over(round, gap(0)) asyvars bar(1, color(black)) bar(2, color(gs8)) over(info, sort(volgorde) relabel(`r(relabel)') label(labsize(vsmall))) ytitle("") ylabel(, nogrid) blabel(bar, size(vsmall) format(%4.1f)) title("Non-attending", size(small))
    graph save "nonattending", replace

    graph hbar outcome if category == 4, over(round, gap(0)) asyvars bar(1, color(black)) bar(2, color(gs8)) over(info, sort(volgorde) relabel(`r(relabel)') label(labsize(vsmall))) ytitle("") ylabel(, nogrid) blabel(bar, size(vsmall) format(%4.1f)) title("Attending", size(small))
    graph save "attending", replace

    grc1leg nonimplementing.gph implementing.gph nonattending.gph attending.gph, col(4) graphregion(color(white))
    Picture1.png


    Question on the above graph:
    - the headings on the left, I added in Word (I know!), how can I add them in Stata. The top two falls under then larger category "Unity", the next two falls under "Knowledge", the next two is under the larger category "Agency" and then the last one falls under "General"




    An alternative is to use the by option, but then it is just ugly:

    graph hbar outcome, by(category) over(round, gap(0)) asyvars bar(1, color(black)) bar(2, color(gs8)) over(info, sort(volgorde) relabel(`r(relabel)') label(labsize(vsmall))) ytitle("") ylabel(, nogrid) blabel(bar, size(vsmall) format(%4.1f)) legend(size(small)) title(,size(small))
    Screenshot 2023-11-20 092515 2.png


    Few questions on this last one:
    • how do I get the titles smaller (my code is not working)
    • How do I get the legend smaller (my code is not working)
    • how do I add the number of columns for the by-option, so that these graphs are not in a grid of 2x2 but 4x1 (like the top graph)

  • #2
    Code:
    graph hbar outcome, by(category, col(1) note("")  leg(pos(7))) ///
    over(round, gap(0)) asyvars bar(1, color(black)) bar(2, color(gs8)) ///
    over(info, sort(volgorde) relabel(`r(relabel)') label(labsize(vsmall))) ///
    ytitle("") ylabel(, nogrid) blabel(bar, size(vsmall) format(%4.1f))  ///
    subtitle(, size(small)) legend(symy(4) symx(4) textw(2) forces ///
    order(1 "Baseline (before implementing program)" ///
    2 "Endline (after program was implemented in half the ECD centres)") ///
    size(small))
    For the long category labels, see the command splitvallabels from SSC.

    Code:
    ssc describe splitvallabels

    Comment


    • #3
      You did ask for a \(4\times 1\) graph in #1, not a \(1 \times 4\). In graph, labels on bars on the right-hand side are difficult to eliminate, but you can use the graph editor. My graph is \(3 \times 3\), so you need to adjust the code to the dimensions of your graph.

      Code:
      graph hbar outcome, by(category, row(1) note("")  leg(pos(7))) ///
      over(round, gap(0)) asyvars bar(1, color(black)) bar(2, color(gs8)) ///
      over(info, sort(volgorde) relabel(`r(relabel)') label(labsize(vsmall))) ///
      ytitle("") ylabel(, nogrid) blabel(bar, size(vsmall) format(%4.1f))  ///
      subtitle(, size(vsmall)) legend(symy(4) symx(4) textw(2) forces ///
      order(1 "Baseline (before implementing program)" ///
      2 "Endline (after program was implemented in half the ECD centres)") ///
      size(small))
      
      forval j=2/3{
          forval i=1/3{
              gr_edit .plotregion1.grpaxis[`j'].edit_tick `i' 1 `" "', tickset(major)
          }
      }
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	40.8 KB
ID:	1734425

      Comment


      • #4
        I would add a suggestion that dot charts would make much better use of the space and allow focus on the real research question, which is more likely to be comparing mean scores with each other, not with zero. Then markers (point symbols) for baseline and endline could go on the same physical line.

        Comment


        • #5
          Thank you so much Andrew! I tweaked it a bit, like you said, and it worked. And I learned some new code. Thanks!
          Thanks Nick - I actually already have a scatter plot that I really like to show the changes from pre- to post. This bar chart is more descriptive, to also show the means (not only the changes). Today's mission is to figure you how to add shadows showing the confidence interval behind each dot(showing a mean value)
          dots.png

          Comment

          Working...
          X