Announcement

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

  • How to Place Brackets on Bar Graphs Created by Twoway Command for Representation of Statistical Significance between Groups in Stata

    I have a small dataset below for the illustrative purpose,

    clear
    input str11 race str6 ses meanwrite sdwrite n
    "hispanic" "low" 46.11111 9.58442 9
    "hispanic" "middle" 46.09091 7.133787 11
    "hispanic" "high" 48.25 10.24288 4
    "asian" "low" 58 12.12436 3
    "asian" "middle" 55.8 7.463243 5
    "asian" "high" 61.66667 4.618802 3
    "african-amer" "low" 48.09091 8.431542 11
    "african-amer" "middle" 48.66667 8.594572 6
    "african-amer" "high" 47.66667 16.86219 3
    "white" "low" 52.54167 9.084048 24
    "white" "middle" 52.80822 9.222041 73
    "white" "high" 56.70833 8.7372 48
    end

    encode race, generate(race2)
    encode ses, generate(ses2)
    drop ses race
    ren race2 race
    ren ses2 ses
    order race ses meanwrite sdwrite n

    g sesrace = race if ses == 1
    replace sesrace = race+5 if ses == 2
    replace sesrace = race+10 if ses == 3


    twoway (bar meanwrite sesrace if race==1) ///
    (bar meanwrite sesrace if race==2) ///
    (bar meanwrite sesrace if race==3) ///
    (bar meanwrite sesrace if race==4) , ///
    legend(row(4) position(right) order(1 "Hispanic" 2 "Asian" 3 "Black" 4 "White") ) ///
    xlabel(0(5)15) ///
    ylabel(0(20)70) ///
    xlabel( 3 "Low" 8 "Middle" 13 "High", noticks) ///
    xtitle("Socio Economic Status") ytitle("Mean Writing Score")

    I know I can borrow some codes from this thread at https://www.statalist.org/forums/for...ificance/page2.
    to add brackets to represent the significant difference between "Asian" and "Black" for the Low SES group students.
    Can someone give me some help with the code?
    Thank you!
    Click image for larger version

Name:	22.JPG
Views:	1
Size:	142.2 KB
ID:	1714663

    Last edited by smith Jason; 23 May 2023, 20:38.

  • #2
    The code I used here is incorrect,
    tw (bar meanwrite sesrace if race==1) ///
    (scatteri 60.55 2 60.55 3, recast(line) ///
    lw(medthick) mc(none) lc(black) lp(solid)) (scatteri 60.55 2 60.55 3, ///
    recast(dropline) base(60.52) lw(medthick) mc(none) lc(black) lp(solid)), ///
    ysc(r(., 11)) scheme(s1mono) leg(off) text(60.57 2.5 "p<.001", place(n) size (0.3cm)) ///

    (bar meanwrite sesrace if race==2) ///
    (bar meanwrite sesrace if race==3) (bar meanwrite sesrace if race==4), ///
    legend(label(1 "Hispanic") label(2 "Asian") label(3 "African-American") label(4 "White")) ///
    xlabel(0(5)15) ylabel(0(20)70) xlabel(3 "Low" 8 "Middle" 13 "High", noticks) ///
    xtitle("Socioeconomic Status") ytitle("Mean Writing Score")
    How can I revise it?
    Last edited by smith Jason; 23 May 2023, 21:06.

    Comment


    • #3
      Can someone help me? Thank you!

      Comment


      • #4
        I am not really sure what the problem is. The y-axis positions are identified by your y-axis and each bar on the categorical axis is centered on an integer, starting at 1.


        Code:
        clear
        input str11 race str6 ses meanwrite sdwrite n
        "hispanic" "low" 46.11111 9.58442 9
        "hispanic" "middle" 46.09091 7.133787 11
        "hispanic" "high" 48.25 10.24288 4
        "asian" "low" 58 12.12436 3
        "asian" "middle" 55.8 7.463243 5
        "asian" "high" 61.66667 4.618802 3
        "african-amer" "low" 48.09091 8.431542 11
        "african-amer" "middle" 48.66667 8.594572 6
        "african-amer" "high" 47.66667 16.86219 3
        "white" "low" 52.54167 9.084048 24
        "white" "middle" 52.80822 9.222041 73
        "white" "high" 56.70833 8.7372 48
        end
        
        encode race, generate(race2)
        encode ses, generate(ses2)
        drop ses race
        ren race2 race
        ren ses2 ses
        order race ses meanwrite sdwrite n
        
        g sesrace = race if ses == 1
        replace sesrace = race+5 if ses == 2
        replace sesrace = race+10 if ses == 3
        
        set scheme s1mono
        twoway (bar meanwrite sesrace if race==1) ///
        (bar meanwrite sesrace if race==2) ///
        (bar meanwrite sesrace if race==3) ///
        (bar meanwrite sesrace if race==4)  ///
        (scatteri 65 2 65 3.15, recast(line) ///
        lw(medthick) mc(none) lc(black) lp(solid)) (scatteri 65 2 65 3.15, ///
        recast(dropline) base(62) lw(medthick) mc(none) lc(black) lp(solid)), ///
        legend(row(4) position(right) order(1 "Hispanic" 2 "Asian" 3 "Black" 4 "White") ) ///
        xlabel(0(5)15) text(66 2.6 "{it:p}<.001", place(n) size(vsmall)) ///
        ylabel(0(20)70) ///
        xlabel( 3 "Low" 8 "Middle" 13 "High", noticks) ///
        xtitle("Socio Economic Status") ytitle("Mean Writing Score")
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.6 KB
ID:	1714750


        Comment


        • #5
          Thank you, Andrew. Also, I tried to add such bracket to Asian and Black.
          I really need color graph instead of gray graph. Could you please help me?
          This is my code below, and it doesn't work.

          clear
          input str11 race str6 ses meanwrite sdwrite n
          "hispanic" "low" 46.11111 9.58442 9
          "hispanic" "middle" 46.09091 7.133787 11
          "hispanic" "high" 48.25 10.24288 4
          "asian" "low" 58 12.12436 3
          "asian" "middle" 55.8 7.463243 5
          "asian" "high" 61.66667 4.618802 3
          "african-amer" "low" 48.09091 8.431542 11
          "african-amer" "middle" 48.66667 8.594572 6
          "african-amer" "high" 47.66667 16.86219 3
          "white" "low" 52.54167 9.084048 24
          "white" "middle" 52.80822 9.222041 73
          "white" "high" 56.70833 8.7372 48
          end

          * Initial Bar Chart.
          graph bar meanwrite, over(race) over(ses) asyvars

          encode race, generate(race2)
          encode ses, generate(ses2)
          drop ses race
          ren race2 race
          ren ses2 ses
          order race ses meanwrite sdwrite n

          g sesrace = race if ses == 1
          replace sesrace = race+5 if ses == 2
          replace sesrace = race+10 if ses == 3
          sort sesrace
          list sesrace ses race, sepby(ses)



          tw (bar meanwrite sesrace if race==1) ///
          (bar meanwrite sesrace if race==2 ) ///
          (scatteri 62.55 2 62.55 3, recast(line) ///
          lw(medthick) mc(on) lc(black) lp(solid)) (scatteri 62.55 2 62.55 3, ///
          recast(dropline) base(61.50) lw(medthick) mc(none) lc(black) lp(solid)), ///
          ysc(r(., 11)) text(62.02 2.5 "***", place(n) size (0.3cm)), ///
          (bar meanwrite sesrace if race==3) ///
          (scatteri 62.55 3 62.55 4, recast(line) ///
          lw(medthick) mc(none) lc(black) lp(solid)) (scatteri 62.55 3 62.55 4, ///
          recast(dropline) base(61.50) lw(medthick) mc(none) lc(black) lp(solid)), ///
          ysc(r(., 11)) text(62.02 3.5 "***", place(n) size (0.3cm)), ///
          (bar meanwrite sesrace if race==4), ///
          legend(label(1 "Hispanic") label(2 "Asian") label(7 "African-American") label(8 "White")) ///
          xlabel(0(5)15) ylabel(0(20)70) xlabel(3 "Low" 8 "Middle" 13 "High", noticks) ///
          xtitle("Socioeconomic Status") ytitle("Mean Writing Score")

          Comment


          • #6
            The color is determined by your scheme.

            Code:
            set scheme s1color
            for example.

            Comment


            • #7
              Originally posted by Andrew Musau View Post
              The color is determined by your scheme.

              Code:
              set scheme s1color
              for example.
              Thank you! I changed this setting; however, the color is not the same as the top. How can I keep the same color?
              Moreover, I would like to remove the horizontal line at the top of the main graph and the vertical line on the right side.
              How can I modify the code to achieve this?
              Last edited by smith Jason; 24 May 2023, 09:08.

              Comment


              • #8
                Sorry, I cannot guide you on every little detail of your graph. Look at the documentation or get a colleague to help you.

                Comment


                • #9
                  Thanks for your advice.
                  I looked at this document at https://www.stata.com/manuals/g-2gra...ayscatteri.pdf and still cannot mimic and modify your code to figure out how to add bracket to the black and white for the low SES group.
                  As you have seen in my above code, once I inserted the corresponding code into any of the race group, although bracket was added to the graph and connected the two groups, there are unexpected legends took place.
                  I know that I need to solve the problem mainly independently, however, sometimes, it doesn't work at all even though I work hard and still don't know what to do and how to do.
                  Thanks for your help.

                  Below is my revised code based on your exemplary code,
                  clear
                  input str11 race str6 ses meanwrite sdwrite n
                  "hispanic" "low" 46.11111 9.58442 9
                  "hispanic" "middle" 46.09091 7.133787 11
                  "hispanic" "high" 48.25 10.24288 4
                  "asian" "low" 58 12.12436 3
                  "asian" "middle" 55.8 7.463243 5
                  "asian" "high" 61.66667 4.618802 3
                  "african-amer" "low" 48.09091 8.431542 11
                  "african-amer" "middle" 48.66667 8.594572 6
                  "african-amer" "high" 47.66667 16.86219 3
                  "white" "low" 52.54167 9.084048 24
                  "white" "middle" 52.80822 9.222041 73
                  "white" "high" 56.70833 8.7372 48
                  end

                  * Initial Bar Chart.
                  graph bar meanwrite, over(race) over(ses) asyvars

                  encode race, generate(race2)
                  encode ses, generate(ses2)
                  drop ses race
                  ren race2 race
                  ren ses2 ses
                  order race ses meanwrite sdwrite n

                  g sesrace = race if ses == 1
                  replace sesrace = race+5 if ses == 2
                  replace sesrace = race+10 if ses == 3
                  sort sesrace
                  list sesrace ses race, sepby(ses)



                  tw (bar meanwrite sesrace if race==1) ///
                  (bar meanwrite sesrace if race==2 ) ///
                  (scatteri 62.55 2 62.55 3, recast(line) ///
                  lw(medthick) mc(on) lc(black) lp(solid)) (scatteri 62.55 2 62.55 3, ///
                  recast(dropline) base(61.50) lw(medthick) mc(none) lc(black) lp(solid)), ///
                  ysc(r(., 11)) text(62.02 2.5 "***", place(n) size (0.3cm)), ///
                  (bar meanwrite sesrace if race==3) ///
                  (scatteri 62.55 3 62.55 4, recast(line) ///
                  lw(medthick) mc(none) lc(black) lp(solid)) (scatteri 62.55 3 62.55 4, ///
                  recast(dropline) base(61.50) lw(medthick) mc(none) lc(black) lp(solid)), ///
                  ysc(r(., 11)) text(62.02 3.5 "***", place(n) size (0.3cm)), ///
                  (bar meanwrite sesrace if race==4), ///
                  legend(label(1 "Hispanic") label(2 "Asian") label(7 "African-American") label(8 "White")) ///
                  xlabel(0(5)15) ylabel(0(20)70) xlabel(3 "Low" 8 "Middle" 13 "High", noticks) ///
                  xtitle("Socioeconomic Status") ytitle("Mean Writing Score")
                  Attached Files
                  Last edited by smith Jason; 24 May 2023, 09:58.

                  Comment


                  • #10
                    The problem is solved by myself with the Stata code below,
                    clear
                    input str11 race str6 ses meanwrite sdwrite n
                    "hispanic" "low" 46.11111 9.58442 9
                    "hispanic" "middle" 46.09091 7.133787 11
                    "hispanic" "high" 48.25 10.24288 4
                    "asian" "low" 58 12.12436 3
                    "asian" "middle" 55.8 7.463243 5
                    "asian" "high" 61.66667 4.618802 3
                    "african-amer" "low" 48.09091 8.431542 11
                    "african-amer" "middle" 48.66667 8.594572 6
                    "african-amer" "high" 47.66667 16.86219 3
                    "white" "low" 52.54167 9.084048 24
                    "white" "middle" 52.80822 9.222041 73
                    "white" "high" 56.70833 8.7372 48
                    end

                    encode race, generate(race2)
                    encode ses, generate(ses2)
                    drop ses race
                    ren race2 race
                    ren ses2 ses
                    order race ses meanwrite sdwrite n

                    g sesrace = race if ses == 1
                    replace sesrace = race+5 if ses == 2
                    replace sesrace = race+10 if ses == 3

                    set scheme s1color
                    twoway (bar meanwrite sesrace if race==1) ///
                    (bar meanwrite sesrace if race==2) ///
                    (bar meanwrite sesrace if race==3) ///
                    (bar meanwrite sesrace if race==4) ///
                    (scatteri 65 2 65 3.05, ///
                    recast(line) lw(medthick) mc(none) lc(black) lp(solid)) ///
                    (scatteri 65 2 65 3.05, ///
                    recast(dropline) base(62) lw(medthick) mc(none) lc(black) lp(solid)) ///
                    (scatteri 65 3.05 65 4.10, ///
                    recast(line) lw(medthick) mc(none) lc(black) lp(solid)) ///
                    (scatteri 65 3.05 65 4.10, ///
                    recast(dropline) base(62) lw(medthick) mc(none) lc(black) lp(solid)), ///
                    text(66 2.6 "{it:p}<.001", place(n) size(vsmall)) ///
                    text(66 3.8 "{it:p}<.001", place(n) size(vsmall)) ///
                    legend(row(4) position(right) ///
                    order(1 "Hispanic" 2 "Black" 3 "White" 4 "Other") ) ///
                    legend(region(lstyle(none))) ///
                    xlabel(0(5)15) ylabel(0(20)70) ///
                    xlabel( 3 "Low" 8 "Middle" 13 "High", noticks) ///
                    ytitle("Predicted Probability of Disiciplinary Action") ///
                    title("Medium Infractions") ///
                    plotregion(style(none))


                    Click image for larger version

Name:	78.JPG
Views:	1
Size:	138.5 KB
ID:	1714818

                    Comment


                    • #11
                      Originally posted by smith Jason View Post
                      I have a small dataset below for the illustrative purpose,

                      clear
                      input str11 race str6 ses meanwrite sdwrite n
                      "hispanic" "low" 46.11111 9.58442 9
                      "hispanic" "middle" 46.09091 7.133787 11
                      "hispanic" "high" 48.25 10.24288 4
                      "asian" "low" 58 12.12436 3
                      "asian" "middle" 55.8 7.463243 5
                      "asian" "high" 61.66667 4.618802 3
                      "african-amer" "low" 48.09091 8.431542 11
                      "african-amer" "middle" 48.66667 8.594572 6
                      "african-amer" "high" 47.66667 16.86219 3
                      "white" "low" 52.54167 9.084048 24
                      "white" "middle" 52.80822 9.222041 73
                      "white" "high" 56.70833 8.7372 48
                      end

                      encode race, generate(race2)
                      encode ses, generate(ses2)
                      drop ses race
                      ren race2 race
                      ren ses2 ses
                      order race ses meanwrite sdwrite n

                      g sesrace = race if ses == 1
                      replace sesrace = race+5 if ses == 2
                      replace sesrace = race+10 if ses == 3


                      twoway (bar meanwrite sesrace if race==1) ///
                      (bar meanwrite sesrace if race==2) ///
                      (bar meanwrite sesrace if race==3) ///
                      (bar meanwrite sesrace if race==4) , ///
                      legend(row(4) position(right) order(1 "Hispanic" 2 "Asian" 3 "Black" 4 "White") ) ///
                      xlabel(0(5)15) ///
                      ylabel(0(20)70) ///
                      xlabel( 3 "Low" 8 "Middle" 13 "High", noticks) ///
                      xtitle("Socio Economic Status") ytitle("Mean Writing Score")

                      I know I can borrow some codes from this thread at https://www.statalist.org/forums/for...ificance/page2.
                      to add brackets to represent the significant difference between "Asian" and "Black" for the Low SES group students.
                      Can someone give me some help with the code?
                      Thank you! [ATTACH=CONFIG]n1714663[/ATTACH]
                      The information of the setting of this color is available at https://imagecolorpicker.com/

                      Comment

                      Working...
                      X