Announcement

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

  • Graphs for groups of countries

    Hello,

    I would like to know if you can help me with the code to create a graph (lines) of the "mean_gdpgrowth" for each group of countries (Scandinavian, Continental, Anglo-Saxon and Mediterranean) over the years. I would like to plot all the lines in the same picture.
    I send an example of the data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str52 country int year float(groups mean_gdpgrowth)
    "Ireland"        1995 3  6.603013
    "Greece"         1995 4   2.58135
    "Denmark"        1995 1 3.0275874
    "Belgium"        1995 2 2.5263705
    "Spain"          1995 4   2.58135
    "United Kingdom" 1995 3  6.603013
    "Italy"          1995 4   2.58135
    "Austria"        1995 2 2.5263705
    "Ireland"        1996 3  7.414229
    "Luxembourg"     1997 2  4.161689
    "Slovenia"       1997 2  4.161689
    "Austria"        1997 2  4.161689
    "Belgium"        1997 2  4.161689
    "Italy"          1998 4 1.8106152
    "Slovenia"       1999 2  4.201847
    "United Kingdom" 1999 3 3.4282234
    "Netherlands"    1999 1  5.034048
    "Hungary"        1999 2  4.201847
    "Finland"        2000 1  4.798251
    "Ireland"        2000 3   9.44575
    "Sweden"         2000 1  4.798251
    "Luxembourg"     2000 2  5.110733
    "Greece"         2000 4 4.3175735
    "Italy"          2000 4 4.3175735
    "Austria"        2000 2  5.110733
    "Belgium"        2000 2  5.110733
    "Spain"          2000 4 4.3175735
    "Denmark"        2000 1  4.798251
    "Portugal"       2003 4  1.996133
    "Spain"          2003 4  1.996133
    "France"         2003 2  1.107968
    "Belgium"        2003 2  1.107968
    "Greece"         2003 4  1.996133
    "Luxembourg"     2003 2  1.107968
    "Sweden"         2003 1 1.5461222
    "Denmark"        2003 1 1.5461222
    "Italy"          2003 4  1.996133
    "Austria"        2003 2  1.107968
    "Ireland"        2003 3  3.019969
    "Finland"        2003 1 1.5461222
    "Netherlands"    2004 1 3.2429085
    "United Kingdom" 2004 3 4.5476522
    "Italy"          2004 4 2.8490295
    "Portugal"       2004 4 2.8490295
    "Greece"         2004 4 2.8490295
    "Belgium"        2004 2 3.8665605
    "Ireland"        2004 3 4.5476522
    "Finland"        2004 1 3.2429085
    "Luxembourg"     2004 2 3.8665605
    "Sweden"         2004 1 3.2429085
    "Spain"          2004 4 2.8490295
    "Austria"        2004 2 3.8665605
    "Poland"         2004 2 3.8665605
    "Denmark"        2004 1 3.2429085
    "Slovenia"       2004 2 3.8665605
    "France"         2004 2 3.8665605
    "Hungary"        2004 2 3.8665605
    "Poland"         2005 2  2.990961
    "Greece"         2005 4 1.4627178
    "Ireland"        2005 3  4.439909
    "Hungary"        2005 2  2.990961
    "Denmark"        2005 1  2.515758
    "Spain"          2005 4 1.4627178
    "France"         2005 2  2.990961
    "Luxembourg"     2005 2  2.990961
    "Italy"          2005 4 1.4627178
    "Sweden"         2005 1  2.515758
    "Finland"        2005 1  2.515758
    "Netherlands"    2005 1  2.515758
    "Austria"        2005 2  2.990961
    "Slovenia"       2005 2  2.990961
    "Portugal"       2005 4 1.4627178
    "United Kingdom" 2005 3  4.439909
    "Belgium"        2005 2  2.990961
    "Ireland"        2006 3 3.9296126
    "Italy"          2006 4 3.2927086
    "Poland"         2006 2 4.2273226
    "Netherlands"    2006 1  4.000984
    "Spain"          2006 4 3.2927086
    "Luxembourg"     2006 2 4.2273226
    "France"         2006 2 4.2273226
    "Greece"         2006 4 3.2927086
    "Portugal"       2006 4 3.2927086
    "Austria"        2006 2 4.2273226
    "Hungary"        2006 2 4.2273226
    "Belgium"        2006 2 4.2273226
    "Denmark"        2006 1  4.000984
    "Slovenia"       2006 2 4.2273226
    "Finland"        2006 1  4.000984
    "United Kingdom" 2006 3 3.9296126
    "Sweden"         2006 1  4.000984
    "Ireland"        2007 3 3.8774104
    "Sweden"         2007 1  3.352993
    "Finland"        2007 1  3.352993
    "Denmark"        2007 1  3.352993
    "France"         2007 2  4.634399
    "Netherlands"    2007 1  3.352993
    "Austria"        2007 2  4.634399
    "Slovenia"       2007 2  4.634399
    "Hungary"        2007 2  4.634399
    end
    format %ty year
    label values groups gnames
    label def gnames 1 "Scandinavian", modify
    label def gnames 2 "Continental", modify
    label def gnames 3 "Anglo_Saxon", modify
    label def gnames 4 "Mediterranean", modify

    Thank you in advance.

  • #2
    Here are two ways:
    Code:
    sort country year
    line mean_g year, c(L) by(group) 
    //or
    encode country, gen(id_country)
    xtset id year
    levelsof group,local(levels)
    foreach l of local levels {
            local v : label (group) `l'
            display "`v'"
            xtline mean if group == `l' , overlay title("`v'") name(gr`l',replace)
            
    }
    graph combine gr1 gr2 gr3 gr4

    Comment


    • #3
      Scott Merryman gives excellent advice. Here is a related idea using fabplot from SSC. See https://www.statalist.org/forums/forum/general-stata-discussion/general/270264-subsetplot-available-on-ssc for more background.

      Code:
      set scheme s1color 
      
      fabplot line mean year, by(group) xla(1995(3)2007, format(%tyYY)) xtitle("") frontopts(lc(orange_red) lw(thick))


      Click image for larger version

Name:	fabplot.png
Views:	1
Size:	62.0 KB
ID:	1549293

      Comment


      • #4
        Hello,

        Thank you for your message. Can you help me with the code to put all groups in the same picture? With one yaxis and xaxis
        Thank you in advance

        Comment


        • #5
          From the graphs in #3, you can see that you'll just end up with a mess, but if that's what you want...

          Code:
          preserve
          collapse mean_gdpgrowth, by(groups year)
          decode groups, gen(name)
          gen mean_gdpgrowth2= mean_gdpgrowth
          replace mean_gdpgrowth2= mean_gdpgrowth2-0.29 if _n==_N
          bys groups (year): replace name="" if _n!=_N
          xtset groups year
          xtline mean_gdpgrowth, addplot(scatter mean_gdpgrowth2 year, ///
          mcolor(none) mlab(name)) leg(off) ytitle("GDP growth") xtitle("") ///
          scheme(s1color) overlay
          restore
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	99.0 KB
ID:	1549406

          Comment


          • #6
            Thank you very much!

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Scott Merryman gives excellent advice. Here is a related idea using fabplot from SSC. See https://www.statalist.org/forums/forum/general-stata-discussion/general/270264-subsetplot-available-on-ssc for more background.

              Code:
              set scheme s1color
              
              fabplot line mean year, by(group) xla(1995(3)2007, format(%tyYY)) xtitle("") frontopts(lc(orange_red) lw(thick))


              [ATTACH=CONFIG]n1549293[/ATTACH]
              Hi Nick, is there a convenient command like this but allows us to 1) hide those grey lines from the background (i.e., show only one line in each subplot) and 2) nicely label each subplot using the var label of the by var and 3) only show y and x axis for subplots on the left and at the bottom? Thank you!
              Last edited by shem shen; 02 Mar 2022, 21:32.

              Comment


              • #8
                That is what a by() option does.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  That is what a by() option does.
                  Thank you!

                  Comment


                  • #10
                    I did this for the data example in #1. This uses value labels.

                    Code:
                    bysort groups year : keep if _n == 1
                    drop country
                    set scheme s1color
                    line mean year, by(groups, note("")) xsc(r(1994.5 2007.5)) yla(, ang(h)) xla(1995(2)2007) xtitle("")
                    Last edited by Nick Cox; 03 Mar 2022, 13:41.

                    Comment

                    Working...
                    X