Announcement

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

  • grc1leg2 -- legend

    Hello,
    I have two different variables. I plotted the mx and min values for each variable and wanted to show the two graphs together


    Code:
    twoway rcap max_X1 min_X1  n,  name(g1, replace)
    
    twoway rcap max_X2 min_X2  n,  name(g1, replace)
    
    grc1leg2 g1 g2

    I tried different ways to show the names of vars within the same legend with no success.
    Sorry I cannot share the data but here is the graph I had
    using
    Code:
    graph combine g1 g2, name(combined, replace)

    Click image for larger version

Name:	combined.jpg
Views:	1
Size:	30.3 KB
ID:	1766963

  • #2
    Is this something you intend to display?

    Code:
    sysuse sp500, clear
    
    generate month = month(date)
    
    sort month
    
    by month: egen lo = min(volume)
    
    by month: egen hi = max(volume)
    
    format lo hi %10.0gc
    
    summarize volume
    
    by month: keep if _n==_N
    
    
    
    
    graph twoway (rcap lo hi month) (rcap low high month,  legend(order(1 "X1" 2 "X2") on pos(6))), name(g0, replace)
    
    graph twoway (rcap low high month,  legend(order(1 "X1") on pos(6))), name(g1, replace)
    
    graph twoway (rcap low high month,  legend(order(1 "X2" ) on pos(6))), name(g2, replace)
    
    grc1leg2 g1 g2 g0, legendfrom(g0) hidelegendfrom  name(combined, replace)



    Click image for larger version

Name:	combined.png
Views:	1
Size:	29.6 KB
ID:	1766971

    Comment


    • #3
      It's common that people can't share data -- so common that we advise explicitly (FAQ Advice #12)

      If your dataset is confidential, then provide a fake example instead.
      What your two graphs share seems to include

      * a horizontal variable integers 1 to 14

      * ranges on two variables

      The focus on grc1leg2 seems a red herring here, as you can just aim from the outset to put everything in one plot.
      Code:
      clear
      
      set obs 14
      
      set seed 314159
      
      gen whatever = _n
      
      gen x1_min = 0.1 * runiform()
      
      gen x1_max = x1_min + runiform()
      
      gen x2_min = 0.2 * runiform()
      
      gen x2_max = x2_min + runiform()
      
      gen show1 = whatever - 0.1
      gen show2 = whatever + 0.1
      
      twoway rcap x1_* show1 || rcap x2_* show2 , xla(1/14) ///
      legend(order(1 "x1" 2 "x2") row(1) pos(12)) ytitle(Something informative here) ///
      xtitle(Whatever this is)
      Click image for larger version

Name:	ind14.png
Views:	1
Size:	53.0 KB
ID:	1766978



      I am going to guess wildly that these are 14 industries, and if so, or even if not so, don't stick to the order 1/14 if there is a better one. Also, the 14 categories presumably deserve detailed labels, which would imply swapping axes.
      Last edited by Nick Cox; 04 Nov 2024, 11:59.

      Comment


      • #4
        I found typos (graphed same variable twice). Here's the corrected answer, visually distinguishable.

        Code:
        sysuse sp500, clear
        
        generate month = month(date)
        
        sort month
        
        by month: egen lo = min(volume)
        
        by month: egen hi = max(volume)
        
        format lo hi %10.0gc
        
        summarize volume
        
        by month: keep if _n==_N
        
        
        
        
        graph twoway     (rcap lo hi month, lc(black) lp(solid))    ///
                        (rcap low high month, lc(red) lp(dash) legend(order(1 "X1" 2 "X2") on pos(6))), name(g0, replace)
        
        graph twoway (rcap low high month, lc(black) lp(solid) legend(order(1 "X1") on pos(6))), name(g1, replace)
        
        graph twoway (rcap lo hi month, lc(red) lp(dash) legend(order(1 "X2" ) on pos(6))), name(g2, replace)
        
        
        
        grc1leg2 g1 g2 g0, legendfrom(g0) hidelegendfrom  name(combined, replace)

        Click image for larger version

Name:	combined.png
Views:	1
Size:	72.9 KB
ID:	1766980



        Comment


        • #5
          Seungmin Lee Thank you for your answer. It is really helpful.

          Comment


          • #6
            Nick Cox Thank you for your answer. This is really helpful. Plotting the two variables in the same graph is even better, I didn't know how to do it before.
            Sorry about not providing a fake data example. I will pay more attention to that in the future.

            Comment

            Working...
            X