Announcement

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

  • Two-way bar chart with CI - how to organize colours?

    Hey Stata users
    I'm trying to make a figure for a manuscript, but is it is not working for me :-( I would like the colours to be by city, but for now they are by disease. I can see why, but I dont know how to change it. Any ideas will be highly appreciated!!
    Kind regards,
    Jannie
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	43.0 KB
ID:	1700528


    This is what the data for the figure looks like :
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(proportion sd LL UL) float(DM city) byte HTN float(htn dysl) byte dmhtnlip float(ALLCON Prop SD LL100 UL100 disease concordancecity)
       .223248122582039  .019453512950407845   .18507755303302637   .2614186921310516 1 1 0 . 0 0 .  22.32481 1.9453512 18.507755  26.14187 1  1
     .31279889469776817   .01919783871072949    .2751299945064912   .3504677948890451 1 2 0 . 0 0 .  31.27989  1.919784    27.513  35.04678 1  2
      .2509348972898948  .042974824311570486   .16661215700010007  .33525763757968957 1 3 0 . 0 0 .  25.09349 4.2974825 16.661215 33.525764 1  3
     .10759384896923126  .008970016150022702   .09000458181337262   .1251831161250899 . 1 1 1 0 0 . 10.759385  .8970016  9.000458 12.518312 2  5
      .1811117768846573  .010748161163981492   .16003575260715178   .2021878011621628 . 2 1 1 0 0 . 18.111177 1.0748161 16.003574  20.21878 2  6
     .15317989718979885   .01930007547997531   .11533445989427478  .19102533448532294 . 3 1 1 0 0 .  15.31799 1.9300076 11.533446 19.102533 2  7
    .020786662234541183 .0043265373146725815  .012302773307833432 .029270551161248933 . 1 . . 1 0 . 2.0786662  .4326537 1.2302773  2.927055 3  9
    .011842849506842123 .0030410353094238235 .0058796960862410345  .01780600292744321 . 2 . . 1 0 . 1.1842849  .3041035  .5879696 1.7806003 3 10
     .02066983022468096  .008174269985676571  .004640938394668132 .036698722054693794 . 3 . . 1 0 .  2.066983   .817427  .4640938  3.669872 3 11
     .24501928024220415  .011760421695000219   .22195831852271034  .26808024196169794 . 1 . . . 1 1  24.50193 1.1760422  22.19583 26.808023 4 13
        .33993681208878  .012513544167612134    .3153990556623092  .36447456851525084 . 2 . . . 1 1 33.993683 1.2513545 31.539906 36.447456 4 14
     .27819987584370576   .02257165197316216    .2339392178844658   .3224605338029457 . 3 . . . 1 1  27.81999 2.2571652  23.39392  32.24605 4 15
    end
    label values city city
    label def city 1 "Chennai", modify
    label def city 2 "Delhi", modify
    label def city 3 "Karachi", modify
    This is the code I use for the two-way bar:
    Code:
    gen concordancecity=city if disease==1
    replace concordancecity=city+4 if disease==2
    replace concordancecity=city+8 if disease==3
    replace concordancecity=city+12 if disease==4
    
    twoway    (bar Prop concordancecity if disease==1) ///
            (bar Prop concordancecity if disease==2) ///
            (bar Prop concordancecity if disease==3) ///
            (bar Prop concordancecity if disease==4) ///
            (rcap LL100 UL100 concordancecity), ///
            legend(row(1) order(1 "Chennai" 2 "Delhi" 3 "Karachi")) ///
            xlabel( 2 "Diabetes" 6 "Hypertension" 10 "Dyslipidemia" 14 "Combined", noticks) ///
            legend(ring(0) position(1) rows(1)) xtitle("") ytitle("Proportion (%)") ylabel(10 (10) 100, nogrid) graphregion( color(white))

  • #2
    You need to differentiate each bar and specify the color separately. This may be achieved more efficiently than I do below. You do not need to have the y-axis go up to 100%, unless the journal requires it.

    Code:
    egen dcity= group(disease city), label
    twoway  (bar Prop concordancecity if dcity==1, bc(blue*0.3)) ///
            (bar Prop concordancecity if dcity==2, bc(blue*0.6)) ///
            (bar Prop concordancecity if dcity==3, bc(blue*0.9)) ///
            (bar Prop concordancecity if disease==2, bc(none)) ///
            (bar Prop concordancecity if dcity==4, bc(blue*0.3)) ///
            (bar Prop concordancecity if dcity==5, bc(blue*0.6)) ///
            (bar Prop concordancecity if dcity==6, bc(blue*0.9)) ///
            (bar Prop concordancecity if disease==3, bc(none)) ///
            (bar Prop concordancecity if dcity==7, bc(blue*0.3)) ///
            (bar Prop concordancecity if dcity==8, bc(blue*0.6)) ///
            (bar Prop concordancecity if dcity==9, bc(blue*0.9)) ///
            (bar Prop concordancecity if disease==4, bc(none)) ///
            (bar Prop concordancecity if dcity==10, bc(blue*0.3)) ///
            (bar Prop concordancecity if dcity==11, bc(blue*0.6)) ///
            (bar Prop concordancecity if dcity==12, bc(blue*0.9)) ///
            (rcap LL100 UL100 concordancecity), ///
            legend(row(1) order(1 "Chennai" 2 "Delhi" 3 "Karachi")) ///
            xlabel( 2 "Diabetes" 6 "Hypertension" 10 "Dyslipidemia" 14 "Combined", noticks) ///
            legend(ring(0) position(1) rows(1)) xtitle("") ytitle("Percent") ///
            ylabel(10 (10) 50, nogrid) graphregion( color(white))

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	22.9 KB
ID:	1700558
    Last edited by Andrew Musau; 06 Feb 2023, 11:24.

    Comment


    • #3
      Details: A proportion is measured from 0 to 1. A percent is measured from 0 to 100%. From one mathematical point of view % is just notation for /100 so it is the same beast under the fur. But not all readers will see that and think that.

      More interesting: As Andrew Musau pointed out, why show the entire scale to 100% when nothing is above 40%?
      Last edited by Nick Cox; 06 Feb 2023, 11:16.

      Comment


      • #4
        Is this what you're looking for?

        Code:
        twoway  (bar Prop concordancecity if city==1) ///
                (bar Prop concordancecity if city==2) ///
                (bar Prop concordancecity if city==3) ///
                (bar Prop concordancecity if city==4) ///
                (rcap LL100 UL100 concordancecity), ///
                legend(row(1) order(1 "Chennai" 2 "Delhi" 3 "Karachi")) ///
                xlabel( 2 "Diabetes" 6 "Hypertension" 10 "Dyslipidemia" 14 "Combined", noticks) ///
                legend(ring(0) position(1) rows(1)) xtitle("") ytitle("Proportion (%)") ylabel(10 (10) 100, nogrid) graphregion( color(white))
        Last edited by Daniel Shin; 06 Feb 2023, 11:24. Reason: removed a line to make colors consistent across cities

        Comment


        • #5
          Thanks for these great inputs! Note taken on proportion vs. percentage. You are also right on why showing the entire scale!

          Comment

          Working...
          X