Search code examples
graphstata

Combining quantile regression plots


I produce multiple quantile regression plots in Stata, which I then want to combine with the community-contributed command grc1leg. This command combines graphs using a single common legend compared to the built-in command graph combine.

However, when I run my code I always run into the following error:

Graph.graphs[1].legend.draw_view.set_false: class type not found
r(4018);

This does not happen if I combine other graphs with grc1leg.

Below is a reproducible example:

*load data
sysuse auto, clear
*Qreg 1
qreg price weight length foreign, quantile (0.5) 
grqreg foreign,  ci ols olsci graphregion(color(white)) 
graph save "H:\graph1.gph", replace
*Qreg 2
qreg price weight length foreign, quantile (0.5) 
grqreg foreign,  ci ols olsci graphregion(color(white)) 
graph save "H:\graph2.gph", replace

/* Combining graphs */
*grc1leg is a user written command that needs to be installed first
net install grc1leg, replace

cd H:\
graph combine graph1.gph graph2.gph 
grc1leg graph1.gph graph2.gph 

Note that grqreg in this example is also a community-contributed command.

Any suggestions on how can I solve this problem?

I read in previous posts that grc1leg is upset if the user used the graph editor in between or if the word key appeared in the graph. Neither is the case here.


Solution

  • The command grc1leg does not work here because grqreg already combines graphs internally. As such, the required graph class types are destroyed.

    The only way to force the former to work, is to alter the following lines in the source code of the latter:

    *-> combine all graphs;
    
    if ("`nodraw'"=="nodraw") {;
        grc1leg `graphlist', nodraw `options';
     // graph combine `graphlist', nodraw `options';
    };
    
    if ("`nodraw'"=="") {;
        grc1leg `graphlist', `options';
     // graph combine `graphlist', `options';
    };
    

    In other words, you need to use grc1leg to combine the internally-produced individual graphs.

    Once this is done and you reload grqreg the following will work as expected:

    sysuse auto, clear
    
    qreg price weight length foreign, quantile (0.5) 
    grqreg foreign, ci ols olsci graphregion(color(white)) name(g1, replace) 
    
    qreg price weight length foreign, quantile (0.5) 
    grqreg foreign, ci ols olsci graphregion(color(white)) name(g2, replace) 
    
    grc1leg g1 g2, name(g3, replace)
    

    enter image description here