Announcement

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

  • How to Add Values to the Bar Charts Created by Twoway Command in Stata?

    Hello,
    Below is my Stata code to generate a bar chart created by Twoway command. As what you can see in the generated graph after running the code, it does not have any value.
    I want to add values to the graph like the attached snapshot.
    Can someone help me with Stata code?
    Thank you!


    clear
    input byte race outcome minor medium severe
    1 1 .7048 .5184 .3047
    2 1 .7004 .4876 .1779
    3 1 .7232 .5578 .3354
    4 1 .7284 .5508 .2053
    1 2 .2429 .3565 .4077
    2 2 .2461 .3705 .3543
    3 2 .2297 .3363 .4086
    4 2 .2258 .3401 .3744
    1 3 .0522 .1251 .2875
    2 3 .0535 .1419 .4687
    3 3 .0471 .1059 .2561
    4 3 .0457 .1091 .4204
    end

    // Set graph style
    grstyle init

    // Add race labels
    label define race_label 1 ///
    "Black" 2 "Hispanic" ///
    3 "White" 4 "Other"
    label values race race_label

    // Add outcome labels
    label define outcome_label 1 ///
    "Low Disciplinary Action" ///
    2 "Medium Disciplinary Action" ///
    3 "High Disciplinary Action"
    label values outcome outcome_label


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


    set scheme s1color
    // Minor Infractions Graph with symbols that show statistical significance.
    tw (bar minor outcomerace if race==1, bcolor("27 133 255")) ///
    (bar minor outcomerace if race==2, bcolor("213 17 89")) ///
    (bar minor outcomerace if race==3, bcolor("1 191 127")) ///
    (bar minor outcomerace if race==4, bcolor("255 212 1")), ///
    legend(row(4) position(right) ///
    order(1 "Hispanic" 2 "Black" 3 "White" 4 "Other") ) ///
    legend(region(lstyle(none))) ///
    xlabel(0(5) 15) ylabel(0(0.2) 0.8) ///
    xlabel( 3 "Low" 8 "Medium" 13 "High", noticks) ///
    ytitle("Predicted Probability of Disiciplinary Action") ///
    title("Minor Infractions") xtitle("") ///
    plotregion(style(none))

    Click image for larger version

Name:	Minor Infractions.JPG
Views:	1
Size:	136.0 KB
ID:	1715260






  • #2
    If you google "twoway bar labels", the following are some relevant threads corresponding to this search:

    https://www.statalist.org/forums/for...ning-bar-chart

    https://www.statalist.org/forums/for...s-between-bars

    https://www.statalist.org/forums/for...way-bar-graphs

    Last edited by Andrew Musau; 28 May 2023, 15:44.

    Comment


    • #3
      Thank you for the resources and they are helpful. With them, I can add the values to the bar charts.
      However, I try to adjust the font size of the value and it seems that text (size) does not work.
      Could you please give me some help or hints?
      Thank you!

      clear
      input byte race outcome minor medium severe
      1 1 .7048 .5184 .3047
      2 1 .7004 .4876 .1779
      3 1 .7232 .5578 .3354
      4 1 .7284 .5508 .2053
      1 2 .2429 .3565 .4077
      2 2 .2461 .3705 .3543
      3 2 .2297 .3363 .4086
      4 2 .2258 .3401 .3744
      1 3 .0522 .1251 .2875
      2 3 .0535 .1419 .4687
      3 3 .0471 .1059 .2561
      4 3 .0457 .1091 .4204
      end

      // Set graph style
      grstyle init

      // Add race labels
      label define race_label 1 ///
      "Black" 2 "Hispanic" ///
      3 "White" 4 "Other"
      label values race race_label

      // Add outcome labels
      label define outcome_label 1 ///
      "Low Disciplinary Action" ///
      2 "Medium Disciplinary Action" ///
      3 "High Disciplinary Action"
      label values outcome outcome_label


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


      set scheme s1color
      // Minor Infractions Graph with symbols that show statistical significance.
      tw (bar minor outcomerace if race==1, msymbol(none) mlabel(minor) mlabposition(2.5) bcolor("27 133 255")) ///
      (bar minor outcomerace if race==2, msymbol(none) mlabel(minor) mlabposition(2.5) bcolor("213 17 89")) ///
      (bar minor outcomerace if race==3, msymbol(none) mlabel(minor) mlabposition(2.5) bcolor("1 191 127")) ///
      (bar minor outcomerace if race==4, msymbol(none) mlabel(minor) mlabposition(2.5) bcolor("255 212 1")), ///
      legend(row(4) position(right) ///
      order(1 "Hispanic" 2 "Black" 3 "White" 4 "Other") ) ///
      legend(region(lstyle(none))) ///
      xlabel(0(5) 15) ylabel(0(0.2) 0.8) ///
      xlabel( 3 "Low" 8 "Medium" 13 "High", noticks) ///
      ytitle("Predicted Probability of Disiciplinary Action") ///
      title("Minor Infractions") xtitle("") ///
      plotregion(style(none))

      Click image for larger version

Name:	77.JPG
Views:	1
Size:	139.5 KB
ID:	1715302





      Comment


      • #4
        I got the solutions as follows,
        clear
        input byte race outcome minor medium severe
        1 1 .7048 .5184 .3047
        2 1 .7004 .4876 .1779
        3 1 .7232 .5578 .3354
        4 1 .7284 .5508 .2053
        1 2 .2429 .3565 .4077
        2 2 .2461 .3705 .3543
        3 2 .2297 .3363 .4086
        4 2 .2258 .3401 .3744
        1 3 .0522 .1251 .2875
        2 3 .0535 .1419 .4687
        3 3 .0471 .1059 .2561
        4 3 .0457 .1091 .4204
        end

        // Set graph style
        grstyle init

        // Add race labels
        label define race_label 1 ///
        "Black" 2 "Hispanic" ///
        3 "White" 4 "Other"
        label values race race_label

        // Add outcome labels
        label define outcome_label 1 ///
        "Low Disciplinary Action" ///
        2 "Medium Disciplinary Action" ///
        3 "High Disciplinary Action"
        label values outcome outcome_label


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


        set scheme s1color
        // Minor Infractions Graph with symbols that show statistical significance.
        tw (bar minor outcomerace if race==1, msymbol(none) mlabel(minor) mlabposition(2.5) mlabsize(vsmall) bcolor("27 133 255")) ///
        (bar minor outcomerace if race==2, msymbol(none) mlabel(minor) mlabposition(2.5) mlabsize(vsmall) bcolor("213 17 89")) ///
        (bar minor outcomerace if race==3, msymbol(none) mlabel(minor) mlabposition(2.5) mlabsize(vsmall) bcolor("1 191 127")) ///
        (bar minor outcomerace if race==4, msymbol(none) mlabel(minor) mlabposition(2.5) mlabsize(vsmall) bcolor("255 212 1")), ///
        legend(row(4) position(right) ///
        order(1 "Hispanic" 2 "Black" 3 "White" 4 "Other") ) ///
        legend(region(lstyle(none))) ///
        xlabel(0(5) 15) ylabel(0(0.2) 0.8) ///
        xlabel( 3 "Low" 8 "Medium" 13 "High", noticks) ///
        ytitle("Predicted Probability of Disiciplinary Action") ///
        title("Minor Infractions") xtitle("") ///
        plotregion(style(none))

        Click image for larger version

Name:	44.JPG
Views:	1
Size:	131.8 KB
ID:	1715304





        Comment


        • #5
          I would like to have the values tightly attached to the top surface of the bar chart. Could anyone provide me with some suggestions or directions?








          Last edited by smith Jason; 28 May 2023, 17:53.

          Comment


          • #6
            Can someone help me with my request to have the values tightly attached to the top surface of the bar chart?
            Thank you!




            Comment


            • #7
              I guess nobody knows what you mean that isn't what has been explained already.

              Comment


              • #8
                It means, let us take 0.2429 as an example, that 0.2429 should be tightly attached to the top surface of the blue bar in the "Medium" group in the middle of the graph.
                what i want looks like the attached snapshot. I just want the value can be displayed right on the upper line of the bar chart.
                Click image for larger version

Name:	111.JPG
Views:	1
Size:	59.5 KB
ID:	1715412





                Comment


                • #9
                  I see.

                  I think Stata's working in your best interests by not going so close. There's no Stata handle here that I know except guessing at a lower y coordinate. mlabpos(0) is not what you want, by the way.

                  Comment


                  • #10
                    Thank you! Below is web address of the snapshot above
                    https://blog.uvm.edu/tbplante/2022/1...igures-graphs/
                    Also, I found a thread that address such issue at https://www.statalist.org/forums/for...raphs-in-stata

                    I try to mimic their code and modify it for my personal use.
                    However, it does not work.














                    Last edited by smith Jason; 29 May 2023, 17:57.

                    Comment


                    • #11
                      Frustrated

                      Comment


                      • #12
                        can someone help me?




                        Comment


                        • #13
                          can someone help me?




                          Comment


                          • #14
                            Repetitions don't make the question easier, but this from #7


                            I guess nobody knows what you mean that isn't what has been explained already.
                            still seems to fit the case -- because this from #10

                            I try to mimic their code and modify it for my personal use.
                            However, it does not work.

                            is a statement, not a question. Code you don't show us doesn't work. That is not a question.

                            If we look across at the linked thread https://www.statalist.org/forums/for...mmand-in-stata then in reference to braces you said

                            Thank you! The symbol is OK, but it is very ugly. Can we adjust the size for beauty?
                            to which (1) I don't know what would be less ugly in your view (2) the previous answer in that thread already showed how to change the symbol size.


                            Sorry, I really don't know what you want here that hasn't been said already, and my guess is that nobody else knows either.


                            Last edited by Nick Cox; 06 Jun 2023, 02:14.

                            Comment


                            • #15
                              The graph in $8 clearly convey what I want. Thank you!




                              Comment

                              Working...
                              X