Announcement

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

  • Drawing a Bar Graph based on a variable's median

    Hello Stata community, I hope everyone is doing well.

    I hope I could get some help for my question, here's the situation:
    I have this data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str30 Country float(EconomicFreedom Gini2017)
    "Albania"            7.65 33.1
    "Argentina"          4.72 41.1
    "Armenia"            7.84 33.6
    "Austria"            7.91 29.7
    "Belgium"            7.85 27.4
    "Bhutan"             6.75 37.4
    "Bolivia"            6.25 44.6
    "Brazil"             6.49 53.3
    "Bulgaria"           7.62 40.4
    "Canada"             8.29 33.3
    "Chile"              7.91 44.4
    "China"              6.21 39.1
    "Colombia"           6.69 49.7
    "Costa Rica"         7.64 48.3
    "Croatia"            7.23 30.4
    "Cyprus"             7.43 31.4
    "Czech Republic"     7.93 24.9
    "Denmark"            8.11 28.7
    "Dominican Republic" 7.53 42.2
    "Ecuador"            6.02 44.7
    "Egypt, Arab Rep."   5.52 31.5
    "El Salvador"        7.49   38
    "Estonia"            8.05 30.4
    "Finland"            7.92 27.4
    "France"             7.72 31.6
    "Gabon"              5.44   38
    "Georgia"            8.31 37.9
    "Germany"            7.96 31.2
    "Greece"             7.17 34.4
    "Honduras"           7.26 49.4
    "Hungary"            7.51 30.6
    "Iceland"            7.33 26.1
    "Indonesia"          7.07 38.1
    "Iran, Islamic Rep."  5.1 40.8
    "Ireland"            8.24 31.4
    "Israel"             7.58 38.2
    "Italy"              7.67 35.9
    "Kazakhstan"         7.14 27.5
    "Kyrgyz Republic"    7.14 27.3
    "Latvia"             8.12 35.6
    "Lesotho"            6.55 44.9
    "Lithuania"          8.08 37.3
    "Luxembourg"         7.85 34.5
    "Malta"              7.97 29.2
    "Mauritius"           7.9 36.8
    "Moldova"            7.01 25.9
    "Montenegro"         7.39 36.9
    "Myanmar"            5.75 30.7
    "Netherlands"        7.94 28.5
    "North Macedonia"    7.52 34.2
    "Norway"             7.82   27
    "Panama"              7.7 49.9
    "Paraguay"           7.01 48.5
    "Peru"               7.76 43.3
    "Poland"             7.54 29.7
    "Portugal"           7.74 33.8
    "Romania"            7.91   36
    "Russian Federation" 6.59 37.2
    "Serbia"             6.78 36.2
    "Slovak Republic"     7.7 23.2
    "Slovenia"           7.31 24.2
    "Spain"              7.75 34.7
    "Sweden"             7.93 28.8
    "Switzerland"        8.49 32.7
    "Thailand"           6.56 36.5
    "Ukraine"            5.96   26
    "United Kingdom"     8.16 35.1
    "United States"      8.18 41.2
    "Uruguay"            7.26 39.5
    "Zimbabwe"            5.9 44.3
    end
    It's basically 70 countries with their Economic Freedom Score and their Gini Index.

    My goal is to draw a bar grapgh that shows all the 70 countries, the X axis should be The Economic Freedom Index and the Y axis should be the Gini Index. By "based on a variable's median" in the title of my post, I mean that, on the X axis, I wanna have my 70 countries devised into 2 groups according to the median of the variable "Economic Freedom", so, on the right side of the graph, I wanna have the countries who are more economically free (compared to the median country), and on the left side of the graph, I wanna have the less economically free countries (compared to the median country, again). And on the Y axis, I wanna have the Gini index, all in a single bar chart.

    I do wish I was very clear with myexplanation, and I do also wish I could get some help with this.

    Thanks very much in advance.

  • #2
    I couldn't follow all of what you want here -- and what I can follow doesn't make full sense. A bar chart with measured x and y axes?

    With a conventional bar chart for these data country names would go on the x axis and -- for 70 countries -- be illegible. Also, bars would start at zero, but it's the comparisons of country values that are of interest, not comparisons with an unattained zero.

    A scatter plot would make a lot of sense -- and would show essentially no relationship -- but you'd struggle to show country names

    I used multidot from SSC to get this. For an overview, see https://www.statalist.org/forums/for...ailable-on-ssc

    Code:
    set scheme s1color 
    scatter Gini Econ, ms(Oh)
    label var Econ "Economic freedom"
    label var Gini "Gini 2017"
    
    multidot Econ Gini, over(Country) ysize(8) ytitle("") yla(, labsize(vsmall)) descending yli(35.5)

    Click image for larger version

Name:	freegini.png
Views:	1
Size:	48.9 KB
ID:	1673164

    Comment


    • #3
      Hello M. Cox and thanks for the help;

      Well, I don't actually need to show the countries's names, I know that, fir 70 countries, it will be very illegible. My idea (and perhaps I was a bit unclear explaining it) is to have a bar chart which shows "freedom index gini coefficients", and it makes kinda sense to use a bar chart since it's the Gini index (tho I don't know if it's statistically and graphically faisable). My goal is to have é bar charts in one bar chart, a bar chart that shows (for each group of countries: less free countries vs. more free countries according to the median of the variable "freedom index"), a bar chart which shows the Gini coefficient for each country, so, a bar would reveal the value of the Gini index for that country, and its place (either to the left or to the right of the grapgh) would show if that country is more free (or less free) than the median country.

      And I guess a bar chart would be better for comparison reasons, inter-group comparison and even intra-group comparison.

      I hope I'm being a bit more clear with this explanation, M. Cox. As I said, it's optional for me to show the countreis names on the graph.

      Thanks for your help.

      Comment


      • #4
        If you don’t need country names, a scatter plot is simple, direct and easy. I have already given you sample code.

        Sorry, but I have no idea precisely what else you have in mind but it would seem to imply degrading the full detail of the scatter plot, which as said shows little relationship between the variables.

        Comment


        • #5
          Dear Nick Cox ,

          This graph in #2 is striking. I am trying to do a similar thing for some of my working papers. I am looking at country-level variation for EU Total Aid and EU Budget Support. I created a graph by using the same code you provided in #2. However, I want to limit the sample for only 30 top countries. How can I limit the number of countries (ascending) by adding this request in a code.

          Thank you so much,

          Best,


          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str60 recipient double(euodatotal eubudgetsupport)
          "Afghanistan"                            6145.529958724976  577.6635794639587
          "Albania"                               1951.9199829101563 190.57368125766516
          "Algeria"                                1376.779990196228  43.81100353598595
          "American Samoa"                                         0                  0
          "Andorra"                                                0                  0
          "Angola"                                1109.5800018310547                  0
          "Anguilla"                              26.410000594332814  9.125247955322266
          "Antigua and Barbuda"                   41.840000219643116 11.639410972595215
          "Argentina"                              500.4199924468994                  0
          "Armenia"                               1172.6799898147583  296.5510320663452
          "Aruba"                                                  0                  0
          "Australia"                                              0                  0
          "Austria"                                                0                  0
          "Azerbaijan"                             468.3299970626831  52.68592917919159
          "Bahamas"                                                0                  0
          "Bahrain"                                                0                  0
          "Bangladesh"                             2650.660011291504 258.86162523715757
          "Barbados"                                76.1000018119812  .2826339900493622
          "Belarus"                                558.0699977874756 .39935699105262756
          "Belgium"                                                0                  0
          "Belize"                                186.77999807335436                  0
          "Benin"                                 1575.0200023651123  575.6367390155792
          "Bermuda"                                                0                  0
          "Bhutan"                                125.23999899625778  65.67895352840424
          "Bolivia"                               1170.2499923706055  362.6177759170532
          "Bosnia and Herzegovina"                 3846.380012512207  7.377908945083618
          "Botswana"                              289.97000005841255 127.12795764952898
          "Brazil"                                1272.3499898910522                  0
          "British Virgin Islands"                                 0                  0
          "Brunei Darussalam"                                      0                  0
          "Bulgaria"                                               0                  0
          "Burkina Faso"                            3146.77001953125  1334.515209197998
          "Burundi"                               1595.1599922180176  203.8042169213295
          "Cabo Verde"                             443.5800018310547  172.6507288813591
          "Cambodia"                              1000.8700017929077  272.2926844358444
          "Cameroon"                               1522.590000152588 135.18121703341603
          "Canada"                                                 0                  0
          "Cayman Islands"                                         0                  0
          "Central African Republic"              1396.4000000953674 274.11818131804466
          "Chad"                                  2265.5100059509277 184.60145461559296
          "Chile"                                  304.5699979066849 1.5726079791784287
          "China (People's Republic of)"          1768.8200073242188                  0
          "Chinese Taipei"                                         0                  0
          "Christmas Island"                                       0                  0
          "Cocos (Keeling) Islands"                                0                  0
          "Colombia"                               1167.079999923706 154.82479498535395
          "Comoros"                               167.94999814033508  9.462395668029785
          "Congo"                                 467.91999888420105  41.80800757929683
          "Cook Islands"                          15.760000053793192 2.8017539978027344
          "Costa Rica"                            169.47999894618988  14.53114901483059
          "Croatia"                                811.5000019073486                  0
          "Cuba"                                  234.20000076293945                  0
          "Cyprus"                                                 0                  0
          "Czech Republic"                                         0                  0
          "Côte d'Ivoire"                        1728.0800075531006 348.52836418151855
          "Democratic People's Republic of Korea" 445.36000061035156                  0
          "Democratic Republic of the Congo"       4367.640068054199 131.47036981582642
          "Denmark"                                                0                  0
          "Djibouti"                              304.85999727249146 .09404199966229498
          "Dominica"                              210.77000224590302  66.39899078011513
          "Dominican Republic"                     939.2999935150146 322.05682587623596
          "Ecuador"                               1030.9800109863281 140.13698724657297
          "Egypt"                                  5160.570069313049 165.65816274285316
          "El Salvador"                            602.3500003814697 189.07860660552979
          "Equatorial Guinea"                     40.909999653697014                  0
          "Eritrea"                                413.5399966239929 .12102899700403214
          "Estonia"                                                0                  0
          "Eswatini"                               382.7299988269806                  0
          "Ethiopia"                               4587.850036621094  620.7552557978779
          "Falkland Islands"                                       0                  0
          "Faroe Islands"                                          0                  0
          "Fiji"                                  233.58000302314758 23.210625648498535
          "Finland"                                                0                  0
          "France"                                                 0                  0
          "French Guiana"                                          0                  0
          "French Polynesia"                                       0                  0
          "Gabon"                                 253.97000056505203   .381633996963501
          "Gambia"                                 420.2899981737137 129.36805188655853
          "Georgia"                               2939.7700061798096  861.9395599365234
          "Germany"                                                0                  0
          "Ghana"                                 1646.9000129699707  578.3030885457993
          "Gibraltar"                                              0                  0
          "Greece"                                                 0                  0
          "Greenland"                                              0                  0
          "Grenada"                                73.38999998569489  38.91586220264435
          "Guadeloupe"                                             0                  0
          "Guam"                                                   0                  0
          "Guatemala"                              677.7000007629395  57.22989289090037
          "Guinea"                                1090.0000095367432 107.68642807006836
          "Guinea-Bissau"                          585.1399993896484   76.1202453225851
          "Guyana"                                 436.1499992609024  183.6045881062746
          "Haiti"                                  2061.629991531372 410.13267493247986
          "Honduras"                              1012.3500003814697 221.06021189689636
          "Hong Kong (China)"                                      0                  0
          "Hungary"                                                0                  0
          "Iceland"                                                0                  0
          "India"                                  3684.250015258789  298.2221433222294
          "Indonesia"                             1393.3399953842163  375.7752431407571
          "Iran"                                   186.9699991941452                  0
          "Iraq"                                   2385.109980583191                  0
          end
          ------------------ copy up to and including the previous line ------------------

          Listed 100 out of 229 observations
          Use the count() option to list more


          [CODE]

          set scheme s1color
          scatter euodatotal eubudgetsupport , ms(Oh)
          help multidot
          multidot euodatotal eubudgetsupport , over(recipient) ysize(8) ytitle("") yla(, labsize(vsmall)) yli(35.5)

          {/CODE]

          Comment


          • #6
            Glad somebody likes it. This sounds like a variation on


            Code:
            egen rank = rank(-euodatotal), unique 
            
            multidot euodatotal eubudgetsupport if rank <= 30, over(recipient) ytitle("")

            Comment


            • #7
              Dear Nick Cox ,

              Thank you so much,
              It helped a lot.

              Best,

              Comment


              • #8
                Nick Cox Thanks very much for the help, perhapds I'll try your solution. Thanks again

                Comment

                Working...
                X