Announcement

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

  • Help with percentage label on bar graph with count command

    Hi,

    I have exposure data of PM2.5>100 with 594315 observations but want to count only
    PM2.5==5 and show the percentage of the count. I want to show the percentage on the bars or on the y-axis but at the moment, the graph is only showing the count number.

    My code looks like this:

    graph bar (count) pm if pm==5 & type==0, blabel(bar,format(%9.1f)) ///
    over(id, label(angle(90) labsize(small))) scale(*.4) ///
    legend(row(2)) over(type) bar(1, fcolor(blue)) ytitle("Number of observations") ///
    title(PM LOD Count)

    Just started working with stata not too long ago and not been able to find a count count command with percentage. Please kindly help. Thank you in advance

  • #2
    We can't see your data (no data example; you don't use a generally accessible dataset either) -- nor your graph.

    Here is the nearest analogue I can imagine:

    Code:
    sysuse auto, clear 
    
    graph bar (count) rep78 if rep78==5 & foreign==0, blabel(bar,format(%9.1f)) ///
    over(make, label(angle(90) labsize(small))) scale(*.4) ///
    legend(row(2)) over(foreign) bar(1, fcolor(blue)) ytitle("Number of observations") ///
    title(something appropriate)
    If you specify type == 0 and also over(type) that is legal but all you could expect from that is one bar for the single distinct value of type shown.

    Further specifying over(id) gives you one bar for each distinct value of id

    My analogue gives a count of 1 for each of two domestic cars with repair record 5. That's probably too far from your real example to be worth showing.

    Meanwhile, why ask for (count) if you don't want it? I am not following. Is what you want the percent of pm that is exactly 5?

    I could make some more guesses, but I think the only way to make real progress here is for you to show example data, or to translate your problem into one using a dataset shipped with Stata or available easily from the web.

    Comment


    • #3
      Thank you Nick for your response.

      I get the following graph below for the number of observations of pm that is exactly 5 but what I want is the percent of pm that is exactly 5.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int id byte type int date byte(hr min) float(pm_original pm)
      1 0 22422 6  6  59.60243  59.60243
      1 0 22422 6  7  63.32354  63.32354
      1 0 22422 6  8  21.90325 20.236584
      1 0 22422 6  9        10         5
      1 0 22422 6 10        10         5
      1 0 22422 6 11 34.536446  31.20311
      1 0 22422 6 12        10         5
      1 0 22422 6 13        10         5
      1 0 22422 6 14        10         5
      1 0 22422 6 15        10         5
      1 0 22422 6 16        10         5
      1 0 22422 6 17        10         5
      1 0 22422 6 18        10         5
      1 0 22422 6 19        10         5
      1 0 22422 6 20        10         5
      1 0 22422 6 21        10         5
      1 0 22422 6 22        10         5
      1 0 22422 6 23        10         5
      1 0 22422 6 24        10         5
      1 0 22422 6 25        10         5
      1 0 22422 6 26        10         5
      1 0 22422 6 27        10         5
      1 0 22422 6 28        10         5
      1 0 22422 6 29        10         5
      1 0 22422 6 30        10         5
      1 0 22422 6 31        10         5
      1 0 22422 6 32        10         5
      1 0 22422 6 33        10         5
      1 0 22422 6 34        10         5
      1 0 22422 6 35 128.00949 125.50948
      1 0 22422 6 36  241.3503  241.3503
      1 0 22422 6 37 101.15939 101.15939
      1 0 22422 6 38  153.9389  153.9389
      1 0 22422 6 39  66.79918  66.79918
      1 0 22422 6 40  39.91228  39.07895
      1 0 22422 6 41  25.76668  23.26668
      1 0 22422 6 42        10         5
      1 0 22422 6 43        10         5
      1 0 22422 6 44        10         5
      1 0 22422 6 45        10         5
      1 0 22422 6 46        10         5
      1 0 22422 6 47        10         5
      1 0 22422 6 48        10         5
      1 0 22422 6 49        10         5
      1 0 22422 6 50        10         5
      1 0 22422 6 51        10         5
      1 0 22422 6 52        10         5
      1 0 22422 6 53        10         5
      1 0 22422 6 54        10         5
      1 0 22422 6 55        10         5
      1 0 22422 6 56        10         5
      1 0 22422 6 57        10         5
      1 0 22422 6 58        10         5
      1 0 22422 6 59        10         5
      1 0 22422 7  0 10.353335  6.186669
      1 0 22422 7  1 17.076775  13.74344
      1 0 22422 7  2        10         5
      1 0 22422 7  3        10         5
      1 0 22422 7  4        10         5
      1 0 22422 7  5        10         5
      1 0 22422 7  6        10         5
      1 0 22422 7  7        10         5
      1 0 22422 7  8        10         5
      1 0 22422 7  9        10         5
      1 0 22422 7 10        10         5
      1 0 22422 7 11        10         5
      1 0 22422 7 12        10         5
      1 0 22422 7 13        10         5
      1 0 22422 7 14        10         5
      1 0 22422 7 15        10         5
      1 0 22422 7 16        10         5
      1 0 22422 7 17        10         5
      1 0 22422 7 18        10         5
      1 0 22422 7 19        10         5
      1 0 22422 7 20        10         5
      1 0 22422 7 21        10         5
      1 0 22422 7 22        10         5
      1 0 22422 7 23        10         5
      1 0 22422 7 24        10         5
      1 0 22422 7 25        10         5
      1 0 22422 7 26        10         5
      1 0 22422 7 27        10         5
      1 0 22422 7 28        10         5
      1 0 22422 7 29        10         5
      1 0 22422 7 30        10         5
      1 0 22422 7 31        10         5
      1 0 22422 7 32        10         5
      1 0 22422 7 33        10         5
      1 0 22422 7 34        10         5
      1 0 22422 7 35        10         5
      1 0 22422 7 36        10         5
      1 0 22422 7 37        10         5
      1 0 22422 7 38        10         5
      1 0 22422 7 39        10         5
      1 0 22422 7 40        10         5
      1 0 22422 7 41        10         5
      1 0 22422 7 42        10         5
      1 0 22422 7 43        10         5
      1 0 22422 7 44   19.4846 16.151266
      1 0 22422 7 45        10         5
      end
      format %td date
      label values id id
      label def id 1 "CK0001", modify
      label values type type
      label def type 0 "Control", modify

      Click image for larger version

Name:	PM trend estimate.png
Views:	1
Size:	189.4 KB
ID:	1652046

      Last edited by Hepzibah Ade; 25 Feb 2022, 10:59.

      Comment


      • #4

        PM == 5 is 1 if true and 0 if false, so something like this may help.

        Code:
        egen fraction = mean(pm== 5), by(type id)
        
        egen tag = tag(type id) 
        
        graph bar (asis) fraction if tag & type == 0, over(id)

        Comment


        • #5
          Thank you very much Nick. It worked perfectly.

          Comment

          Working...
          X