Announcement

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

  • Can I save the last graph's legend as a local?

    Dear Stata users,

    Suppose I'm producing two graphs, say plot1 and plot2. I want to suppress plot2's default legend, and use the legend of plot1 instead. So, can I save the legend of plot1 as a local? Codes would be something like:
    Code:
    twoway scatter weight price mpg, sort
    local alegend ...
    twoway whatever..., legend(`alegend')

  • #2
    If you save a graph as a .gph file you can see instructions for the legend. But I think it would take an extraordinarily deep knowledge of Stata graphics to write code to do what you want. A graph command doesn't leave much in its wake otherwise.

    Comment


    • #3
      Dear Nick, thank you. I find it is difficult whether for a general case or for my particular case. In the following code, all commands I used are from SSC. In this particular case, -addplot- produces unnecessary legend beacuse the legend of plot1 produced by -mscatter- is informative enough. If I can save the legend of plot1 as a local and then use it as an option when executing addplot, it will be very convenient. However, as you have already put in #1, that is a very troublesome task. In that case, I think it's not worthy of devoting time to.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int(mpg weight) byte foreign float(hv0 hv1)
      12 4720 0 12  .
      12 4840 0 12  .
      21 4290 0 21  .
      28 3260 0 28  .
      34 1800 0 34  .
      28 1800 0 28  .
      21 2650 0 21  .
      14 3830 0 14  .
      12 4720 0 12  .
      14 3420 1  . 14
      41 2040 1  . 41
      28 1760 1  . 28
      26 1830 1  . 26
      21 2130 1  . 21
      18 2410 1  . 18
      14 3420 1  . 14
      14 4330 0  .  .
      20 2830 0  .  .
      14 4130 0  .  .
      21 4060 0  .  .
      14 4060 0  .  .
      17 3350 0  .  .
      30 2120 0  .  .
      25 2200 0  .  .
      14 3900 0  .  .
      29 2110 0  .  .
      17 2830 1  .  .
      23 2070 1  .  .
      17 3170 1  .  .
      25 1930 1  .  .
      18 2670 1  .  .
      35 2020 1  .  .
      35 2050 1  .  .
      22 2580 0  .  .
      15 4080 0  .  .
      15 3720 0  .  .
      16 4030 0  .  .
      26 2230 0  .  .
      26 2520 0  .  .
      22 3220 0  .  .
      30 1980 1  .  .
      31 2200 1  .  .
      25 2650 1  .  .
      23 2160 1  .  .
      25 1990 1  .  .
      21 2750 1  .  .
      24 2750 0  .  .
      24 2690 0  .  .
      18 3700 0  .  .
      22 2640 0  .  .
      22 3180 0  .  .
      24 2730 0  .  .
      16 3600 0  .  .
      16 3880 0  .  .
      16 3690 0  .  .
      24 2280 1  .  .
      23 2370 1  .  .
      25 2240 1  .  .
      22 2930 0  .  .
      17 3740 0  .  .
      18 3330 0  .  .
      19 3200 0  .  .
      18 3690 0  .  .
      18 3670 0  .  .
      18 3470 0  .  .
      20 3250 0  .  .
      18 3600 0  .  .
      18 3370 0  .  .
      19 3210 0  .  .
      20 3280 0  .  .
      19 3300 0  .  .
      19 3420 0  .  .
      19 3400 0  .  .
      19 3370 0  .  .
      19 3310 0  .  .
      19 3430 0  .  .
      end
      label values foreign origin
      label def origin 0 "Domestic", modify
      label def origin 1 "Foreign", modify
      
      graph drop _all
      mscatter mpg weight, over(foreign) alegend name(plot1)
      colorpalette st, nograph
      addplot plot1: line hv0 hv1 weight, lcolor(`r(p)')
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	222.6 KB
ID:	1774705

      Comment


      • #4
        After plotting, the serset command can capture the variable names used to create the graph. By default, variable labels are used in the legend if they are present. You can enhance the following program to check whether labels exist and use variable names if they do not.

        Code:
        cap prog drop legend1
        program legend1, rclass
            preserve
            qui serset use
            restore
            local legend1
            local count = wordcount("`r(varnames)'")
            forval i=1/ `=`count'-1'{
                local legend1 `legend1' `i' "`:var lab `=word("`r(varnames)'", `i')''"
            } 
            return local legend1 "on order(`legend1')"
        end
        
        
        sysuse auto, clear
        twoway scatter weight price mpg, sort saving(gr1, replace)
        legend1
        
        webuse grunfeld, clear
        twoway scatter invest mvalue kstock if company==1, legend(`r(legend1)') saving(gr2, replace)
        
        gr combine gr1.gph gr2.gph
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	51.6 KB
ID:	1774721

        Comment


        • #5
          Dear Andrew Musau, thank you so much! I never heard serset. And I never thought that people have dug it long time ago: st: Re: How Do I Plot a Serset?

          Comment


          • #6
            I want to suppress plot2's default legend, and use the legend of plot1 instead
            Code:
            tw scatteri 1 1, leg(on title(A)) name(A) tit(A) nodraw  
            tw scatteri 2 2, leg(on title(B)) name(B) tit(B) nodraw
            
            * net describe grc1leg, from(http://www.stata.com/users/vwiggins)
            grc1leg A B, leg(A)  
            gr_edit .plotregion1.graph1.draw_view.setstyle, style(no)

            Comment


            • #7
              Thank you very much Bjarte Aagnes. I used grc1leg before posting this thread, however, it seems that grc1leg run unsuccessfully my particular case as a piece of code in a program.

              Comment

              Working...
              X