Announcement

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

  • Labelling lines directly on the graph

    Hello I have the following data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year float(tw_eshare sk_eshare jp_eshare hk_eshare us_eshare ch_eshare)
    1999   .353104  .2180597   .132673  .07837033 .013242254 .0037732776
    2000  .3613982  .1935984 .14054033  .09470285  .01197752 .0041396185
    2001  .3733647 .18836835 .13537122  .09850802 .011657768  .008487331
    2002  .3761892  .2064878 .12076926  .11045322 .011942135  .011063125
    2003  .3783749  .2163858 .11433821  .09861916 .011756462  .016428739
    2004  .3859389  .2263669 .11611678  .08417509 .012050012  .016369808
    2005  .3845172 .22625177  .1219711  .07566063 .012466455   .02556598
    2006 .37641215 .23493522  .1331212 .070133805 .013106585   .02747921
    2007  .3615798 .24617197 .14213298  .06388681 .015180472  .032136057
    2008  .3476018  .2535338 .14422223  .06750516 .014348704   .03782948
    2009  .3287824 .24717003  .1626905  .06264513  .01920605   .04352529
    2010  .3215968 .25062367 .16644105  .06488687  .01863375    .0437016
    2011  .3131113 .25433823   .173319  .06182805 .016680311   .04585869
    2012  .3052285 .25912577 .17756553   .0608872 .016151281   .04603336
    2013  .3044777 .26401043  .1663623  .06046329 .017376207   .04823454
    2014  .2974295 .28048614  .1559516 .065785125  .01672531   .05081726
    2015 .28691626 .29673535 .14978747  .06400553 .016013706   .05590628
    2016 .27359244  .3085391   .143486 .065558076 .015584015   .06190511
    2017 .26286304  .3131993 .14147463  .06799361 .015544648   .07033642
    end

    and the following code:

    Code:
    twoway (connected tw_eshare year, mcolor(navy) msize(vsmall) msymbol(circle) lcolor(navy) lpattern(solid)) (connected sk_eshare year, mcolor(midgreen) msize(vsmall) msymbol(square) lcolor(midgreen) lpattern(dash)) (connected jp_eshare year, mcolor(purple) msize(vsmall) msymbol(triangle) lcolor(purple) lpattern(longdash)) (connected hk_eshare year, mcolor(maroon) msize(vsmall) msymbol(diamond) lcolor(maroon) lpattern(dash_dot)) (connected us_eshare year, mcolor(orange_red) msize(small) msymbol(lgx) lcolor(orange_red) lpattern(shortdash_dot)) (connected ch_eshare year, mcolor(red) msize(small) msymbol(plus) lcolor(red) lpattern(longdash_shortdash)), legend(off)
    My chart does not look the best right now, but I'm trying to get there. I would like to know how I can add a label directly next to the lines for each of these series, for example, each series represents a country, so next to the end of each line on the graph I would like to label it with the country for which it corresponds. The countries are Taiwan, South Korea, Japan, Hong Kong, US, China.

    Thanks,
    Jad

  • #2
    HTML Code:
    https://geterika.com/stats/world-bank-indicators-in-stata

    Comment


    • #3
      If you want to keep your data in the original wide format, you can use scatteri to label the last value.

      If you are willing to reshape to long, you can use mostly missing marker labels. I find the second easier since many Stata commands for panel data require this format and the code is more compact (once the data is transformed)

      Here is an example:

      Code:
      clear
      input int year float(tw_eshare sk_eshare jp_eshare hk_eshare us_eshare ch_eshare)
      1999   .353104  .2180597   .132673  .07837033 .013242254 .0037732776
      2000  .3613982  .1935984 .14054033  .09470285  .01197752 .0041396185
      2001  .3733647 .18836835 .13537122  .09850802 .011657768  .008487331
      2002  .3761892  .2064878 .12076926  .11045322 .011942135  .011063125
      2003  .3783749  .2163858 .11433821  .09861916 .011756462  .016428739
      2004  .3859389  .2263669 .11611678  .08417509 .012050012  .016369808
      2005  .3845172 .22625177  .1219711  .07566063 .012466455   .02556598
      2006 .37641215 .23493522  .1331212 .070133805 .013106585   .02747921
      2007  .3615798 .24617197 .14213298  .06388681 .015180472  .032136057
      2008  .3476018  .2535338 .14422223  .06750516 .014348704   .03782948
      2009  .3287824 .24717003  .1626905  .06264513  .01920605   .04352529
      2010  .3215968 .25062367 .16644105  .06488687  .01863375    .0437016
      2011  .3131113 .25433823   .173319  .06182805 .016680311   .04585869
      2012  .3052285 .25912577 .17756553   .0608872 .016151281   .04603336
      2013  .3044777 .26401043  .1663623  .06046329 .017376207   .04823454
      2014  .2974295 .28048614  .1559516 .065785125  .01672531   .05081726
      2015 .28691626 .29673535 .14978747  .06400553 .016013706   .05590628
      2016 .27359244  .3085391   .143486 .065558076 .015584015   .06190511
      2017 .26286304  .3131993 .14147463  .06799361 .015544648   .07033642
      end
      
      
      /* Wide Format Version */
      
      /* (1) Store the last share in a scalar */
      foreach c in tw sk jp hk us ch {
          summarize `c'_eshare if year == 2017, meanonly
          scalar `c'_last_eshare = r(mean)
      }
      
      /* (2) Plot with scatteri */
      twoway ///
      (connected tw_eshare year, mcolor(navy) msize(vsmall) msymbol(circle) lcolor(navy) lpattern(solid)) ///
      (connected sk_eshare year, mcolor(midgreen) msize(vsmall) msymbol(square) lcolor(midgreen) lpattern(dash)) ///
      (connected jp_eshare year, mcolor(purple) msize(vsmall) msymbol(triangle) lcolor(purple) lpattern(longdash)) ///
      (connected hk_eshare year, mcolor(maroon) msize(vsmall) msymbol(diamond) lcolor(maroon) lpattern(dash_dot)) ///
      (connected us_eshare year, mcolor(orange_red) msize(small) msymbol(lgx) lcolor(orange_red) lpattern(shortdash_dot)) ///
      (connected ch_eshare year, mcolor(red) msize(small) msymbol(plus) lcolor(red) lpattern(longdash_shortdash)) ///
      (scatteri `=scalar(tw_last_eshare)' 2017 (3) "Taiwan `=ustrunescape("\uD83C\uDDF9\uD83C\uDDFC")'", msymbol(i) mlabcolor(navy) mlabsize(medlarge)) ///
      (scatteri `=scalar(sk_last_eshare)' 2017 (3) "South Korea `=ustrunescape("\uD83C\uDDF0\uD83C\uDDF7")'", msymbol(i) mlabcolor(midgreen) mlabsize(medlarge)) ///
      (scatteri `=scalar(jp_last_eshare)' 2017 (3) "Japan `=ustrunescape("\uD83C\uDDEF\uD83C\uDDF5")'", msymbol(i) mlabcolor(purple) mlabsize(medlarge)) ///
      (scatteri `=scalar(hk_last_eshare)' 2017 (3) "Hong Kong `=ustrunescape("\uD83C\uDDED\uD83C\uDDF0")'", msymbol(i) mlabcolor(maroon) mlabsize(medlarge)) ///
      (scatteri `=scalar(us_last_eshare)' 2017 (3) "Unites States `=ustrunescape("\uD83C\uDDFA\uD83C\uDDF8")'", msymbol(i) mlabcolor(orange_red) mlabsize(medlarge)) ///
      (scatteri `=scalar(ch_last_eshare)' 2017 (2) "China `=ustrunescape("\uD83C\uDDE8\uD83C\uDDF3")'", msymbol(i) mlabcolor(red) mlabsize(medlarge) xlab(1999(2)2017) graphregion(m(r+27))) ///
      , legend(off) name(gph_wide, replace) ytitle("Share")
      
      
      /* Easier Long Format Version */
      rename (*_eshare) (share*)
      reshape long share, i(year) j(country, string)
      strrec country ("tw" = 1 "Taiwan `=ustrunescape("\uD83C\uDDF9\uD83C\uDDFC")'") ///
      ("sk" = 2 "South Korea `=ustrunescape("\uD83C\uDDF0\uD83C\uDDF7")'") ///
      ("jp" = 3 "Japan `=ustrunescape("\uD83C\uDDEF\uD83C\uDDF5")'") ///
      ("hk" = 4 "Hong Kong `=ustrunescape("\uD83C\uDDED\uD83C\uDDF0")'") ///
      ("us" = 5 "Unites States `=ustrunescape("\uD83C\uDDFA\uD83C\uDDF8")'") ///
      ("ch" = 6 "China `=ustrunescape("\uD83C\uDDE8\uD83C\uDDF3")'"), replace 
      decode country if year== 2017, gen(label)
      xtset country year
       
      xtline share, overlay legend(off) xlab(1999(2)2017) graphregion(m(r+25)) name(gph_long, replace) ytitle("Share") ///
      plot1opts(recast(connected) mcolor(navy) msize(vsmall) msymbol(circle) lcolor(navy) lpattern(solid) mlab(label) mlabcolor(navy) mlabsize(medlarge)) ///
      plot2opts(recast(connected) mcolor(midgreen) msize(vsmall) msymbol(square) lcolor(midgreen) lpattern(dash) mlab(label)mlabcolor(midgreen) mlabsize(medlarge)) ///
      plot3opts(recast(connected) mcolor(purple) msize(vsmall) msymbol(triangle) lcolor(purple) lpattern(longdash) mlab(label) mlabcolor(purple) mlabsize(medlarge)) ///
      plot4opts(recast(connected) mcolor(maroon) msize(vsmall) msymbol(diamond) lcolor(maroon) lpattern(dash_dot) mlab(label) mlabcolor(maroon) mlabsize(medlarge)) ///
      plot5opts(recast(connected) mcolor(orange_red) msize(small) msymbol(lgx) lcolor(orange_red) lpattern(shortdash_dot) mlab(label) mlabcolor(orange_red) mlabsize(medlarge)) ///
      plot6opts(recast(connected) mcolor(red) msize(small) msymbol(plus) lcolor(red) lpattern(longdash_shortdash) mlab(label)mlabcolor(red) mlabsize(medlarge) mlabpos(2))
      This should get you something like this:

      Click image for larger version

Name:	Screen Shot 2024-05-21 at 4.43.45 PM.png
Views:	1
Size:	148.1 KB
ID:	1754074

      If you don't like flags, just get rid of the `=ustrunescape("XXXXXX")'.

      Comment


      • #4
        Thanks alot guys!

        Comment


        • #5
          I agree strongly that

          * direct labelling can be a great improvement on a legend.

          * a long layout is preferable (although in Dimitriy V. Masterov 's excellent answer watch out for his typo "Unites States")


          That said, there are as always other choices that might work as well or better.

          Here I use

          1. a front-and-back plot as discussed in https://journals.sagepub.com/doi/pdf...6867X211025838

          2. ordering other than alphabetical (using the most recent value is one example) as discussed in https://journals.sagepub.com/doi/pdf...6867X211045582

          3. log scale (logit scale would be similar here)

          4. percent labels to avoid several leading zeros: see also https://journals.sagepub.com/doi/pdf...6867X221141058

          5, zapping the time axis title (no predictable reader needs to see the text "year")


          That is, fabplot mylabels myaxis are all from the Stata Journal.

          Code:
          clear
          input int year float(tw_eshare sk_eshare jp_eshare hk_eshare us_eshare ch_eshare)
          1999   .353104  .2180597   .132673  .07837033 .013242254 .0037732776
          2000  .3613982  .1935984 .14054033  .09470285  .01197752 .0041396185
          2001  .3733647 .18836835 .13537122  .09850802 .011657768  .008487331
          2002  .3761892  .2064878 .12076926  .11045322 .011942135  .011063125
          2003  .3783749  .2163858 .11433821  .09861916 .011756462  .016428739
          2004  .3859389  .2263669 .11611678  .08417509 .012050012  .016369808
          2005  .3845172 .22625177  .1219711  .07566063 .012466455   .02556598
          2006 .37641215 .23493522  .1331212 .070133805 .013106585   .02747921
          2007  .3615798 .24617197 .14213298  .06388681 .015180472  .032136057
          2008  .3476018  .2535338 .14422223  .06750516 .014348704   .03782948
          2009  .3287824 .24717003  .1626905  .06264513  .01920605   .04352529
          2010  .3215968 .25062367 .16644105  .06488687  .01863375    .0437016
          2011  .3131113 .25433823   .173319  .06182805 .016680311   .04585869
          2012  .3052285 .25912577 .17756553   .0608872 .016151281   .04603336
          2013  .3044777 .26401043  .1663623  .06046329 .017376207   .04823454
          2014  .2974295 .28048614  .1559516 .065785125  .01672531   .05081726
          2015 .28691626 .29673535 .14978747  .06400553 .016013706   .05590628
          2016 .27359244  .3085391   .143486 .065558076 .015584015   .06190511
          2017 .26286304  .3131993 .14147463  .06799361 .015544648   .07033642
          end
          
          reshape long @_eshare, i(year) j(country) string 
          
          gen Country = "Taiwan" if country == "tw"
          replace Country = "South Korea" if country == "sk"
          replace Country = "United States" if country == "us"
          replace Country = "China" if country == "ch"
          replace Country = "Hong Kong" if country == "hk"
          replace Country = "Japan" if country == "jp"
          
          myaxis id=Country, sort(mean _eshare) descending subset(year == 2017)
          
          mylabels 1 3 10 30, myscale(@/100) local(yla)
           
          fabplot line _eshare year, by(id) ytitle(% share) frontopts(lw(thick))  ///
          xla(1999 "1999" 2017 "2017" 2002(3)2014, format(%tyYY)) ysc(log) xtitle("") ///
          yla(0.003 "0.3" `yla')

          Click image for larger version

Name:	share.png
Views:	1
Size:	124.2 KB
ID:	1754093

          Comment


          • #6
            A lot of useful stuff here. Thanks.

            Comment

            Working...
            X