Announcement

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

  • Drawing a bar graph of the Age Dependency Ratio by Country

    Hello Stata users;

    I'm using the 13.1 version of Stata, and working with this data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str11 Country float value
    "Austria"     30.2
    "Belgium"     31.3
    "Germany"     35.2
    "Estonia"     32.2
    "Greece"      36.7
    "Spain"       30.8
    "Finland"     37.8
    "France"      34.8
    "Croatia"     36.6
    "Ireland"     23.6
    "Italy"       38.4
    "Lithuania"   31.2
    "Luxembourg"  21.7
    "Latvia"      33.9
    "Netherlands" 31.8
    "Portugal"    38.2
    "Slovenia"    34.3
    "Slovakia"    27.9
    end
    The idea is to draw a bar graph showing the age dependency ratio by country. The problem is that the command I'm using seems to be right: "graph bar (value), over (Country)" but that there's something with the variable "value" that's stopping me from getting the result.

    Any help please? With thanks!

  • #2
    Thanks for the data example, but please match that precision with clarity about code:

    show us the exact code you tried and tell us exactly hat happened: an error message, presumably.

    FAQ Advice, especially #12, points the way, as always.

    I don't think (value) was ever allowed as a syntax element like that. (asis) yes, but here, as there is just one observation for each country, the default of showing a mean works just as well.

    A bar chart is a poor idea for these data -- a lot of ink showing that many ratios are not zero, which is not of use or interest.

    I'd do something like this:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str11 Country float value
    "Austria"     30.2
    "Belgium"     31.3
    "Germany"     35.2
    "Estonia"     32.2
    "Greece"      36.7
    "Spain"       30.8
    "Finland"     37.8
    "France"      34.8
    "Croatia"     36.6
    "Ireland"     23.6
    "Italy"       38.4
    "Lithuania"   31.2
    "Luxembourg"  21.7
    "Latvia"      33.9
    "Netherlands" 31.8
    "Portugal"    38.2
    "Slovenia"    34.3
    "Slovakia"    27.9
    end
    
    graph dot (asis) value, over(Country, sort(1)) exclude0 ysc(alt) title(Age dependency ratio)
    Click image for larger version

Name:	age_dep.png
Views:	1
Size:	74.2 KB
ID:	1779777

    Comment


    • #3
      Nick Cox The error message I guess is saying "(value) invalid statistic", I do believe it is a problem of syntax, even though when I apply the "mean" command on that variable, everything looks ok. Well, I do believe that a bar graph is always the classic representation to show levels of the same variable for different categories, the graph dot you've suggested is good too, but, it's a bit too technical for this kind of simple representation (again, this is a personnal view of mine, I'm not judging), yet I do like the idea of sorting the countries by that variable's value.

      Comment


      • #4
        Code:
        graph bar (asis) value, over(Country, sort(1) descending)
        Last edited by Chen Samulsion; 13 Jul 2025, 02:32.

        Comment


        • #5
          Aziz: I wasn't asking you to make guesses. I was asking you to show us what you tried. If the recipe for the command you tried started with

          Code:
          graph bar (value) 
          then that is not one of the allowed forms.


          I do imagine that bar charts may be more popular than dot charts in whatever literature you're following, but herd thinking does not here beat thinking for yourself. If your readers know about scatter plots, they should understand dot charts. At worst, you can provide a quick explanation.

          See https://www.jstor.org/stable/2683401 for the canonical paper.

          I am very happy to make judgments here. The principle is to me that if a graph design is poor or good, you should be able to say why you do or don't like.it. Then there can be a discussion. If one person says "I do like that" and another person says "I don't" they have to dig deeper to get anywhere.

          Now @Chen Samulsion's code suggestion is illegal but he probably meant something like the graph bar code below. But the result is an appalling mess of country names, and while there are work-arounds (country text vertical or sloping) the best work-around if you must insist on a bar chart is to go horizontal.

          The dot chart lets you focus directly on the key question, which is presumably comparing values with each other in this case. Comparison with zero is neither needed nor helpful, and a bar chart is a mass of ink expended uselessly.

          I took it as uncontroversial that there is no advantage here to alphabetical order. If you want to find a particular cointry, the effort of scanning the names quickly is trivial compared with the gains of sorting by value.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str11 Country float value
          "Austria"     30.2
          "Belgium"     31.3
          "Germany"     35.2
          "Estonia"     32.2
          "Greece"      36.7
          "Spain"       30.8
          "Finland"     37.8
          "France"      34.8
          "Croatia"     36.6
          "Ireland"     23.6
          "Italy"       38.4
          "Lithuania"   31.2
          "Luxembourg"  21.7
          "Latvia"      33.9
          "Netherlands" 31.8
          "Portugal"    38.2
          "Slovenia"    34.3
          "Slovakia"    27.9
          end
          
          graph bar (asis) value, over(Country, sort(1)) title(Age dependency ratio) name(bar, replace)
          
          graph hbar (asis) value, over(Country, sort(1)) title(Age dependency ratio) name(hbar, replace)
          
          graph dot (asis) value, over(Country, sort(1)) exclude0 ysc(alt) title(Age dependency ratio) name(dot, replace)
          Last edited by Nick Cox; 13 Jul 2025, 03:07.

          Comment


          • #6
            Here the graphs. Horizontal comes first, but no reason for that.

            Click image for larger version

Name:	hbar.png
Views:	1
Size:	46.5 KB
ID:	1779793
            Click image for larger version

Name:	bar.png
Views:	1
Size:	52.4 KB
ID:	1779792

            Comment


            • #7
              To reconcile #4 and #5: IIRC Chen Samulsion’s proposal was illegal as he first posted it and when I started writing #5. He realised his error and fixed it. I didn’t notice the fix. Sorry about that.

              Comment


              • #8
                Thank you Nick Cox, you are absolutely right as I forgot to specify (1) after sort in my original answer. I hate that specification because I think only a sort is sufficient. And after so many times writing sort(1) in practice, I had an illusion that I need not keep it this time. But I checked in help file and had to edit my answer soon.
                Last edited by Chen Samulsion; 14 Jul 2025, 07:05.

                Comment


                • #9
                  Chen Samulsion Thanks for your nice reply.

                  If I am Stata and you tell me "sort the data before you ..." I really need to know how!

                  There is a default in that the result of

                  Code:
                  graph bar (asis) value, over(Country)
                  is sorted by Country, so sort(value) and sort(1) are two ways to get something different.
                  Last edited by Nick Cox; 14 Jul 2025, 07:09.

                  Comment


                  • #10
                    So Stata provides (or as you liked to put, is provided) three ways of sort:
                    sort(varname) put bars in prespecified order
                    sort(#) put bars in height order
                    sort((stat) varname) put bars in derived order
                    If there's only one yvar, set sort as default of sort[(1)] would be friendly for lazy people like me. But lazy people often ignore other ways of sort or the mechanism behind it.

                    Comment


                    • #11
                      Indeed My own position is that alphabetic or alphanumeric order by string variable would be a dopey default for almost any display. even though it is often directly implied by user choice, but "you shouldn't want to do that" is generally not a tactful way to treat users. (Other way round, I have written commands that yield daily dates as doubles, even if the user asks for something else!).

                      Howard Wainer has often mocked this choice in print, as say Alabama first! or Austria first! (The examples are a little arbitrary, but not the principle.)

                      Comment

                      Working...
                      X