Announcement

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

  • Sorted bar graphs by year for multiple variables

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str32 country int year float(rank_NGDPRPC rank_NGDPDPC rank_NGSD_NGDP rank_PCPIPCH)
    "Greece"  2010 2 2 4 1
    "Estonia" 2010 4 4 1 2
    "Cyprus"  2010 1 1 3 3
    "Malta"   2010 3 3 2 4
    "Greece"  2011 2 2 4 3
    "Malta"   2011 3 3 2 4
    "Estonia" 2011 4 4 1 1
    "Cyprus"  2011 1 1 3 2
    "Malta"   2012 3 3 2 2
    "Greece"  2012 2 2 4 4
    "Cyprus"  2012 1 1 3 3
    "Estonia" 2012 4 4 1 1
    "Estonia" 2013 4 4 1 1
    "Greece"  2013 3 3 3 4
    "Malta"   2013 2 2 2 2
    "Cyprus"  2013 1 1 4 3
    "Cyprus"  2014 2 1 4 3
    "Estonia" 2014 4 4 1 2
    "Malta"   2014 1 2 2 1
    "Greece"  2014 3 3 3 4
    end
    Q1. I am trying to plot rank_* variables of countries by year. For example, the following code does the job (partially) for the variable rank_NGDPRPC .
    Code:
    graph hbar (asis) rank_NGDPRPC, over(country) by(year)
    . This plot, however, is not sorted. How could I attain a plot that always puts country with rank 1 on top then country with rank 2 and so on?

    Q2. How do I apply the above requested code to all rank_* variables using loop?

    Q3. Instead of showing all the plots in one graph, how do I generate individual plots for each year for all the rank_* variables?

  • #2
    It does not make sense to me to show rankings (categories) using a bar graph as you do. A table will do. In any case, #1

    Code:
    graph hbar (asis) rank_NGDPRPC, over(country, sort(1)) by(year)
    and #3

    Code:
    rename rank_* rank_#, addnumber(1)
    reshape long rank_, i(country year)
    label define _j 1 "NGDPRPC" 2 "NGDPDPC" 3 "NGSD" 4 "PCPIPCH"
    label values _j _j
    graph hbar (asis) rank_, over(country, sort(1)) over(_j) by(year)
    I do not get what you want for Q2.

    Comment


    • #3
      Andrew Musau Sorry I could not fully explain what I wanted. In Q2, I am requesting code which would generate graphs for all the rank_* variables similar to Q1. The code in Q1 plots only one variable -- rank_NGDPRPC. So in Q2, I am looking for 4 graphs since there are 4 rank_* variables.

      I am afraid I could not manage to explain Q3 very well. Here, I am looking for 20 different graphs i.e. considering one variable (there are four) and one year (5 different years) at a time

      Please let me know if you need more clarifications. Thanks!

      Comment


      • #4
        #2

        Code:
        local vars "NGDPRPC NGDPDPC NGSD PCPIPCH"
        foreach var in `vars'{
        graph hbar (asis) rank_`var', over(country, sort(1)) by(year) saving(gr_`var')
        }
        #3
        Code:
        local vars "NGDPRPC NGDPDPC NGSD PCPIPCH"
        foreach var in `vars'{
        forval year= 2010/2014{
        graph hbar (asis) rank_`var' if year==`year', over(country, sort(1)) saving(gr_`var'_`year')
        }
        }

        Comment


        • #5
          See also -multidot now available on SSC-

          Comment


          • #6
            Andrew Musau Thanks a lot! This is what I wanted!

            Nick Cox Thanks for the suggestion. Going to have a look.

            Comment


            • #7
              I am trying highlight a single country, say, Cyprus by using Green. How would the following code, given in #4, change?
              Code:
              local vars "NGDPRPC NGDPDPC NGSD PCPIPCH"
              foreach var in `vars'{
              forval year= 2010/2014{
              graph hbar (asis) rank_`var' if year==`year', over(country, sort(1)) saving(gr_`var'_`year')
              }
              }
              Note: I tried to follow the instructions here https://www.stata-journal.com/articl...article=gr0049 , but failed. I am still not comfortable with loops

              Comment


              • #8
                Look at adding the options

                Code:
                 bar(1, color(green)) asyvars

                Comment


                • #9
                  I didn't look at any of the bar charts suggested in #1 to #8 because nobody showed any of them! (me neither, but I was away from any computer) --- and as Andrew Musau pointed out in #2 there is no information in a bar chart of ranks that isn't in a table showing them in order. Some play got to this point:

                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input str32 country int year float(rank_NGDPRPC rank_NGDPDPC rank_NGSD_NGDP rank_PCPIPCH)
                  "Greece"  2010 2 2 4 1
                  "Estonia" 2010 4 4 1 2
                  "Cyprus"  2010 1 1 3 3
                  "Malta"   2010 3 3 2 4
                  "Greece"  2011 2 2 4 3
                  "Malta"   2011 3 3 2 4
                  "Estonia" 2011 4 4 1 1
                  "Cyprus"  2011 1 1 3 2
                  "Malta"   2012 3 3 2 2
                  "Greece"  2012 2 2 4 4
                  "Cyprus"  2012 1 1 3 3
                  "Estonia" 2012 4 4 1 1
                  "Estonia" 2013 4 4 1 1
                  "Greece"  2013 3 3 3 4
                  "Malta"   2013 2 2 2 2
                  "Cyprus"  2013 1 1 4 3
                  "Cyprus"  2014 2 1 4 3
                  "Estonia" 2014 4 4 1 2
                  "Malta"   2014 1 2 2 1
                  "Greece"  2014 3 3 3 4
                  end
                  
                  reshape long rank_, i(country year) j(measure) string
                  
                  separate rank_, by(country) veryshortlabel
                  
                  scatter rank_? year, by(measure, compact note("") legend(off)) ///
                  ms(none ..) mlabpos(0 ..) mla(country country country country) ///
                  xsc(r(2009.5 2014.5)) yla(, ang(h)) xla(2010/2014) subtitle(, fcolor(eltgreen*0.25)) xtitle("")
                  Click image for larger version

Name:	budu.png
Views:	1
Size:	28.7 KB
ID:	1491140


                  Further comments:

                  1. Use civilised variable labels to explain what these measures are.

                  2. Some measures should be inverted? Ideally "good" will be consistently at the top -- or the bottom.

                  3. Scope to mess around with colours and font sizes.

                  Comment


                  • #10
                    Andrew Musau : I tried adding the code. Unfortunately, it changes color of every bar. Here is what I did :
                    Code:
                    local vars "NGDPRPC NGDPDPC NGSD PCPIPCH"
                    foreach var in `vars'{
                    forval year= 2010/2014{
                    graph hbar (asis) rank_`var' if year==`year', over(country, sort(1)) bar(1, color(green)) asyvars saving(gr_`var'_`year')
                    }
                    }
                    Again, I am trying to highlight only a single country, for example Cyprus, whose ranking may change over the years.

                    Comment


                    • #11
                      Nick Cox : Thank you for the "Further comments". I will keep them in mind. Here is my data followed by label and rank_* codes.
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input str32 country int year double(NGDPRPC NGDPDPC NGSD_NGDP PCPIPCH)
                      "Cyprus"  2010 20490.637 31261.236 12.558  2.558
                      "Cyprus"  2011  20051.42 32692.698 14.706  3.481
                      "Cyprus"  2012 18936.417 29066.268  10.16  3.089
                      "Cyprus"  2013 17732.983  27825.87  8.246   .381
                      "Cyprus"  2014 17644.755 27267.428  8.022  -.268
                      "Estonia" 2010  11052.81 14672.328 23.075  2.741
                      "Estonia" 2011 11928.681 17470.842 26.442   5.08
                      "Estonia" 2012 12487.099  17431.54 27.143  4.219
                      "Estonia" 2013  12774.29 19078.213 27.426  3.247
                      "Estonia" 2014 13170.071 19969.128 26.937   .476
                      "Greece"  2010 20327.829 26972.873  5.664  4.704
                      "Greece"  2011 18464.601 25896.934  5.099  3.118
                      "Greece"  2012  17173.71 22171.911  8.972  1.035
                      "Greece"  2013  16742.04 21805.257   9.56  -.854
                      "Greece"  2014 16984.502  21726.89  10.28 -1.394
                      "Malta"   2010 15939.801 21150.423 18.954  2.041
                      "Malta"   2011 16113.851 22933.749 19.254  2.513
                      "Malta"   2012 16450.882  22069.52 19.718  3.227
                      "Malta"   2013 17005.342 24021.743 21.781   .979
                      "Malta"   2014 18097.074 26202.597 26.674   .771
                      end
                      Code:
                      *label variables
                      label    variable    NGDPRPC    "Gross domestic product per capita, constant prices"
                      label    variable    NGDPDPC    "Gross domestic product per capita, current prices"
                      label    variable    NGSD_NGDP    "Gross national savings"
                      label    variable    PCPIPCH    "Inflation, average consumer prices"
                      *
                      *generate rank variabels : highest is rank 1
                      sort year
                      foreach v in NGDPRPC NGDPDPC NGSD_NGDP PCPIPCH {
                          by year: egen rank_`v' = rank(-`v')
                      }
                      I would like to generate bar graphs (rank 1 on top) for each year (separately), and highlight a single country (Cyprus, for example), whose ranking may change over the years.

                      Last, thank you for the code in # 9. It looks great! How do I put variable label instead of variable name, and rank on reversed scale i.e. 1 on top in the graph you provided?

                      Comment


                      • #12
                        If you assign variable labels, they will be used automatically in the code of #9.

                        This appears to be what you were driving towards in #10 and some earlier posts. The main trick is precisely that documented in https://www.stata-journal.com/articl...article=gr0049 (I can't comment on what you found difficult in that, because you don't say). But some other tricks are included in the code.

                        Note that quietly is not a good idea until the code is debugged.

                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input str32 country int year float(rank_NGDPRPC rank_NGDPDPC rank_NGSD_NGDP rank_PCPIPCH)
                        "Greece"  2010 2 2 4 1
                        "Estonia" 2010 4 4 1 2
                        "Cyprus"  2010 1 1 3 3
                        "Malta"   2010 3 3 2 4
                        "Greece"  2011 2 2 4 3
                        "Malta"   2011 3 3 2 4
                        "Estonia" 2011 4 4 1 1
                        "Cyprus"  2011 1 1 3 2
                        "Malta"   2012 3 3 2 2
                        "Greece"  2012 2 2 4 4
                        "Cyprus"  2012 1 1 3 3
                        "Estonia" 2012 4 4 1 1
                        "Estonia" 2013 4 4 1 1
                        "Greece"  2013 3 3 3 4
                        "Malta"   2013 2 2 2 2
                        "Cyprus"  2013 1 1 4 3
                        "Cyprus"  2014 2 1 4 3
                        "Estonia" 2014 4 4 1 2
                        "Malta"   2014 1 2 2 1
                        "Greece"  2014 3 3 3 4
                        end
                        
                        local names 
                        gen tosort = . 
                        
                        quietly foreach var in NGDPRPC NGDPDPC NGSD PCPIPCH {
                           separate rank_`var', by(country == "Cyprus") veryshortlabel gen(toplot) 
                           forval year= 2010/2014 {
                               replace tosort = cond(year == `year', rank_`var', .) 
                               graph hbar (asis) toplot* if year==`year', nofill over(country, sort(tosort)) ///
                               title(`var' `year') legend(off) name(gr_`var'_`year', replace) nodraw 
                               local names `names' gr_`var'_`year' 
                           } 
                           drop toplot* 
                        }
                        
                        drop tosort 
                        
                        graph combine `names'
                        All that said, the result is in competition with #9, and any other ideas.

                        Click image for larger version

Name:	budu2.png
Views:	1
Size:	42.5 KB
ID:	1491161

                        Comment


                        • #13
                          Nick Cox Thanks a lot! Just another request : How would the code in # 12 change if I want to produce 20 different graphs i.e. one graph per variable?

                          Comment


                          • #14
                            That's set as an exercise. Teacher is getting on with his own work!

                            Comment


                            • #15
                              Nick Cox I will try to do the homework!

                              Comment

                              Working...
                              X