Announcement

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

  • Label the bar graph categories along the axis rather than in the legend

    I am trying to graph the number of students in each percentile of an online education program. I am using the "blabel" command to show the number of students within each percentile, but am also hoping to label each bar with the decile category along the axis rather than within a legend. The code that I'm currently using to make this graph is below, but I would prefer to have the bar category labeled beneath the bar along the axis, rather than in the legend. Do you have any recommendations?

    graph bar (sum) perc1 perc2 perc3 perc4 perc5 perc6 perc7 perc8 perc9 perc10, ///
    blabel(total, size(vsmall) color(gs5) format(%9.0f)) ///
    graphregion(color(white)) bgcolor(white) bargap(0) ///
    legend(size(small) label(1 "10") label(2 "20") label(3 "30") label(4 "40") label(5 "50") ///
    label(6 "60") label(7 "70") label(8 "80") label(9 "90") label(10 "100"))

    Thank you for any suggestions!

  • #2
    You are more likely to get help if you present a data example. Read the FAQs on how to do this using dataex. I will nevertheless attempt a solution, but do post back with a data example if the code fails

    Code:
    *RESHAPE DATA TO LONG FORM
    gen n= _n
    reshape long perc, i(n)
    gen double percentile= _j* 10
    graph bar (sum) perc, over(percentile) blabel(total, size(vsmall) color(gs5) format(%9.0f)) graphregion(color(white))
    Actually, the easiest way is to add

    Code:
    showyvars legend(off)
    and this is documented in -help graph bar-

    Code:
    graph bar (sum) perc, over(percentile) blabel(total, size(vsmall) ///
    color(gs5) format(%9.0f)) graphregion(color(white))  asyvars showyvars legend(off)  b1title("Percentile")
    Last edited by Andrew Musau; 13 Feb 2018, 16:06.

    Comment


    • #3
      Hi Andrew - Thank you for the rapid response. I really like the idea of using "showyvars," but when I do so, I end up with the "sum of perc1", "sum of perc2", etc. along the x-axis. Is there any way to have Stata instead show the *label* for these variables along the x-axis?

      Many thanks for your suggestion!
      I am actually trying to do this with two datasets now - both for this percentile question and in order to show how many students are enrolled within each country, where I have run into a similar problem. In the below graph, when I use showyvars, I get "sum of africa" rather than just the label of "Africa" :

      graph bar (sum) africa south_asia se_asia lac mena europe north_am, showyvars

      Any recommendations for how to display the label in these two use-cases rather than "sum of X" would be much appreciated!

      Comment


      • #4
        PS. Here is the dataex code that you asked about:
        (This is for the latter example on region data, but concerns the same problem)

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(africa south_asia se_asia lac mena europe north_am)
        1 0 0 0 0 0 0
        0 1 1 0 0 0 0
        1 1 0 0 0 1 0
        0 1 0 0 0 0 0
        0 0 0 1 0 0 0
        0 0 1 1 0 0 0
        0 1 1 0 0 0 0
        0 0 0 0 0 0 0
        0 0 0 1 0 0 0
        1 0 0 0 0 0 0
        0 0 0 0 0 0 1
        0 0 0 0 0 0 1
        0 0 0 0 0 0 0
        0 0 0 0 0 0 1
        0 0 0 1 0 0 1
        0 1 0 0 0 0 0
        1 1 0 1 0 0 0
        1 0 0 0 0 0 0
        0 1 0 0 0 1 0
        1 0 0 0 1 0 0
        end
        label var africa "Africa" 
        label var south_asia "South Asia" 
        label var se_asia "Southeast Asia" 
        label var lac "Latin America and the Caribbean" 
        label var mena "Middle East and North Africa" 
        label var europe "Europe" 
        label var north_am "North America"

        Comment


        • #5
          Thanks for the data example. Here are the two flavors in #2

          Code:
          rename (africa south_asia se_asia lac mena europe north_am) (region#), addnumber(1)
          gen n= _n
          reshape long region, i(n)
          label define _j 1 "Africa" 2 "South Asia" 3 "S.E.Asia" 4 "LAC" 5 "MENA" 6 "Europe" 7 "N. America"
          label values _j _j
          graph bar (sum) region, over(_j, label) blabel(total)
          graph bar (sum) region, over(_j, label) asyvars showyvars legend(off) blabel(total)
          Click image for larger version

Name:	sgrp1.png
Views:	1
Size:	17.3 KB
ID:	1430171

          Click image for larger version

Name:	sgrp2.png
Views:	1
Size:	17.1 KB
ID:	1430172



          You may want to amend the labels, for example, "South Asia" to "S. Asia".

          Comment


          • #6
            Different direction, so to speak: I'd suggest graph hbar for readability. Even with fairly short category names after Andrew's renaming some are running into one another.

            Here for anyone's interest and convenience is code from #4 and #5 and some sample different code at end:


            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(africa south_asia se_asia lac mena europe north_am)
            1 0 0 0 0 0 0
            0 1 1 0 0 0 0
            1 1 0 0 0 1 0
            0 1 0 0 0 0 0
            0 0 0 1 0 0 0
            0 0 1 1 0 0 0
            0 1 1 0 0 0 0
            0 0 0 0 0 0 0
            0 0 0 1 0 0 0
            1 0 0 0 0 0 0
            0 0 0 0 0 0 1
            0 0 0 0 0 0 1
            0 0 0 0 0 0 0
            0 0 0 0 0 0 1
            0 0 0 1 0 0 1
            0 1 0 0 0 0 0
            1 1 0 1 0 0 0
            1 0 0 0 0 0 0
            0 1 0 0 0 1 0
            1 0 0 0 1 0 0
            end
            label var africa "Africa" 
            label var south_asia "South Asia" 
            label var se_asia "Southeast Asia" 
            label var lac "Latin America and the Caribbean" 
            label var mena "Middle East and North Africa" 
            label var europe "Europe" 
            label var north_am "North America"
            
            rename (africa south_asia se_asia lac mena europe north_am) (region#), addnumber(1)
            gen n= _n
            reshape long region, i(n)
            label define _j 1 "Africa" 2 "South Asia" 3 "S.E.Asia" 4 "LAC" 5 "MENA" 6 "Europe" 7 "N. America"
            label values _j _j
            graph hbar (sum) region, over(_j, label sort(1) descending) blabel(total, pos(base)) bar(1, bfcolor(none))
            Click image for larger version

Name:	musau.png
Views:	1
Size:	18.9 KB
ID:	1430177

            Comment


            • #7
              Thanks Nick, was about to suggest the same. I think -tabplot- from SSC written by Nick as well is another option. You can search the forum fhis.

              Comment


              • #8
                Dear Andrew and Nick -

                Thank you very much for your replies and suggestions. I especially like the horizontal bar chart. Really appreciate you both taking the time to offer your advice - the command worked perfectly.

                Many thanks,
                Morgan

                Comment

                Working...
                X