Announcement

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

  • legend position

    Dear All, I have this data set
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(_Y_treated _Y_synthetic year)
     4.797 4.7839860000000005 2009
     4.073 3.7585460000000004 2010
     3.814  4.156040000000001 2011
     4.693           4.559967 2012
     4.875           4.691477 2013
     3.608           3.828704 2014
     3.316           3.246187 2015
      8.17           5.311161 2016
    11.204  5.821659000000001 2017
     10.73  5.816053999999999 2018
    15.314           7.701801 2019
    end
    with code
    Code:
    twoway (line _Y_treated year) (line _Y_synthetic year), xline(2016, lp(dash))
    and resulting graph
    Click image for larger version

Name:	legend.png
Views:	1
Size:	65.2 KB
ID:	1594986


    My question is: how can I put the legend on the "upper left" (possibly in one or two rows) part of the graph? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Here is my example:
    Code:
    twoway (line _Y_treated year) (line _Y_synthetic year), xline(2016, lp(dash)) legend(position(11) cols(1) ring(0))
    which renders:
    Click image for larger version

Name:	Tutorial_Graph_Legend_Upper_Left_Corner.png
Views:	1
Size:	53.4 KB
ID:	1594989


    and to control the text of the variables of the legend use:
    Code:
    twoway (line _Y_treated year) (line _Y_synthetic year), xline(2016, lp(dash)) legend(position(11) cols(1) ring(0) lab(1 "treated unit") lab(2 "synthetic control unit"))
    which renders:
    Click image for larger version

Name:	Tutorial_Graph_Legend_Upper_Left_Corner2.png
Views:	1
Size:	55.0 KB
ID:	1594991

    Last edited by ericmelse; 25 Feb 2021, 01:53.
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Dear ericmelse, Thanks a lot. This is really helpful. In case I want to change, say "_Y_treated" to "AAA" and "_Y_synthetic" to "BBB", how can I do that?
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Dear River,
        Yes, my second example shows how to control legend text strings.
        More about such options can be read from the help file and in the manual.
        Best,
        Eric
        http://publicationslist.org/eric.melse

        Comment


        • #5
          Dear ericmelse, My bad. I missed the second figure. It works now. Thanks.

          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment

          Working...
          X