Announcement

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

  • coefplot - country label on xaxis instead of numbers

    Dear Statalist users,

    I am trying to produce this graph showing changes in a variable across countries from 1995 to 2010. This is the code I am using with Stata 18. Does anyone have any advice on how to get the ccode (NOR, USA, GBR etc.) labels instead of the numbers on the xaxis?

    Code:
    mean SK_RR_S100 if y==2010, over(ccode)
    matrix b88 = e(b)
    mean  SK_RR_S100 if y==1995, over(ccode)
    mata: assert(st_matrixcolstripe("b88")==st_matrixcolstripe("e(b)"))
    quietly estadd matrix b88
    coefplot, ci((b b) (b b88)) ciopts(recast(rcap pcarrow)) cionly ///
       vertical sort(, by(ul 2)) rename(^.+@([0-9]+)\..+$ = \1, regex) ///
       xtitle("test") ytitle("Change in sk_rep from 1995 to 2010")
    and I get this graph:
    Click image for larger version

Name:	graph_statalist.png
Views:	1
Size:	104.4 KB
ID:	1769455


  • #2
    coefplot is from SSC, as you are asked to explain (FAQ Advice #12). To make any such labels readable, you would likely need to flip the axes. Regarding your question, please provide a data example (refer to the referenced FAQ for details).

    Comment


    • #3
      Andrew: thanks for the reminder. For more context: I'm working with an unbalanced panel with N=31 and T=50 (for this graph, the US has no information because they do not have this type of policy (SK_RR_S100). CCODE label consist of three-letter country code.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input double(ccode y SK_RR_S100)
      1 1970                  .
      1 1971  .1490314564448123
      1 1972 .22108290608978368
      1 1973 .26279867213144936
      1 1974 .26559960322047965
      1 1975 .27233623343570973
      1 1976   .288849134851387
      1 1977 .32024585695653696
      1 1978  .3098841376149818
      1 1979 .29505050950637435
      1 1980  .2525459889098983
      1 1981 .23535648115217472
      1 1982 .22859200881318592
      1 1983 .24727451252962138
      1 1984 .26430078861742246
      1 1985  .2635630216411925
      1 1986  .2898393015571314
      1 1987 .29758726669868457
      1 1988  .3024782822466128
      1 1989 .29011140696457355
      1 1990 .30083486660800224
      1 1991  .3102643217799459
      1 1992   .322183891069193
      1 1993  .3088118989255487
      1 1994  .2991224500110392
      1 1995 .29728083038094044
      1 1996  .3087723761257178
      1 1997 .29998280993348986
      1 1998 .28912405511100875
      1 1999 .28709232149580105
      1 2000  .2750577530037783
      1 2001  .2743600009142581
      1 2002  .2600769658663214
      1 2003  .2561624824841846
      1 2004  .2601794917614027
      1 2005  .2543653187827645
      1 2006 .25097259772001806
      1 2007 .24675239945893138
      1 2008  .2393541427991614
      1 2009 .23417023680219776
      1 2010 .22833919195653365
      1 2011 .22362510655904838
      1 2012 .21909540065384062
      1 2013 .21325534998857246
      1 2014 .21481431323526218
      1 2015 .21691779843383077
      1 2016 .21751763810695313
      1 2017  .2181894004504813
      1 2018 .21718236086097298
      1 2019 .21552151737556047
      1 2020  .2107960207619468
      2 1970  .7377818264826091
      2 1971  .7425672677788261
      2 1972  .7513783095463765
      2 1973  .7395600352186933
      2 1974  .8678472428049153
      2 1975  .8869656218440207
      2 1976  .8556651445147881
      2 1977  .8651366687203876
      2 1978  .8662035600801308
      2 1979  .8556457059852272
      2 1980  .8671743524129336
      2 1981  .8736959616894158
      2 1982  .8689310733463569
      2 1983  .8646136604855573
      2 1984   .875145518032241
      2 1985  .8848562938809796
      2 1986  .8924320812594145
      2 1987   .875964009694303
      2 1988   .883257083566614
      2 1989   .829311938486311
      2 1990  .7996919870025553
      2 1991  .7973331104866977
      2 1992  .7787053545831498
      2 1993  .7659071005308834
      2 1994  .7496918034716041
      2 1995  .7558863782354146
      2 1996   .751670997892195
      2 1997  .7412930314616144
      2 1998  .7326748171504593
      2 1999  .7251447448438763
      2 2000  .7218581294916074
      2 2001  .8997571119475032
      2 2002  .9630914346238543
      2 2003  .9586498855803846
      2 2004  .9490048870396198
      2 2005  .9277326549619026
      2 2006  .9217806849589663
      2 2007  .9162847504544809
      2 2008  .9112955725579135
      2 2009  .8849949598880074
      2 2010  .8754624028870912
      2 2011  .8767336651080851
      2 2012  .8674735027287377
      2 2013  .8600721196634573
      2 2014  .8376151712061853
      2 2015 1.0006626769965732
      2 2016  .9640131070913923
      2 2017  .9678878890787516
      2 2018  .9733020495897372
      end
      label values ccode CCODE
      label def CCODE 1 "AUS", modify
      label def CCODE 2 "AUT", modify

      Comment


      • #4
        Thanks. You can run coefplot with the -nodraw- option, pick up the order of the labels from r(labels) and retrieve the labels from your categorical variable. Using these, redraw the graph. Vertical labels may just work, but otherwise see the -vertical- option to flip the axes.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input double(ccode y SK_RR_S100)
        1 1970                  .
        1 1971  .1490314564448123
        1 1972 .22108290608978368
        1 1973 .26279867213144936
        1 1974 .26559960322047965
        1 1975 .27233623343570973
        1 1976   .288849134851387
        1 1977 .32024585695653696
        1 1978  .3098841376149818
        1 1979 .29505050950637435
        1 1980  .2525459889098983
        1 1981 .23535648115217472
        1 1982 .22859200881318592
        1 1983 .24727451252962138
        1 1984 .26430078861742246
        1 1985  .2635630216411925
        1 1986  .2898393015571314
        1 1987 .29758726669868457
        1 1988  .3024782822466128
        1 1989 .29011140696457355
        1 1990 .30083486660800224
        1 1991  .3102643217799459
        1 1992   .322183891069193
        1 1993  .3088118989255487
        1 1994  .2991224500110392
        1 1995 .29728083038094044
        1 1996  .3087723761257178
        1 1997 .29998280993348986
        1 1998 .28912405511100875
        1 1999 .28709232149580105
        1 2000  .2750577530037783
        1 2001  .2743600009142581
        1 2002  .2600769658663214
        1 2003  .2561624824841846
        1 2004  .2601794917614027
        1 2005  .2543653187827645
        1 2006 .25097259772001806
        1 2007 .24675239945893138
        1 2008  .2393541427991614
        1 2009 .23417023680219776
        1 2010 .22833919195653365
        1 2011 .22362510655904838
        1 2012 .21909540065384062
        1 2013 .21325534998857246
        1 2014 .21481431323526218
        1 2015 .21691779843383077
        1 2016 .21751763810695313
        1 2017  .2181894004504813
        1 2018 .21718236086097298
        1 2019 .21552151737556047
        1 2020  .2107960207619468
        2 1970  .7377818264826091
        2 1971  .7425672677788261
        2 1972  .7513783095463765
        2 1973  .7395600352186933
        2 1974  .8678472428049153
        2 1975  .8869656218440207
        2 1976  .8556651445147881
        2 1977  .8651366687203876
        2 1978  .8662035600801308
        2 1979  .8556457059852272
        2 1980  .8671743524129336
        2 1981  .8736959616894158
        2 1982  .8689310733463569
        2 1983  .8646136604855573
        2 1984   .875145518032241
        2 1985  .8848562938809796
        2 1986  .8924320812594145
        2 1987   .875964009694303
        2 1988   .883257083566614
        2 1989   .829311938486311
        2 1990  .7996919870025553
        2 1991  .7973331104866977
        2 1992  .7787053545831498
        2 1993  .7659071005308834
        2 1994  .7496918034716041
        2 1995  .7558863782354146
        2 1996   .751670997892195
        2 1997  .7412930314616144
        2 1998  .7326748171504593
        2 1999  .7251447448438763
        2 2000  .7218581294916074
        2 2001  .8997571119475032
        2 2002  .9630914346238543
        2 2003  .9586498855803846
        2 2004  .9490048870396198
        2 2005  .9277326549619026
        2 2006  .9217806849589663
        2 2007  .9162847504544809
        2 2008  .9112955725579135
        2 2009  .8849949598880074
        2 2010  .8754624028870912
        2 2011  .8767336651080851
        2 2012  .8674735027287377
        2 2013  .8600721196634573
        2 2014  .8376151712061853
        2 2015 1.0006626769965732
        2 2016  .9640131070913923
        2 2017  .9678878890787516
        2 2018  .9733020495897372
        end
        label values ccode CCODE
        label def CCODE 1 "AUS", modify
        label def CCODE 2 "AUT", modify
        
        
        mean SK_RR_S100 if y==2010, over(ccode)
        matrix b88 = e(b)
        mean  SK_RR_S100 if y==1995, over(ccode)
        mata: assert(st_matrixcolstripe("b88")==st_matrixcolstripe("e(b)"))
        quietly estadd matrix b88
        coefplot, ci((b b) (b b88)) ciopts(recast(rcap pcarrow)) cionly ///
           vertical sort(, by(ul 2)) rename(^.+@([0-9]+)\..+$ = \1, regex)  ///
           xtitle("test") ytitle("Change in sk_rep from 1995 to 2010") nodraw
          
        local order= ustrregexra(ustrregexra(" "+`"`r(labels)'"', "\s[\d+]\s", ""), "[^\d\s]", " ")
        local labs
        local i 0
        foreach num of numlist `order'{
            local ++i
            local lev= "`:lab (ccode) `num''"
            local labs `labs' `i' "`lev'"
        }
        
        coefplot, ci((b b) (b b88)) ciopts(recast(rcap pcarrow)) cionly ///
           vertical sort(, by(ul 2)) rename(^.+@([0-9]+)\..+$ = \1, regex)  ///
           xtitle("test") ytitle("Change in sk_rep from 1995 to 2010") xlab(`labs', angle(vertical))

        Res.:
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	32.0 KB
ID:	1769489


        Comment


        • #5
          Excellent! Thanks so much for this.

          Comment

          Working...
          X