Announcement

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

  • Seek Help for the Creation of Bar Charts in Stata Code



    Hi, I have a small dataset below, which is about a table of the disciplinary action predicted probabilities
    that students from different ethnic groups may face after different levels of disciplinary infractions.

    clear
    input byte race outcome infraction1 infraction2 infraction3
    1 1 0.684 0.699 0.229
    2 1 0.690 0.465 0.127
    3 1 0.712 0.577 0.291
    4 1 0.718 0.547 0.148
    1 2 0.284 0.388 0.527
    2 2 0.255 0.390 0.368
    3 2 0.240 0.329 0.433
    4 2 0.235 0.353 0.392
    1 3 0.069 0.146 0.354
    2 3 0.056 0.148 0.511
    3 3 0.050 0.094 0.277
    4 3 0.048 0.111 0.418
    end

    Among them,
    race--1(black);
    race--2-hispanic;
    race--3-White;
    race--4-Other

    outcome-1 (receiving minor disciplinary);
    outcome-2-(receiving medium disciplinary action);
    ​​​​​​​outcome-3- (receiving severe disciplinary action)

    infraction1-(the predicted probability of receiving minor disciplinary action);
    infraction2- (the predicted probability of receiving medium disciplinary action);
    infraction3- (the predicted probability of receiving severe disciplinary action)

    What I want is to write Stata code to achieve the requirements below,

    To aid in the visualization of the results (utilizing the updated predicted probabilities), it would be beneficial to compile a set of bar graphs.
    A potential approach involves generating 3 bar graphs corresponding to each infraction type (1, 2, and 3).
    For instance, the initial bar graph would depict the likelihood, based on race, of students who engage in minor infractions
    receiving either low, medium, or high levels of disciplinary action.

    Thank you for your Stata code or guidance.

  • #2
    Can someone help me? Thank you!

    Comment


    • #3
      Bumping so quickly is deprecated here. See #1 at https://www.statalist.org/forums/help#adviceextras

      I am not sure I understand these data, but here is one stab at a plot using tabplot from the Stata Journal.

      Code:
      clear
      input byte race outcome infraction1 infraction2 infraction3
      1 1 0.684 0.699 0.229
      2 1 0.690 0.465 0.127
      3 1 0.712 0.577 0.291
      4 1 0.718 0.547 0.148
      1 2 0.284 0.388 0.527
      2 2 0.255 0.390 0.368
      3 2 0.240 0.329 0.433
      4 2 0.235 0.353 0.392
      1 3 0.069 0.146 0.354
      2 3 0.056 0.148 0.511
      3 3 0.050 0.094 0.277
      4 3 0.048 0.111 0.418
      end
      
      reshape long infraction, i(race outcome) j(which)
      rename infraction probability
      
      label def outcome 1 minor 2 medium 3 severe
      label val outcome outcome
      label var outcome "Disciplinary action"
      
      label def race 1 black 2 Hispanic 3 white 4 other
      label val race race  
      label var race "Race"
      
      label val which outcome
      
      tabplot outcome race [iw=probability], yreverse by(which, t1title(Predicted probability)  note("") row(1)) showval(format(%4.3f))

      Comment


      • #4
        Thank you for your kindly help!
        If I just want the separate graph vertically by infraction at different level, how can I split the generated big graph?

        Comment


        • #5
          If you just want each subgraph one at a time, you don't need to reshape. Your pattern is just


          Code:
           
           tabplot outcome race [iw=infraction1], yreverse t1title(Predicted probability) showval(format(%4.3f))
          or infraction2 or infraction3.

          Comment


          • #6
            Thank you very much!

            Comment


            • #7
              I am trying to distinguish the graph for each group (race) by different colors, however, the command "asyvars" does not work.
              Could you please show me how to do this in Stata code? Thank you!

              Comment


              • #8
                asyvars is an option, not a command. But indeed it does not work here because tabplot is a wrapper for graph twoway -- not for graph bar or a related command.

                What you want here is documented

                separate(sepspec) specifies that bars associated with different sepspec will be shown differently, most obviously using different colors. sepspec is passed as an argument to the by()
                option of separate, except that references to @ are first translated to be references to the quantity being plotted.

                A call to separate() may be supplemented with calls to options bar1() ... bar20() or to barall(). The arguments should be options of twoway rbar.

                Options bar1() to bar20() are provided to allow overriding the defaults on up to 20 categories, the first, second, etc., shown. The limit of 20 is arbitrary and more than any
                user should really want. Option barall() is available to override the defaults for all bars. Any bar#() option always overrides barall(). Thus if you wanted thicker blwidth() on
                all bars, you could specify barall(blwidth(thick)). If you wanted to highlight the first category only, you could specify bar1(blwidth(thick)).




                Here is code from #2 with extra options at the end. Clearly you should choose your own colours.

                Code:
                clear
                input byte race outcome infraction1 infraction2 infraction3
                1 1 0.684 0.699 0.229
                2 1 0.690 0.465 0.127
                3 1 0.712 0.577 0.291
                4 1 0.718 0.547 0.148
                1 2 0.284 0.388 0.527
                2 2 0.255 0.390 0.368
                3 2 0.240 0.329 0.433
                4 2 0.235 0.353 0.392
                1 3 0.069 0.146 0.354
                2 3 0.056 0.148 0.511
                3 3 0.050 0.094 0.277
                4 3 0.048 0.111 0.418
                end
                
                reshape long infraction, i(race outcome) j(which)
                rename infraction probability
                
                label def outcome 1 minor 2 medium 3 severe
                label val outcome outcome
                label var outcome "Disciplinary action"
                
                label def race 1 black 2 Hispanic 3 white 4 other
                label val race race  
                label var race "Race"
                
                label val which outcome
                
                tabplot outcome race [iw=probability], yreverse by(which, t1title(Predicted probability)  note("") row(1)) showval(format(%4.3f)) ///
                separate(race) bar1(lcolor(black) fcolor(black*0.2)) bar2(lcolor(red) fcolor(red*0.2)) bar3(lcolor(blue) fcolor(blue*0.2)) bar4(lcolor(magenta) fcolor(magenta*0.2))

                Comment


                • #9
                  Thank you! I have to bother you because I want to add the legend to represent different race corresponding to 4 colors in the graph although I did some learning efforts by myself.
                  I tried to add lab(1 "Black") lab(2 "Hispanic") lab(3 "White") lab(4 "Other") to the code you provided in different places, it does not work for me. Could you please help me again?
                  Thank you!

                  Comment


                  • #10
                    tabplot outcome race [iw=probability], yreverse by(which, t1title(Predicted Probability) note("") row(1)) showval(format(%4.3f))separate(race) bar1(lcolor(black) fcolor(black*0.2))
                    bar2(lcolor(red) fcolor(red*0.2)) bar3(lcolor(blue) fcolor(blue*0.2)) bar4(lcolor(magenta) fcolor(magenta*0.2)) legend(label(1 Black) label(2 Hispanic) label(3 White) label(4 Other)) legend(order(1 2 3 4))

                    Comment


                    • #11
                      A legend would, in my view, be superfluous with this design. Each column in the graphical table is labelled at the bottom. Adding colours is allowed decoration, but does not add information to the graph. If you think the colours need explanation, why add them?

                      If you don't like the design, that is fine by me, but then you need to write your own code.

                      Comment

                      Working...
                      X