Announcement

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

  • Bar Chart by ID

    Dears,
    I am trying to figure a bar chart by id over the period 1990-2021.
    I use the following command
    bys id: egen mean_inequal = mean( gini)
    bys id: replace mean_inequal = . if _n>1
    tw (bar mean_inequal id), title(Average Inequality by country from 1990-2021) xtitle("countries")

    However, I couldn't get all the countries in the bar. Plus, how can I change the color and size of the bar?
    Attached figure is from a published paper. I want to generate a similar figure
    Thanks
    Attached Files

  • #2
    There are about 50 countries in what you show and that graph is struggling. I would strongly suggest for those data and for yours too:

    1. Horizontal layout to avoid the country names -- surely key information and not ornamental or incidental -- being so crowded and placed at an awkward angle (giraffe graphics).

    2. A dot chart not a bar chart.

    3. Experiment with the axis ratio.

    Comment


    • #3
      Dear Nix, here is my data.
      i run the following codes. But couldn't get a similar graph.

      encode country, gen (id)
      sort id year
      xtset id year
      bys id: egen mean_inequal = mean( ginii )
      bys id: replace mean_inequal = . if _n>1
      tw (bar mean_inequal id), title(Average Inequality by country from 1990-2021) xtitle("countries")
      Attached Files

      Comment


      • #4
        You didn't address my points in #2. But at least one problem is evident from your code. Your id variable would run 1 upwards and correspond to country names in alphabetical order, which is no use if you want, as is clearly a good idea, sorting on the variable to be shown.

        Please see https://www.statalist.org/forums/help#stata for our longstanding request not to post .dta files but to use dataex.

        As you want not the 1320 observations in your dataset, but 40 country means, dataex is easy after a collapse.

        Here is what I got. The crowding problem is serious but just about soluble. Putting the y axis at the top is just a style choice, on which see https://journals.sagepub.com/doi/pdf...867X1201200314

        You could show numbers too with an extra option something like blabel(bar, format(%2.0f))

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str32 country float ginii
        "Angola"                           55.65242
        "Benin"                            49.67545
        "Botswana"                         65.60667
        "Burkina Faso"                     54.01212
        "Burundi"                          47.17485
        "Cabo Verde"                       55.52454
        "Cameroon"                         52.28212
        "Central African Republic"         64.97303
        "Chad"                              47.8503
        "Comoros"                          60.12818
        "Côte d'Ivoire"                   50.07485
        "Democratic Republic of the Congo" 49.11454
        "Equatorial Guinea"                 51.3803
        "Eswatini"                         61.55515
        "Ethiopia"                         47.01364
        "Gabon"                             48.1803
        "Ghana"                            48.03455
        "Guinea"                           46.37909
        "Guinea-Bissau"                    50.77394
        "Kenya"                             53.4897
        "Lesotho"                          55.19182
        "Madagascar"                       51.43636
        "Malawi"                            52.6497
        "Mali"                              48.0503
        "Mauritius"                        46.81788
        "Mozambique"                       61.14879
        "Namibia"                          68.78728
        "Niger"                            49.07243
        "Nigeria"                          44.50515
        "Republic of Congo"                55.14545
        "Rwanda"                           56.46879
        "Senegal"                          50.15758
        "Seychelles"                       52.33394
        "Sierra Leone"                     49.18606
        "South Africa"                     57.96545
        "Tanzania"                          47.7303
        "The Gambia"                       52.00515
        "Togo"                             49.75151
        "Uganda"                           52.45485
        "Zambia"                           59.22333
        end
        
        graph dot (asis) ginii, over(country, label(labsize(vsmall)) sort(1) descending) exclude0 linetype(line) ysc(r(43 .) alt) lines(lw(vthin) lc(gs12)) ysize(8) ytitle(Mean Gini inequality 1990-2022)
        Click image for larger version

Name:	gini.png
Views:	1
Size:	39.6 KB
ID:	1734447

        Comment


        • #5
          Dear Nick Cox, Thank you very much. It works

          Comment

          Working...
          X