Announcement

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

  • Adding Other Data as a Label to Bar Graph

    The graph created by the code below shows the total population of each region of the US. I am hoping to add a label to each bar displaying the percentage of the total, which I have calculcated using the code below. Is there an easy way to do this?

    I know I could create a stacked bar chart showing this group as a proportion of the total, but I'm specifically interested in whether it's possible to add this information as labels.

    Your advice and ideas would be greatly appreciated.

    sysuse census, clear
    collapse (sum) pop pop5_17, by(region)
    generate pr_5_17 = round(pop5_17/pop, 0.01)*100
    replace pop = pop/1000000
    rename pop pop_millions
    graph bar (asis) pop_millions, over(region) ytitle("Population (In Millions)")

    Best,
    Erika

  • #2
    First note that your code for the graph implied can be shortened to

    Code:
    sysuse census, clear
    graph bar (sum) pop, over(region) ytitle("Population (millions)") yla(0 20e6 "20" 40e6 "40" 60e6 "60" 80e6 "80")
    as graph bar will show sums without a prior collapse and the change of units can be achieved just via axis labels.

    I am a little puzzled on what percent you want shown, as the percents of the population aged 5-17 are not being shown on the graph just defined.

    However, this question resembles a previous one of yours within

    http://www.statalist.org/forums/foru...s-in-bar-graph

    and an answer is thus the same as given in that thread: to show unrelated text on a bar graph, you should use twoway bar and show text as added text or (more easily) as marker labels.

    Independently of that: round() with fractional arguments is not a good way to produce rounded values for display. Similar code for rounding to 1 or 2 decimal places would often bite you, for reasons often discussed under the heading of precision, i.e. exact decimals can only be held via binary approximations to them.

    This code shows some technique:

    Code:
    sysuse census, clear
    collapse (sum) pop pop5_17, by(region)
    generate pr5_17 = string(100 * pop5_17/pop, "%2.0f")
    
    twoway bar pop5_17 region, ytitle(Population 5-17 (millions)) yla(0 4e6 "4" 8e6 "8" 12e6 "12" 16e6 "16") ysc(r(0 18)) ///
    barw(0.5) xla(1/4, valuelabel noticks) || scatter pop5_17 region, mla(pr5_17) mlabpos(12) ms(none) legend(off) ///
    note(percents of total shown)
    Last edited by Nick Cox; 24 Jun 2014, 18:04.

    Comment


    • #3
      Dear Erika, as Nick has mentioned the solution supplied in the quoted thread is also applicable in this case:
      Code:
      sysuse census, clear
      
      collapse (sum) pop pop5_17, by(region)
      generate pr_5_17 = round(pop5_17/pop, 0.01)*100
      replace pop = pop/1000000
      rename pop pop_millions
      graph bar (asis) pop_millions, over(region) ytitle("Population (In Millions)") blabel(bar)
      
      
      local nb=`.Graph.plotregion1.barlabels.arrnels'
      forval i=1/`nb' {
        .Graph.plotregion1.barlabels[`i'].text[1]=`"`=string(`=pr_5_17[`i']',"%6.1f")'% of `=string(`=pop_mil[`i']',"%6.1f")'mln"'
      }
      .Graph.drawgraph

      Click image for larger version

Name:	bar_percent.png
Views:	1
Size:	15.0 KB
ID:	33297

      Best, Sergiy Radyakin
      Last edited by Sergiy Radyakin; 24 Jun 2014, 22:19.

      Comment

      Working...
      X