Announcement

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

  • How to combine legend for a twoway figure

    Hi, Everyone,

    I'm trying to create a figure plotting the independent variable and dependent variable showing the difference by two groups by a cutoff point of the independent variable (sort of a regression discontinuity combined with dif-in-dif). I have tried different ways but couldn't figure out how to consolidate the labels in the legend for the four plots.

    The data I'm using is simply for illustration purposes.
    In the code below, the two groups are smaller household(<=262) VS bigger household (>262), dependent variable is income_pc, and I want to show how the relationship between income_pc and fips vary by a cutoff point of the independent variable (fips=252800) in two groups.
    In the figure generated from this code, how do I consolidate the legend to showing only one label for 95% CI, one label for orange line (smaller household), and one label for blue line (bigger household)?

    Code:
    use http://www.stata-press.com/data/r7/emd.dta
    twoway (lpolyci income_pc fips if  hhsize<=262 & fips<=252800 , clcolor(orange))(lpolyci income_pc fips if  hhsize<=262 & fips>252800, clcolor(orange))(lpolyci income_pc fips if  hhsize>262 & fips<=252800, clcolor(blue))(lpolyci income_pc fips if  hhsize>262 & fips>252800, clcolor(blue))

    Attached Files

  • #2
    Thanks for the reproducible example. See the -order()- suboption of -legend()-.

    Code:
    help legend_options
    The lines on the graph are partially obscured by the shaded area in some sections. This is a classical scenario where using transparency would be beneficial.

    Code:
    use http://www.stata-press.com/data/r7/emd.dta, clear
    twoway (lpolyci income_pc fips if  hhsize<=262 & fips<=252800 , ///
    color(gray%40) clcolor(orange))(lpolyci income_pc fips if  hhsize<=262 & fips>252800, ///
    color(gray%40) clcolor(orange))(lpolyci income_pc fips if  hhsize>262 & fips<=252800, ///
    color(gray%40) clcolor(blue))(lpolyci income_pc fips if  hhsize>262 & fips>252800, ///
    color(gray%40) clcolor(blue)  leg(order(1 2 "Small" 6 "Large")))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	110.5 KB
ID:	1751349

    Last edited by Andrew Musau; 26 Apr 2024, 07:20.

    Comment


    • #3
      @Andrew Musau Thank you so much Andrew for your help! That resolved my problem!

      Comment

      Working...
      X