Announcement

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

  • Overlay Keys in Legend

    I am graphing point estimates and confidence intervals for two different groups. In the legend, I would like to have the key for point estimate to be overlaid over the key for confidence intervals. But the key for point estimate is displayed next to the key for confidence interval. Is there any way to overlay the keys? Here is the code:

    Code:
    graph twoway (scatter beta_group1 run_group1, msize(vsmall) mfcolor(black) mlcolor(black) msymbol(diamond))  (rcap low_group1 up_group1 run_group1, lpattern(solid) lcolor(black)), || ///
                 (scatter beta_group2 run_group2, msize(vsmall) mfcolor(gray)  mlcolor(gray)  msymbol(triangle)) (rcap low_group2 up_group2 run_group2, lpattern(dash)  lcolor(gray))
    Last edited by Trevor Andrew; 16 Dec 2021, 18:19.

  • #2
    Scott Merryman and Sergiy Radyakin do you have any suggestions since you helped on a similar problem in the past? Below is the graph. I would like to combine the legend keys for group 1 and group 2.


    Click image for larger version

Name:	Graph.png
Views:	1
Size:	47.2 KB
ID:	1641405

    Last edited by Trevor Andrew; 17 Dec 2021, 13:30.

    Comment


    • #3
      Your problem is not difficult. However, to increase your chances of obtaining a helpful reply, provide a reproducible example following FAQ Advice #12.

      Comment


      • #4
        I would like to combine the two legend keys of price together and two legend keys of weight together. Here is a reproducible example:

        Code:
        sysuse auto.dta, clear
        
        gen price_1=(price>3500)
        gen price_2=(price>4000)
        gen price_3=(price>5000)
        gen price_4=(price>6000)
        gen price_5=(price>7000)
            
        gen weight_1=(weight>2000)
        gen weight_2=(weight>2500)
        gen weight_3=(weight>3000)
        gen weight_4=(weight>3500)
        gen weight_5=(weight>4000)
        
        local i=1
        reg     price_`i' mpg
        gen     price_b=r(table)[1,1] in `i'
        gen     price_l=r(table)[5,1] in `i'
        gen     price_u=r(table)[6,1] in `i'   
        forvalues i=2(1)5 {
        reg     price_`i' mpg
        replace price_b=r(table)[1,1] in `i'
        replace price_l=r(table)[5,1] in `i'
        replace price_u=r(table)[6,1] in `i'
        }
        
        local i=1
        reg     weight_`i' mpg
        gen     weight_b=r(table)[1,1] in `i'
        gen     weight_l=r(table)[5,1] in `i'
        gen     weight_u=r(table)[6,1] in `i'   
        forvalues i=2(1)5 {
        reg     weight_`i' mpg
        replace weight_b=r(table)[1,1] in `i'
        replace weight_l=r(table)[5,1] in `i'
        replace weight_u=r(table)[6,1] in `i'
        }
        
        local j=1
        gen     xvar=0   in `j'    
        local j=2    
        forvalues i=10(10)40 {
        replace xvar=`i' in `j'
        local ++j
        }    
        format xvar %9.0fc
        
        gen xvar1=xvar-2
        gen xvar2=xvar+2
        
        label var price_b "Price"
        label var price_l "Price"
        label var price_u "Price"
        
        label var weight_b "Weight"
        label var weight_l "Weight"
        label var weight_u "Weight"
            
            
        graph twoway (scatter price_b  xvar1, msize(vsmall) mfcolor(black) mlcolor(black) msymbol(diamond))  (rcap price_l  price_u  xvar1, lpattern(solid) lcolor(black)), || ///
                     (scatter weight_b xvar2, msize(vsmall) mfcolor(gray)  mlcolor(gray)  msymbol(triangle)) (rcap weight_l weight_u xvar2, lpattern(dash)  lcolor(gray))
        Andrew Musau I thought the code was enough. But you're right a reproducible example is even better.

        Comment


        • #5
          Here is one way. Note plots 5 and 6 do not exist (as "weight" is never negative) and are used just for the legend.

          Code:
          sysuse auto.dta, clear
          
          gen price_1=(price>3500)
          gen price_2=(price>4000)
          gen price_3=(price>5000)
          gen price_4=(price>6000)
          gen price_5=(price>7000)
              
          gen weight_1=(weight>2000)
          gen weight_2=(weight>2500)
          gen weight_3=(weight>3000)
          gen weight_4=(weight>3500)
          gen weight_5=(weight>4000)
          
          local i=1
          reg     price_`i' mpg
          gen     price_b=r(table)[1,1] in `i'
          gen     price_l=r(table)[5,1] in `i'
          gen     price_u=r(table)[6,1] in `i'   
          forvalues i=2(1)5 {
          reg     price_`i' mpg
          replace price_b=r(table)[1,1] in `i'
          replace price_l=r(table)[5,1] in `i'
          replace price_u=r(table)[6,1] in `i'
          }
          
          local i=1
          reg     weight_`i' mpg
          gen     weight_b=r(table)[1,1] in `i'
          gen     weight_l=r(table)[5,1] in `i'
          gen     weight_u=r(table)[6,1] in `i'   
          forvalues i=2(1)5 {
          reg     weight_`i' mpg
          replace weight_b=r(table)[1,1] in `i'
          replace weight_l=r(table)[5,1] in `i'
          replace weight_u=r(table)[6,1] in `i'
          }
          
          local j=1
          gen     xvar=0   in `j'    
          local j=2    
          forvalues i=10(10)40 {
          replace xvar=`i' in `j'
          local ++j
          }    
          format xvar %9.0fc
          
          gen xvar1=xvar-2
          gen xvar2=xvar+2
          
          label var price_b "Price"
          label var price_l "Price"
          label var price_u "Price"
          
          label var weight_b "Weight"
          label var weight_l "Weight"
          label var weight_u "Weight"
              
              
          graph twoway (scatter price_b  xvar1, msize(vsmall) mfcolor(black) mlcolor(black) msymbol(diamond))  (rcap price_l  price_u  xvar1, lpattern(solid) lcolor(black)), || ///
                       (scatter weight_b xvar2, msize(vsmall) mfcolor(gray)  mlcolor(gray)  msymbol(triangle)) (rcap weight_l weight_u xvar2, lpattern(dash)  lcolor(gray))|| /// 
                       (rconnected price_b price_u  xvar1 if weight<-10, msize(vsmall) mfcolor(black) mlcolor(black) msymbol(diamond)) || ///
                      (rconnected price_b price_u  xvar1 if weight<-10, msize(vsmall) mfcolor(gray)  mlcolor(gray)  msymbol(triangle) lpattern(dash)  lcolor(gray)), leg(order(5 6 "Weight"))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.0 KB
ID:	1641430

          Comment


          • #6
            This is great! Just one minor thing. Is it possible to have the spikes from rcap in the legend as well?

            Comment


            • #7
              There is no such plot type that will generate the symbol with caps. But it is not difficult to create the legend within your plot region using twoway scatteri.

              Code:
              sysuse auto.dta, clear
              
              gen price_1=(price>3500)
              gen price_2=(price>4000)
              gen price_3=(price>5000)
              gen price_4=(price>6000)
              gen price_5=(price>7000)
                  
              gen weight_1=(weight>2000)
              gen weight_2=(weight>2500)
              gen weight_3=(weight>3000)
              gen weight_4=(weight>3500)
              gen weight_5=(weight>4000)
              
              local i=1
              reg     price_`i' mpg
              gen     price_b=r(table)[1,1] in `i'
              gen     price_l=r(table)[5,1] in `i'
              gen     price_u=r(table)[6,1] in `i'   
              forvalues i=2(1)5 {
              reg     price_`i' mpg
              replace price_b=r(table)[1,1] in `i'
              replace price_l=r(table)[5,1] in `i'
              replace price_u=r(table)[6,1] in `i'
              }
              
              local i=1
              reg     weight_`i' mpg
              gen     weight_b=r(table)[1,1] in `i'
              gen     weight_l=r(table)[5,1] in `i'
              gen     weight_u=r(table)[6,1] in `i'   
              forvalues i=2(1)5 {
              reg     weight_`i' mpg
              replace weight_b=r(table)[1,1] in `i'
              replace weight_l=r(table)[5,1] in `i'
              replace weight_u=r(table)[6,1] in `i'
              }
              
              local j=1
              gen     xvar=0   in `j'    
              local j=2    
              forvalues i=10(10)40 {
              replace xvar=`i' in `j'
              local ++j
              }    
              format xvar %9.0fc
              
              gen xvar1=xvar-2
              gen xvar2=xvar+2
              
              label var price_b "Price"
              label var price_l "Price"
              label var price_u "Price"
              
              label var weight_b "Weight"
              label var weight_l "Weight"
              label var weight_u "Weight"
                  
                  
              graph twoway (scatter price_b  xvar1, msize(vsmall) mfcolor(black) mlcolor(black) msymbol(diamond))  (rcap price_l  price_u  xvar1, lpattern(solid) lcolor(black)), || ///
                          (scatter weight_b xvar2, msize(vsmall) mfcolor(gray)  mlcolor(gray)  msymbol(triangle)) (rcap weight_l weight_u xvar2, lpattern(dash)  lcolor(gray))|| ///
                          (scatteri 0.01 20 0.01 25 "Price", msy(pipe) connect(line)  mc(black) lc(black) mlabsize(medium) mlabcolor(black)) || ///
                          (scatteri 0.01 22.5, msy(diamond)  msize(vsmall) mc(black)) || ///
                          (scatteri 0.01 32.5, msy(triangle)  msize(vsmall) mc(gray)) || ///
                          (scatteri 0.01 30 0.01 35 "Weight", msy(pipe) connect(line) lp(dash) mc(gray) lc(gray) mlabsize(medium) mlabcolor(black)) || ///
                          , ylab(-0.08 (0.02) 0) leg(off)
              Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.9 KB
ID:	1641466

              Comment

              Working...
              X