Announcement

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

  • Completely custom legend in -grc1leg-

    Dear Statalisters,

    I am preparing several combined graphs for my clients (details are confidential) using the grc1leg user-made command, to have a single legend. See below a simple example from the auto dataset:

    Code:
    net install grc1leg, from(http://www.stata.com/users/vwiggins/)
    
    sysuse auto, clear
    tw (sc price rep78 if foreign), legend(order(1 "Price by rep78 if foreign")) name(g1, replace)
    tw (sc price displacement if !foreign, color(red)), legend(order(1 "Price by displacement if domestic")) name(g2, replace)
    
    grc1leg g1 g2, legendfrom(g1)
    and the output is in the first reply.

    The problem, as you can see from the resulting graph, is that it may happen that neither of the subgraphs contain all the categories (in this case, foreign and domestic) so, given that grc1leg forces me to choose between the legends of one of the subgraphs, the overall legend will necessarily be incomplete.

    Is there a way around this? I am aware of the existence of undocumented graph editing command such as gr_edit, but I wouldn't know how to use them to specify a new legend from scratch.

    Thank you in advance,

    Fede
    Last edited by Federico Bindi; 26 Jan 2023, 09:50.

  • #2
    output:
    Click image for larger version

Name:	banana.png
Views:	1
Size:	65.1 KB
ID:	1698916

    Comment


    • #3
      I use grc1leg2, a community-contributed extension to grc1leg from Mead Over for the following.
      Code:
      * net describe grc1leg2, from(http://digital.cgdev.org/doc/stata/MO/Misc)
      * net install  grc1leg2, from(http://digital.cgdev.org/doc/stata/MO/Misc)
      
      sysuse auto, clear
      tw (sc price rep78        if foreign,  color(blue)),      name(g1, replace)
      tw (sc price displacement if !foreign, color(red)),       name(g2, replace)
      tw (sc price price rep78,              color(blue red)),  name(gg, replace) ///
          legend(order(1 "Price by rep78 if foreign" 2 "Price by displacement if domestic"))
      
      grc1leg2 g1 g2 gg, legendfrom(gg) hidelegendfrom
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	41.0 KB
ID:	1698970

      Added in edit: the basic problem is that this task is using legends for something other than that for which they are designed - distinguishing between variables on an overlaid scatter plot, in this case. For this task I'd be more likely to place a title over or under each separate graph.
      Last edited by William Lisowski; 26 Jan 2023, 12:42.

      Comment


      • #4
        Thank you William, this is exactly what I was looking for! Have a nice day.

        Fede

        Comment


        • #5
          That may be exactly what you were looking for but an alternative is to use the by() option to do most of the work for you -- not graph combine followed by surgery.

          This alternative does require some surgery in advance and some fiddling with graph options. There is here no need for a legend and thus no cause to mess with a legend at all.

          The sermon in favour can be found at https://journals.sagepub.com/doi/pdf...36867X20976341

          Code:
          set scheme s1color
          
          sysuse auto, clear
          
          gen predictor = cond(foreign, displacement, rep78)
          separate price, by(foreign)
          label var price0 "Price (USD)"
          label var price1 "Price (USD)"
          
          gen where = foreign
          label def where 0 "Repair record" 1 "Displacement"
          label val where where
          scatter price? predictor, by(where, xrescale note("") legend(off)) subtitle(, pos(6) fcolor(none) nobox) xtitle("") mc(red blue) name(g0, replace)
          Click image for larger version

Name:	bettercombine.png
Views:	1
Size:	20.2 KB
ID:	1699119

          Comment

          Working...
          X