Announcement

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

  • graph bar with multiple yvars with different scales

    I have prepared a bar graph with the following data and code. There are two yvars with different scales so I am using axis(1) and axis(2) and overlaying two graphs. The bars of the overlaid graphs overlap. Now I wish to separate the maroon and white bars so they do not overlap.
    Any suggestions would be highly welcome.
    Grant

    agegrp numptgrp agegr grpop
    5-9 869 05-09 27860
    10-14 240 10-14 23117
    15-19 150 15-19 17725
    20-24 104 20-24 16597
    25-29 98 25-29 11490
    30-34 87 30-34 10328
    35-39 80 35-39 6984
    40-44 77 40-44 7042
    45-49 60 45-49 4960
    50-54 51 50-54 3876
    55-59 31 55-59 3123
    60-64 39 60-64 2270
    65-69 30 65-69 1979
    70-74 20 70-74 1334
    75-79 8 75-79 1038
    80-84 9 80-84 515
    85-89 0 85-89 437
    90-94 4 90-94 173
    95-99 4 95-99 97
    100-104 0 >=100 34

    twoway bar numptgrp agegrp, color(maroon) yaxis(1) ytitle("Number of patients", axis(1) size(*.9)) ylabel(100(100)700, axis(1) labsize(*.9) nogrid) /*
    */ xtitle("Age group, in years", size(*.9)) xlabel(1(1)21, valuelabel labsize(*.8) angle(45)) ysc(titlegap(2)) graphregion(fcolor(white)) || /*
    */ bar grpop agegrp, fcolor(none) lcolor(black) yaxis(2) ylabel(5000 10000 15000 20000 25000 30000, axis(2) labsize(*.9)) /*
    */ ytitle(" " "Basse HDSS population", axis(2) size(*.9)) legend(order(1 "Patients" 2 "Population") position(1) ring(0) rows(2) size(*.9) /*
    */ region(lcolor(white)))

  • #2
    Welcome to Statalist. Please take the time to read through the FAQs to familiarize yourself with presenting data examples using the dataex command. This is crucial for your problem as it involves value labels. I have attempted to re-construct your data set and have used the labmask command (Stata Journal, by Nick Cox) to add the labels. The solution I propose involves creating adjacent consecutive sequences of numbers and using these as the basis for defining your x-axis. I skip a number between pairs of bars to introduce spaces. Finally, I would not have white bars on a white background for obvious reasons.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str22 agegrp float numptgrp str22 agegr float grpop
    "5-9"   869 "05-09" 27860
    "10-14" 240 "10-14" 23117
    "15-19" 150 "15-19" 17725
    "20-24" 104 "20-24" 16597
    "25-29"  98 "25-29" 11490
    "30-34"  87 "30-34" 10328
    "35-39"  80 "35-39"  6984
    "40-44"  77 "40-44"  7042
    "45-49"  60 "45-49"  4960
    "50-54"  51 "50-54"  3876
    "55-59"  31 "55-59"  3123
    "60-64"  39 "60-64"  2270
    "65-69"  30 "65-69"  1979
    "70-74"  20 "70-74"  1334
    "75-79"   8 "75-79"  1038
    "80-84"   9 "80-84"   515
    "85-89"   0 "85-89"   437
    "90-94"   4 "90-94"   173
    "95-99"   4 "95-99"    97
    end
    
    
    gen agegrp2 = (2*_n)-1 +_n-1
    gen agegr2=agegrp2+1
    *type findit labmask and follow instructions to install
    labmask agegr2, values(agegr)
    
    twoway bar numptgrp agegrp2, color(maroon) yaxis(1) ytitle("Number of patients", axis(1) size(*.9)) ///
    ylabel(100(100)700, axis(1) labsize(*.9) nogrid)  ysc(titlegap(2)) graphregion(fcolor(white)) || ///
    bar grpop agegr2, fcolor(gray) lcolor(black) yaxis(2) ///
    ylabel(5000 10000 15000 20000 25000 30000, axis(2) labsize(*.9)) ///
    ytitle(" " "Basse HDSS population", axis(2) size(*.9)) ///
    legend(order(1 "Patients" 2 "Population") position(1) ring(0) rows(2) size(*.9) ///
    region(lcolor(white))) xtitle("Age group, in years", size(*.9)) xlabel(2(3)56,  valuelabel labsize(*.8) angle(45) notick)

    Result:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.5 KB
ID:	1481744

    Comment


    • #3
      Because people tend to compare the height of bars when reading bar graphs, a better idea is to graph the log of number of patients and log of population so that you do not have a situation like the first pair of bars where the bar corresponding to the number of patients is taller than that corresponding to population. Here, in my opinion is a better representation. You can amend the value labels following #2 above.

      Code:
      foreach var of varlist numptgrp grpop{
      gen l`var'= log(`var')
      }
      
      twoway bar lnumptgrp agegrp2, color(maroon)  ytitle("Log number of patients and Log population", size(*.9))  ///
      (titlegap(2)) graphregion(fcolor(white)) || bar lgrpop agegr2, fcolor(gray%25) lcolor(black) ///
       legend(order(1 "Patients" 2 "Population") position(1) ring(0) rows(2) size(*.9) region(lcolor(white)))///
      ("Age group, in years", size(*.9)) xlabel(2(3)56,  valuelabel labsize(*.8) angle(45) notick)
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.3 KB
ID:	1481747

      Comment


      • #4
        Andrew, many thanks for your help and response. It is much appreciated. Regards, Grant

        Comment


        • #5
          Andrew Musau has some very good ideas there but I find myself queasy at bars on log scales and bars with arbitrary bases and logarithms that have to be back-transformed. Here is another take, square root scale, likely to seem more esoteric still.


          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str22 agegrp float numptgrp str22 agegr float grpop
          "5-9"   869 "05-09" 27860
          "10-14" 240 "10-14" 23117
          "15-19" 150 "15-19" 17725
          "20-24" 104 "20-24" 16597
          "25-29"  98 "25-29" 11490
          "30-34"  87 "30-34" 10328
          "35-39"  80 "35-39"  6984
          "40-44"  77 "40-44"  7042
          "45-49"  60 "45-49"  4960
          "50-54"  51 "50-54"  3876
          "55-59"  31 "55-59"  3123
          "60-64"  39 "60-64"  2270
          "65-69"  30 "65-69"  1979
          "70-74"  20 "70-74"  1334
          "75-79"   8 "75-79"  1038
          "80-84"   9 "80-84"   515
          "85-89"   0 "85-89"   437
          "90-94"   4 "90-94"   173
          "95-99"   4 "95-99"    97
          end
          
          
          gen agegrp2 = (2*_n)-1 +_n-1
          gen agegr2=agegrp2+1
          *type findit labmask and follow instructions to install
          labmask agegr2, values(agegr)
          
          gen numptgrp2 = sqrt(numptgrp) 
          gen grpop2 = sqrt(grpop) 
          
          * ssc inst mylabels 
          mylabels 1000 300 100 30 10 0, myscale(sqrt(@)) local(yla1) 
          mylabels 30000 10000 3000 1000 300 0, myscale(sqrt(@)) local(yla2) 
          
          twoway connected numptgrp2 agegrp2,  color(maroon) yaxis(1) ytitle("Number of patients", axis(1) size(*.9)) ///
          ylabel(`yla1', ang(h) axis(1) labsize(*.9) nogrid)  ysc(titlegap(2)) graphregion(fcolor(white)) || ///
          connected grpop2 agegr2,  ms(Dh) mc(black) lcolor(black) yaxis(2) ///
          ylabel(`yla2', ang(h) axis(2) labsize(*.9)) ///
          ytitle(" " "Basse HDSS population", axis(2) size(*.9)) ///
          legend(order(1 "Patients" 2 "Population") position(1) ring(0) rows(2) size(*.9) ///
          region(lcolor(white))) xtitle("Age group, in years", size(*.9)) xlabel(2(3)56,  valuelabel labsize(*.8) angle(45) notick)
          Click image for larger version

Name:	dual_axis.png
Views:	1
Size:	90.3 KB
ID:	1482709

          Comment

          Working...
          X