Announcement

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

  • How can I store the percentage values from tabulate to use them in a bar chart?

    Dear Statalist members:

    I work with two variables: age_c5 which clusters respondents into five age groups; and aoss which is the dependent variable and takes values from -4 to 4. What I want to do is to show the distribution of the age groups (age_c5) across the values of the dependent variable (aoss) in a bar chart.

    When I use histogram with the by() option, I get five histograms, one for each age group, in one graph. However, what I actually want is to compare the distribution of the age groups across the values directly, similar to this graph:

    Click image for larger version

Name:	bar_age_groups.png
Views:	1
Size:	42.3 KB
ID:	1402897


    When I use the following code, I get the distribution of the age groups across the dependent variables including percentages.

    Code:
    tab age_c5 aoss, row
    Click image for larger version

Name:	percentages.png
Views:	1
Size:	29.8 KB
ID:	1402898


    Now, the question is: how do I get the row percentage values of each age group into a variable to present the distribution of all age groups across the dependent variable in a bar chart?
    I have already read the instruction of Nicholas J. Cox and Scott Merryman on creating percent summary variables:

    http://www.stata.com/support/faqs/da...ary-variables/

    But as my dependent variable is not an indicator variable, I think this doesn’t solve my problem.

    Thank you for any help!

  • #2
    You don't need to obtain or to store them at all. Several commands will calculate a percent breakdown and show it directly. See e.g.

    Code:
    help graph bar
    
    ssc desc catplot 
    
    ssc desc tabplot
    https://www.statalist.org/forums/for...updated-on-ssc is a fairly recent overview of tabplot. Search the forum for other mentions.

    I would give you an example using your data, but contrary to our advice in

    https://www.statalist.org/forums/help#stata

    you've chosen to show output by an image that doesn't allow easy copy and paste. Please do read and act on that.

    Comment


    • #3
      Thank you for your quick response.

      I should have read the FAQ more carefully! I'm sorry for that!

      Here is an example of my data:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(age_c5 aoss)
      5  0
      4 -1
      4 -1
      1  0
      2 -2
      3  0
      5  0
      4 -1
      5  0
      4 -1
      4 -1
      2  0
      2  0
      1  1
      4  0
      5  .
      3 -1
      5  0
      1 -1
      1  1
      end
      label values age_c5 c5
      label def c5 1 "<35", modify
      label def c5 2 "35-44", modify
      label def c5 3 "45-54", modify
      label def c5 4 "55-64", modify
      label def c5 5 "65+", modify
      label values aoss aoss
      label var age_c5 "Age (5 Categories)"
      label var aoss "Attitudes towards age-oriented social spending"
      Thanky you for your effort!

      Comment


      • #4
        Thanks for your data example. Possible commands (you must install as in #2 although you can also install tabplot from the Stata Journal site) are

        Code:
        tabplot aoss age_c5, percent(age_c5) showval 
        
        catplot aoss age_c5, percent(age_c5) recast(bar) blabel(total, format(%2.0f))

        Comment


        • #5
          Thank you very much for your help, Nick!

          With catplot, I finally got what I wanted.

          In case that anyone will ever be interested in how it worked and what the result is, I post my code and the respective graph.

          Code:
          catplot age_c5 aoss , percent(age_c5) recast(bar) var1opts(gap(0)) asyvars ///
          legend(label(1 "<35") label(2 "35-44") label(3 "45-54") label(4 "55-64") ///
          label(5 "65+")) legend(title("Age Groups", size(medsmall))) ytitle(" ") ///
          title("Distribution of the Dependent Variable", size(medium)) ///
          b1title("Preferences for Age-Related Social Policies", size(medsmall))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.1 KB
ID:	1402979

          Best

          Comment


          • #6
            Thank you so much. It was very helpful for me.

            Adding a tip: You can add the percentages for each bar using '`legg' blabel(bar, format(%3.1f) size(small) pos(inside) color(gs12))"

            How can I suppress y-axis legend? (= casa, creche pública, creche privada")



            Click image for larger version

Name:	Graph.png
Views:	1
Size:	39.2 KB
ID:	1405406
            thanks in advance,
            Tiago
            Last edited by Tiago Munhoz; 07 Aug 2017, 10:56.

            Comment


            • #7
              I have a similar issue. When drawing a bar graph of summary measures (in this case a median) of many vars over a couple of groups graph bar(median) {vars}, over{groups}
              I encounter several problems, one, is the inability to change the summary variable names (in this case it's "p50 of var" to var.)
              The alternative is to store the median values of vars for each group as variables name them appropriately and redo the graph, or to collapse. But perhaps there's a simpler workaround for just a graph?
              Thanks

              Comment

              Working...
              X