Announcement

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

  • problem with legend

    Using the below codes, I could create three graphs and combine them into one nice graph. As shown in the below image, the position and appearance of the legend is not quite right. I would like to have the legend right below the combined graphs. Also, I was wondering if there is any way to wrap up the code using a loop.


    twoway kdensity TLE_50_511 || kdensity TLE_50_512 || kdensity TLE_50_513, xtitle("Total Life Expectancy (TLE)") legend(off) name(gr1, replace)

    twoway kdensity HLE_50_511 || kdensity HLE_50_512 || kdensity HLE_50_513, legend( pos(6) rows(1) bmargin(large) order (1 "Married non_caregiver" 2 "Spouse caregiver <14h/w" 3 "Spouse caregiver >=14h/w")) xtitle("Healthy Life Expectancy (HLE)") name(gr2, replace)


    twoway kdensity PLE_50_511 || kdensity PLE_50_512 || kdensity PLE_50_513, xtitle("PLE (HLE/TLE)") legend(off) name(gr3, replace)

    graph combine gr1 gr2 gr3 , ///
    rows(1) xsize(12.5) ysize(6)


    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	101.4 KB
ID:	1541173




  • #2
    You will always have a scaling problem if you include a legend in some graphs and not others and then combine them. This uses grc1leg by Vince Wiggins and is not tested as you do not provide a reproducible example.

    Code:
    local optsTLE "xtitle("Total Life Expectancy (TLE)") legend(off) name(gr1, replace)"
    local optsHLE "legend( pos(6) rows(1) bmargin(large) order (1 "Married non_caregiver" 2 "Spouse caregiver <14h/w" 3 "Spouse caregiver >=14h/w")) xtitle("Healthy Life Expectancy (HLE)") name(gr2, replace)"
    local optsPLE "xtitle("PLE (HLE/TLE)") legend(off) name(gr3, replace)"
    
    foreach var in TLE HLE PLE{
    tw kdensity `var'_50_511 || kdensity `var'_50_512 || kdensity `var'_50_513, `opts`var''
    }
    net install grc1leg, from (http://www.stata.com/users/vwiggins)
    grc1leg gr1 gr2 gr3 ,rows(1) xsize(12.5) ysize(6) legendfrom(gr2)
    Last edited by Andrew Musau; 13 Mar 2020, 10:43.

    Comment


    • #3
      Thanks. Below is the combined graph using your code. Graph #1 is exactly the same as post#1 but two others are different. Also, the appearance of the legend does not look readable enough. Is there any way to fix this.

      Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	129.9 KB
ID:	1541195


      Comment


      • #4
        It seems that you need to adjust the x axis in both graphs and you could have a legend spanning multiple rows as it is too long. If you provide a data example, I can attempt to tidy up. Otherwise, I do not combine graphs often, so the modifications are not apparent to me.

        Comment

        Working...
        X