Announcement

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

  • Bar chart comparing rates

    Hello,

    Im using the twoway bar command in Stata MP v16.1.

    Im trying to create a bar chart comparing rates of hospitalisations for different ethnic groups after adjusting for different variables - so comparing rates by ethnicity in 4 different models, each one controlling for additional characteristics.

    The problem is that i want the bars to start at 1 and show the estimated rate above or below 1. The code below gets me bars that start at zero.

    Is there a way i can tweak the code below to give me what i need, or do i need to try something else like margins?

    I cant post the actual bar chart or data because the data is sensitive - so im hoping the code will give enough information.

    My data is long and has 5 variables: ethnicity (9 cats), model (4 cats), irr, lci, uci

    There is one additional variable eth_mod which structures the data by model and ethnicity...

    Code:
    gen eth_mod = model if ethnicity==9
    replace eth_mod = model+5 if ethnicity==8
    replace eth_mod = model+10 if ethnicity==7
    [etc - you get the picture re eth_mod so i wont type it all out]

    Code:
    twoway
    (bar irr eth_mod if model==1, horizontal) ///
    (bar irr eth_mod if model==2, horizontal) ///
    (bar irr eth_mod if model==3, horizontal) ///
    (bar irr eth_mod if model==4, horizontal) ///
    (rcap lci uci eth_mod, horizontal lcolor(black)),
    legend(row(1) order(1 "mod1" 2 "mod2" 3 "mod3" 4 "mod4") ///
    ylabel(2.5 "eth1) 7.5 "eth2" 12.5 "eth3" 17.5 "eth4" 22.5 "eth5" 27.7 "eth6" 32.5 "eth7" 37.5 "eth8" 42.5 "eth9", noticks angle(horizontal)) ///
    ytitle("") xtitle("irr") ///
    xline(1, lwidth(thin) lcolor(black) lpattern(solid))
    Grateful for any tips.

    Many thanks

  • #2
    See the base() option. You want base(1).

    I would use scatter || rspike here. with a reference line at 1. And log scale.
    Last edited by Nick Cox; 25 Nov 2022, 11:41.

    Comment


    • #3
      So simple! Thank you!

      Comment


      • #4
        Hi Nick,

        Returning to the above example, i have another problem. I have added a 5th model and as a result have lost the nice gap between the bars for each of the ethnicity categories. Is there a way to get a small gap between each ethnicity category?

        Code:
        twoway  (bar irr eth_mod if model==1, base(1) horizontal) ///
                (bar irr eth_mod if model==2, base(1) horizontal) ///
                (bar irr eth_mod if model==3, base(1) horizontal) ///
                (bar irr eth_mod if model==4, base(1) horizontal) ///
                (bar irr eth_mod if model==5, base(1) horizontal) ///
                (rcap lci uci eth_mod, horizontal lcolor(black)), ///
                legend(row(1) order(1 "mod1" 2 "mod2" 3 "mod3" 4 "mod4" 5 "mod5")) ///
                ylabel(2.5 "eth1" 7.5 "eth2" 12.5 "eth3" 17.5 "eth4" 22.5 "eth5" 27.5 "eth6" 32.5 "eth7" 37.5 "eth8" 42.5 "eth9", noticks angle(horizontal)) ///
                ytitle("") xtitle("xtitle") xline(1, lwidth(thin) lcolor(black) lpattern(solid)) graphregion(color(white)) title("") ///
                by(group, note("") title("title", size(*0.8)) graphregion(color(white)))
        Sorry, again, cant show the actual graph or data...will try to remember to post it on here once its been approved for release.

        Thank you,
        Joanna

        Comment


        • #5
          I should also say that i didnt go down the scatter || rspike route because i quite like the look of the bars. And, the scale is a rate-ratio with a smallish range from about .8 to 1.2 - so i dont think i need to go for a log-scale either. But always grateful for the suggestions.
          Best,
          Joanna

          Comment


          • #6
            All you need do is show fake data with the same structure, but for me trying to imagine what that looks like is not going to be fun.

            Comment


            • #7
              Would be easier if you added a data example that reproduces your issue.

              Comment


              • #8
                Ok, thanks. My data is long and has 5 variables: ethnicity (9 cats), model (5 cats), irr, lci, uci. And one additional variable eth_mod which structures the data by model and ethnicity. Is this enough to go off?

                code for gen eth_mod is in #1 the first post

                Im not really sure how to create 'fake' data....other than typing out by hand a made up dataset - is there a way to do this quickly? I cant even copy and paste the varnames and categorical variables because its all in a secure settling that wont allow my to copy (sorry!)
                Last edited by Joanna Davies; 24 Mar 2023, 08:39.

                Comment


                • #9
                  You can call up any number of random number functions. This is tough on you, but what otherwise what you're implying is that a reader who can't imagine your problem can only solve it by inventing their own data example or using an existing dataset as sandbox. That might still happen.

                  Comment


                  • #10
                    ok, fair point, standby, ive created a fake dataset (it actually didnt take that long!)...just working how to post...

                    Comment


                    • #11
                      Ok, here is my attempt at a very basic fake...all the irr lci uci just repeat im afraid - grateful for any advice on how to make it more realistic!

                      Code:
                      clear
                      input byte(ethnicity group model) float(irr lci uci eth_mod)
                      9 1 1 1.5  1   2  1
                      9 2 1 1.5  1   2  1
                      9 2 2 1.4 .9 1.9  2
                      9 1 2 1.4 .9 1.9  2
                      9 1 3 1.3 .8 1.8  3
                      9 2 3 1.3 .8 1.8  3
                      9 1 4 1.2 .7 1.7  4
                      9 2 4 1.2 .7 1.7  4
                      9 2 5 1.1 .6 1.6  5
                      9 1 5 1.1 .6 1.6  5
                      8 1 1 1.5  1   2  6
                      8 2 1 1.5  1   2  6
                      8 1 2 1.4 .9 1.9  7
                      8 2 2 1.4 .9 1.9  7
                      8 2 3 1.3 .8 1.8  8
                      8 1 3 1.3 .8 1.8  8
                      8 1 4 1.2 .7 1.7  9
                      8 2 4 1.2 .7 1.7  9
                      8 1 5 1.1 .6 1.6 10
                      8 2 5 1.1 .6 1.6 10
                      7 1 1 1.5  1   2 11
                      7 2 1 1.5  1   2 11
                      7 2 2 1.4 .9 1.9 12
                      7 1 2 1.4 .9 1.9 12
                      7 1 3 1.3 .8 1.8 13
                      7 2 3 1.3 .8 1.8 13
                      7 2 4 1.2 .7 1.7 14
                      7 1 4 1.2 .7 1.7 14
                      7 2 5 1.1 .6 1.6 15
                      7 1 5 1.1 .6 1.6 15
                      6 1 1 1.5  1   2 16
                      6 2 1 1.5  1   2 16
                      6 2 2 1.4 .9 1.9 17
                      6 1 2 1.4 .9 1.9 17
                      6 2 3 1.3 .8 1.8 18
                      6 1 3 1.3 .8 1.8 18
                      6 2 4 1.2 .7 1.7 19
                      6 1 4 1.2 .7 1.7 19
                      6 1 5 1.1 .6 1.6 20
                      6 2 5 1.1 .6 1.6 20
                      5 1 1 1.5  1   2 21
                      5 2 1 1.5  1   2 21
                      5 1 2 1.4 .9 1.9 22
                      5 2 2 1.4 .9 1.9 22
                      5 1 3 1.3 .8 1.8 23
                      5 2 3 1.3 .8 1.8 23
                      5 1 4 1.2 .7 1.7 24
                      5 2 4 1.2 .7 1.7 24
                      5 1 5 1.1 .6 1.6 25
                      5 2 5 1.1 .6 1.6 25
                      4 2 1 1.5  1   2 26
                      4 1 1 1.5  1   2 26
                      4 1 2 1.4 .9 1.9 27
                      4 2 2 1.4 .9 1.9 27
                      4 2 3 1.3 .8 1.8 28
                      4 1 3 1.3 .8 1.8 28
                      4 2 4 1.2 .7 1.7 29
                      4 1 4 1.2 .7 1.7 29
                      4 1 5 1.1 .6 1.6 30
                      4 2 5 1.1 .6 1.6 30
                      3 2 1 1.5  1   2 31
                      3 1 1 1.5  1   2 31
                      3 2 2 1.4 .9 1.9 32
                      3 1 2 1.4 .9 1.9 32
                      3 1 3 1.3 .8 1.8 33
                      3 2 3 1.3 .8 1.8 33
                      3 2 4 1.2 .7 1.7 34
                      3 1 4 1.2 .7 1.7 34
                      3 2 5 1.1 .6 1.6 35
                      3 1 5 1.1 .6 1.6 35
                      2 2 1 1.5  1   2 36
                      2 1 1 1.5  1   2 36
                      2 2 2 1.4 .9 1.9 37
                      2 1 2 1.4 .9 1.9 37
                      2 1 3 1.3 .8 1.8 38
                      2 2 3 1.3 .8 1.8 38
                      2 1 4 1.2 .7 1.7 39
                      2 2 4 1.2 .7 1.7 39
                      2 1 5 1.1 .6 1.6 40
                      2 2 5 1.1 .6 1.6 40
                      1 1 1 1.5  1   2 41
                      1 2 1 1.5  1   2 41
                      1 1 2 1.4 .9 1.9 42
                      1 2 2 1.4 .9 1.9 42
                      1 2 3 1.3 .8 1.8 43
                      1 1 3 1.3 .8 1.8 43
                      1 1 4 1.2 .7 1.7 44
                      1 2 4 1.2 .7 1.7 44
                      1 2 5 1.1 .6 1.6 45
                      1 1 5 1.1 .6 1.6 45
                      end
                      Last edited by Joanna Davies; 24 Mar 2023, 09:22.

                      Comment


                      • #12
                        Ah! Ive figured it out...i had forgotten how eth_mod worked...so basically i just needed to change the coding for eth_mod to the following:

                        Code:
                        gen eth_mod = model if ethnicity==9
                        replace eth_mod = model+6 if ethnicity==8
                        replace eth_mod = model+12 if ethnicity==7
                        replace eth_mod = model+18 if ethnicity==6
                        replace eth_mod = model+24 if ethnicity==5
                        replace eth_mod = model+30 if ethnicity==4
                        replace eth_mod = model+36 if ethnicity==3
                        replace eth_mod = model+42 if ethnicity==2
                        replace eth_mod = model+48 if ethnicity==1
                        sort eth_mod eth_mod ethnicity model
                        and then reposition the ethnicity labels

                        A problem shared...many thanks

                        Comment

                        Working...
                        X