Announcement

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

  • Adding a label to the last observation on a line chart

    I'm pretty sure I've seen this here before, but I can't seem to find the post. I'm interested in adding the value of the last observation to a line chart. Consider the data below,

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year float unemployment
    1990  5.9
    1991    8
    1992  9.9
    1993  9.9
    1994  9.3
    1995    8
    1996  8.1
    1997  6.9
    1998  6.6
    1999  5.9
    2000  5.4
    2001  5.7
    2002  6.7
    2003    7
    2004  6.5
    2005  5.4
    2006  4.8
    2007  5.1
    2008  7.6
    2009 11.6
    2010 12.5
    2011 12.2
    2012 10.9
    2013  9.8
    2014  8.3
    2015  6.6
    2016  5.3
    2017  4.8
    2018  4.7
    end
    I would like a line chart (twoway line unemployment year) with the value of the last observation appended to the line (4.7). I've attached a screenshot that I found of someone doing this using Stata (if that helps to explain what I'm getting at - I know not everyone is fond of screenshots because they're often not easy to see).

    Click image for larger version

Name:	Screen Shot 2019-04-25 at 8.44.11 AM.png
Views:	1
Size:	57.1 KB
ID:	1495077

  • #2
    Thanks for the data example. Combine a line graph and scatter plot.

    Code:
    sort year
    gen tag=year== year[_N]
    tw (line unemployment year) (scatter unemployment year if tag, mlabel(year) legend(order(1)) scheme(s1color))
    Result:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	32.7 KB
ID:	1495081



    ADDED IN EDIT: You want mlabel(unemployment)
    Last edited by Andrew Musau; 25 Apr 2019, 10:24.

    Comment


    • #3
      That's what I was looking for. Thanks, Andrew.

      Comment


      • #4
        https://www.statalist.org/forums/for...-in-panel-data may help too.

        With your example, this works:

        Code:
        set scheme s1color 
        gen toshow = string(une, "%2.1f") 
        line une year, lc(blue) || scatter une year if year == 2018, ms(none) mlabc(blue) mlabsize(medlarge) mlabel(toshow) legend(off)
        Matching the colours is fiddly but worthwhile with two or more series. Your "someone" was good, but not outstanding. (What's with the slanting year labels? There's enough space to avoid that.)

        Pushing the outcome variable through string() is a way to stop puzzling labels such as 4.6999998 which can appear.

        Comment


        • #5
          That's very helpful. Thank you, Nick!

          Comment

          Working...
          X