Announcement

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

  • Barlabels in graph bar

    Hi,
    I'm trying to add percentages to a graph bar of counts. Hope you'll be able to help.
    I'm working in a remote desktop where I cannot install e.g. tabplot
    /Anne


    Example of my data:
    clear
    input region year no_test tested percent_test
    1 2010 3 5 0.375
    1 2011 4 11 0.267
    1 2012 5 13 0.278
    2 2010 4 7 0.364
    2 2011 7 12 0.368
    2 2012 11 14 0.44

    end

    My code:
    graph bar no_test tested, over(year) over(region) stack blabel(percent_test)

  • #2
    You have to create the graph with twoway or delete and add new values using the graph editor with graph.

    Comment


    • #3
      Thank you Andrew!

      Comment


      • #4
        As I understand it the path you've chosen is doomed as graph bar doesn't allow bar labels to be anything but what is being plotted directly. That's not fatal as you can and arguably should change to twoway bar.

        What you've called a percentage looks like a proportion to me: to me, and many others, a percentage of this kind lies in [0, 100] and a proportion lies in [0, 1]. The difference is one of presentation but also best matched by careful use of terms.

        This example is offered more in terms of showing things you can do and choices I have made that you need to change according to your real data and your tastes.

        Code:
        clear
        input region year no_test tested percent_test
        1 2010 3 5 0.375
        1 2011 4 11 0.267
        1 2012 5 13 0.278
        2 2010 4 7 0.364
        2 2011 7 12 0.368
        2 2012 11 14 0.44
        end
        
        label def region 1 Ruritania 2 Fredonia 
        label val region region 
        
        gen all = tested + no_test 
        gen prop = strofreal(no_test / all, "%4.3f")  
        
        twoway bar all year, fc(none) lc(black) base(0) || bar no_test year, lc(stc2) fc(stc2*0.2) || scatter all year, ms(none) mlabel(prop) mlabcolor(black) mlabpos(12) legend(order(1 "all" 2 "no test")) xla(2010/2012) ytitle(Frequency) by(region, note(""))
        Here and often elsewhere what looks like stacking when you're done is just plotting a bar for a subset on top of a bar for the total. The trick can be extended to three or more components.

        Click image for larger version

Name:	region_year.png
Views:	1
Size:	28.2 KB
ID:	1724098


        Comment


        • #5
          Thank you again for your help, Nick! This did exactly what I wanted, though it did take me a bit to figure out where to place the title: , by (var, title("") )

          Comment

          Working...
          X