Announcement

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

  • Histogram inquiry for presenting multiple countries from panel data

    Histogram inquiry

    I am handicapped here as a novice Stata user. I am attempting to create, as part of a graduate writing task, a histogram figure to project 14 West African countries’ respective percentages of public service governing malfeasance using panel data. My setup is as follows:
    1. Each country has 8 ordinal trust level variables I created on the dataset to generate a combined total.
    2. The combined total for each of the 14 countries is what I would like to display in the histogram figure.
    3. Percentages should be displayed on the y-axis.
    4. The countries should be in alphabetical order on the x-axis.
    I do not know how to code to include the 14 countries in a single figure even after searching various sources for examples. Sample codes for what I am trying to accomplish follow for just one country, Niger. Its malfeasance rate is 33 percent which is the value found in variable niger_perc_report. The sample attached histogram is not even close to what I would like to project.

    gen niger_total = nigert1 + nigert2 + nigert3 + nigert4 + nigert5 + nigert6 + nigert7 + nigert8
    gen niger_average = niger_total / 8
    gen niger_percentage = niger_average / 1199
    gen niger_perc_report = niger_percentage * 100
    format niger_perc_report %2.0f

    The above coding is similarly done for the other 13 countries.

    The attached figure was created with the command:
    histogram niger_perc_report, discrete addlabels xtitle("") ylabel(,grid) percent start(9) by(country_code, row(1)) xlabel(, ang(45))

    Click image for larger version

Name:	Histogram Fig 1.png
Views:	1
Size:	42.7 KB
ID:	1710136



    Again, all this information is resident in a panel dataset. Does anyone have suggestions for my situation? Thanks in advance.

    Yarh

  • #2
    Once you calculate the percentages, you can keep one observation per country and then use graph to plot. Some notes:

    1. No need to squeeze the country names with vertical bars. Switch to horizontal bars.
    2. There is no compelling reason to sort country names alphabetically. Either sort in an ascending or descending order as the goal is to compare values across countries.
    3. You need the percentages in one variable (long layout).

    No data example, so the following is pseudo-code:

    Code:
    *WITH PERCENTAGES AND A LONG LAYOUT
    preserve
    bys country: keep if _n==1
    set scheme s1mono
    graph hbar percent, over(country, sort(1))  ytitle("Percent")
    restore

    Comment


    • #3
      Hello, Andrew.

      I much appreciate your suggestion. It worked. Please see my codes and attached result.

      I only need to figure out now how to widen the bar lines with a consistent color presentation for each country. I thought that would be the easiest part, but I don’t seem to find a workable solution.

      Again, thanks.

      Yarh

      preserve
      bys country_code: keep if _n==1
      set scheme s1color
      graph hbar bt_perc_report bft_perc_report cvt_perc_report gamb_perc_report ghan_perc_report gui_perc_report ivc_perc_report lib_perc_report mali_perc_report niger_perc_report nigria_perc_report sengl_perc_report srln_perc_report togo_perc_report, over(country_code, sort(1)) title("Figure ##: Percentage of population who ...", size(medsmall)) ytitle("Percent", size(medsmall)) note("Source: Author's calculations ...", size(medsmall)) legend(off) blabel(bar, color(black) format(%2.1f)) asyvars
      restore

      Click image for larger version

Name:	Hbar Fig.png
Views:	1
Size:	68.9 KB
ID:	1710694

      Comment


      • #4
        Without data there isn't much we can do. Can you use dataex to give us an example of the data?
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          I tend to agree with both Andrew Musau and Maarten Buis.

          Your data structure seems unusual, perhaps reflecting a great deal of experience in MS Excel.

          In terms of why you get the result in #3 I suggest adding the option nofill.

          Otherwise you can do better, even if it means typing in the data all over again. Above all, alphabetical order here as almost everywhere is a poor design. If your teachers are insisting on that, ask them why? The result in #3 is clear in principle, but poor in practice. Answer quickly: which are the 3 countries highest on this measure? the 3 countries lowest? the 7th and 8th, so the comedians (values averaged to get the median)? Those aren't easy questions with alphabetical order. (Also, some might prefer Côte d'Ivoire.)

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str12 country float whatever
          "Benin"        20.9
          "Burkina Faso" 26.2
          "Cabo Verde"   14.2
          "Gambia"       24.8
          "Ghana"        18.6
          "Guinea"       21.4
          "Ivory Coast"  19.3
          "Liberia"      12.4
          "Mali"         19.1
          "Niger"        33.1
          "Nigeria"       9.2
          "Senegal"      21.1
          "Sierra Leone" 23.9
          "Togo"         18.8
          end
          
          graph hbar (asis) whatever, ysc(alt) over(country, sort(1) descending) blabel(bar) name(G1, replace)  subtitle(You fill this in)
          
          
          graph dot (asis) whatever, ysc(alt r(8 .)) over(country, sort(1) descending) blabel(bar) exclude0 name(G2, replace) linetype(line) lines(lc(gs12) lw(vthin)) subtitle(You fill this in)
          The dot chart hinges on this thought. The goal of the graph is surely to compare countries with each other, not with zero as such. It doesn't appear to be documented but blabel() works with graph dot too.

          Click image for larger version

Name:	wafrica_G1.png
Views:	1
Size:	25.0 KB
ID:	1710706


          Click image for larger version

Name:	wafrica_G2.png
Views:	1
Size:	23.2 KB
ID:	1710707


          Extra details:

          With graph dot the default of a dotted line is something that doesn't export well in my experience e.g. to MS Office applications. So, I go for a continuous line.

          It's a matter of taste, but when a graph has a table flavour, as is surely true here, the horizontal axis to me looks good at the top.

          More discussion at https://www.stata-journal.com/articl...article=gr0053

          On tables as graphs more at https://journals.sagepub.com/doi/pdf...867X0800800208

          Comment


          • #6
            Thanks to you all.

            Nick’s code example especially helped me get through the finish line!! Much appreciated. Please see attachment.

            Yarh

            Click image for larger version

Name:	Hbar Fig RECREATED.png
Views:	1
Size:	67.4 KB
ID:	1710911

            Comment


            • #7
              Hello, Nick.

              I have restarted portion of a project you guided me through earlier this year. This time I am trying to create a bar graph of population trust percentage in governments covering 14 countries as follows:
              • Each country group is measured for years 6, 7, and 8.
              • I want each country to have all its years (6, 7, 8) represented together in the graph, with only the first line in the group showing the country’s name.
              • Each country graph line per year should be color-coded as Year6 Year7 Year8 to match the graph’s legend, thus eliminating “Y6”, “Y7”, or “Y8” from the country names on the individual graph lines.
              Please see the attached coding and output from which I am attempting to get clear and readable results I want but with no success. I do appreciate your help and suggestions. Thanks much.

              Yarh


              CODE

              clear all
              set scheme s1mono
              input str17 country float whatever
              "Benin Y6" 18.9
              "Benin Y7" 22.0
              "Benin Y8" 20.9
              "Burkina Faso Y6" 28.2
              "Burkina Faso Y7" 32.1
              "Burkina Faso Y8" 26.2
              "Cabo Verde Y6" 15.9
              "Cabo Verde Y7" 13.8
              "Cabo Verde Y8" 14.2
              "Côte d'Ivoire Y6" 23.2
              "Côte d'Ivoire Y7" 24.8
              "Côte d'Ivoire Y8" 19.3
              "Gambia Y6" 0.0
              "Gambia Y7" 0.0
              "Gambia Y8" 24.8
              "Ghana Y6" 18.7
              "Ghana Y7" 23.0
              "Ghana Y8" 18.6
              "Guinea Y6" 0.0
              "Guinea Y7" 20.1
              "Guinea Y8" 21.4
              "Liberia Y6" 9.7
              "Liberia Y7" 0.0
              "Liberia Y8" 12.4
              "Mali Y6" 27.4
              "Mali Y7" 23.5
              "Mali Y8" 19.1
              "Niger Y6" 49.9
              "Niger Y7" 36.6
              "Niger Y8" 33.1
              "Nigeria Y6" 7.5
              "Nigeria Y7" 11.8
              "Nigeria Y8" 9.2
              "Senegal Y6" 30.7
              "Senegal Y7" 29.4
              "Senegal Y8" 21.1
              "Sierra Leone Y6" 19.1
              "Sierra Leone Y7" 0.0
              "Sierra Leone Y8" 23.9
              "Togo Y6" 21.0
              "Togo Y7" 13.4
              "Togo Y8" 18.8
              end

              graph hbar (asis) whatever, ysc(alt) over(country, sort(1) descending) blabel(bar) name(G1, replace) title("Percentage of population with trust in their governments", size(small)) subtitle(" ", size(small)) ytitle("Percent", size(small)) note("Source: Author's calculations", size(small)) legend(off) bar(1, fcolor(brown))


              GRAPH
              Click image for larger version

Name:	Graph-.png
Views:	1
Size:	61.7 KB
ID:	1734345

              Comment


              • #8
                Red and green should not be combined in a graphic: https://venngage.com/blog/color-blind-friendly-palette/

                Otherwise here are some ideas. I have to think that bar charts are doomed here.

                Code:
                clear all
                set scheme s1mono
                input str17 country float whatever
                "Benin Y6" 18.9
                "Benin Y7" 22.0
                "Benin Y8" 20.9
                "Burkina Faso Y6" 28.2
                "Burkina Faso Y7" 32.1
                "Burkina Faso Y8" 26.2
                "Cabo Verde Y6" 15.9
                "Cabo Verde Y7" 13.8
                "Cabo Verde Y8" 14.2
                "Côte d'Ivoire Y6" 23.2
                "Côte d'Ivoire Y7" 24.8
                "Côte d'Ivoire Y8" 19.3
                "Gambia Y6" 0.0
                "Gambia Y7" 0.0
                "Gambia Y8" 24.8
                "Ghana Y6" 18.7
                "Ghana Y7" 23.0
                "Ghana Y8" 18.6
                "Guinea Y6" 0.0
                "Guinea Y7" 20.1
                "Guinea Y8" 21.4
                "Liberia Y6" 9.7
                "Liberia Y7" 0.0
                "Liberia Y8" 12.4
                "Mali Y6" 27.4
                "Mali Y7" 23.5
                "Mali Y8" 19.1
                "Niger Y6" 49.9
                "Niger Y7" 36.6
                "Niger Y8" 33.1
                "Nigeria Y6" 7.5
                "Nigeria Y7" 11.8
                "Nigeria Y8" 9.2
                "Senegal Y6" 30.7
                "Senegal Y7" 29.4
                "Senegal Y8" 21.1
                "Sierra Leone Y6" 19.1
                "Sierra Leone Y7" 0.0
                "Sierra Leone Y8" 23.9
                "Togo Y6" 21.0
                "Togo Y7" 13.4
                "Togo Y8" 18.8
                end
                
                gen when = real(substr(country, -1, 1))
                replace country = substr(country, 1, strlen(country) -3)
                reshape wide whatever, i(country) j(when)
                foreach y in 6 7 8    {
                    label var whatever`y' "Year `y'"
                }
                
                graph dot (asis) whatever?, over(country, sort(3) descending) ysc(alt r(-1 .)) ///
                marker(1, ms(Oh) mc(black)) marker(2, ms(+) mc(black) msize(medlarge)) marker(3, mc(black) ms(Th)) ///
                subtitle(Percent of population trusting their government) ///
                legend(row(1)) linetype(line) lines(lc(gs12) lw(vthin))
                Click image for larger version

Name:	westafrica.png
Views:	1
Size:	47.6 KB
ID:	1734351

                Comment


                • #9
                  Thanks much, Nick.

                  Your suggestion and advice provide a clearer pathway to what I want to achieve. Anyone looking at the data presentation will likely follow my graph interpretation and meaning.

                  Yarh

                  Comment

                  Working...
                  X