Announcement

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

  • how to hide zero percent in blabel option of graph bar

    Dear Stata Users,

    I have a question as to option for labeling bars. When I graph bar charts of grouped data (percents of different activity of three business types), I want to add a label on each bar. However, there are some zero percent in my data, and the -blabel()- option will display them as same as non-zero values. And my question is how to hide zero percents in corresponding bars.

    Code:
    graph bar v2 v3 v4 v5, over(v1) stack blabel(bar, format(%9.1f) posi(center)) nofill legend(row(1) order(1 "None" 2 "One" 3 "Two" 4 "Three"))
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 v1 float(v2 v3 v4 v5)
    "business1" 31.47 68.53    0   0
    "business2" 67.33 29.53 3.13   0
    "business3" 78.93   7.4 13.2 .47
    end
    Click image for larger version

Name:	business.png
Views:	1
Size:	25.4 KB
ID:	1462314
    Last edited by Chen Samulsion; 17 Sep 2018, 06:05.

  • #2
    Code:
    replace v5 = . if v5 == 0
    replace v4 = . if v4 == 0

    Comment


    • #3
      Stacked bars are a popular design but they often work badly, as I find myself arguing frequently here. In addition to the usual details you have to work hard even to see the text labels in this case.

      You've not shown us the graph command you used and also (I guess) omitted to show the variable labels needed in this case to reproduce the graph.

      In this particular case I suspect that replacing zeros with missings would do what you're asking.

      More generally I would recommend a different design using tabplot (Stata Journal). With tabplot, just as with graph bar, zeros just correspond to bars of zero length, which necessarily are hard to see, except with tabplot you can see where they would be. Search the forum for other mentions of this command.

      Here is some sample code. I haven't tried fooling with the graph bar colours. If you continued to prefer the stacked design, it would be a very good idea to use more subdued colours. Conversely, you can choose your won colours with tabplot too. With tabplot there is no need for the fruit salad/technicolour dreamcoat approach of numerous different colours.

      Code:
      clear
      input str9 v1 float(v2 v3 v4 v5)
      "business1" 31.47 68.53    0   0
      "business2" 67.33 29.53 3.13   0
      "business3" 78.93   7.4 13.2 .47
      end
      label var v2 "None"
      label var v3 "One" 
      label var v4 "Two" 
      label var v5 "Three"  
      
      set scheme s1color 
      
      preserve 
      
      mvdecode v2-v5, mv(0) 
      
      graph bar (asis) v2-v5, over(v1) stack  blabel(bar, pos(center)) name(G1, replace) 
      
      rename v1 what 
      
      rename (v2 v3 v4 v5) (v0 v1 v2 v3) 
      
      reshape long v, i(what) j(whatever) 
      
      tabplot whatever what [iw=v] , showval yasis bfcolor(ltblue) blcolor(blue) name(G2, replace) 
      
      restore
      Click image for larger version

Name:	yatabplot1.png
Views:	1
Size:	17.1 KB
ID:	1462325



      Click image for larger version

Name:	yatabplot2.png
Views:	1
Size:	13.8 KB
ID:	1462326

      Comment


      • #4
        Dear Scott Merryman and Nick Cox, thank you both of you. Your solutions are nice. I have used tabplot command before, and it indeed illustrate data structure more clear than bar charts, although it seems a bit anti-traditional.

        Comment


        • #5
          "anti-traditional"? "untraditional", perhaps, depending on your definition of a tradition.

          In the help file I give many examples of literature references going back to the 1930s and I continue to add references as I come across them. Necessarily you can't see the latest private version of the help on my main machine from 4 June 2018 which has even more references than any public version

          I guess you're right in the sense that many graphics books don't even show this design. That gives me a small role in life, to keep banging on about it.

          (My post was a long time in preparation as a meal and a meeting intervened, so only just now have I seen Scott Merryman's answer, which doesn't contradict mine.)

          Comment

          Working...
          X