Announcement

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

  • Trying to combine several graphs

    Dear Statalist,

    I want to combine in a single graph (image) several marginsplot done through contours plot using Stata 15. My problem is with the zlegend. After some search I have managed to erase it from each graph, but I would need the zlegend in the combined graph at position 6. Is there any easy way to do this?
    This is the command for each graph
    Code:
    use predictions1, clear
    rename _margin pr_innprod
    twoway  (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )),  ///             
              xlabel(-6.16 (0.323) -1.32, labsize(vsmall))       ///                                        
              ylabel(0(0.1)1, angle(horizontal))       ///
        zlabel(0.3 (0.03) 1, angle(horizontal) labsize(vsmall))   ///
              xtitle("offshoring sectorial (% offshorer by sector)", size(small))                      ///                     
              ytitle("Internal R&D (as % of sales)", size(small))                      ///                     
              ztitle("Predicted probability of product innovation", size(small))         ///        
              title("Predicted probability of product innovation by internal R&D and sectoral offshoring", size(small))
    graph save Graph "D:\damia\Dropbox\PhD\3er year\Tables\Paper\Principal\offshoring.gph", replace
    Then I combine
    Code:
    graph combine "offshoring.gph" "collaboration.gph"
    This is what I got
    Click image for larger version

Name:	Combined_off_coll.png
Views:	1
Size:	54.3 KB
ID:	1489755

    As you can see, zlegend is taking a lot of space from each graph, is there any way to put just a single zlegend in the combined graph at the botton (position 6)?

    Thanks in advance, any help will be much appreciated.

  • #2
    Rather than getting different graphs on one image using graph combine, you can do so using the by() option. There is an example starting here: http://maartenbuis.nl/workshops/grap...l#slide58.smcl
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten, thanks for the material you refer (it seems really good, especially for people with difficulties with Stata like me). Please, correct me if I am wrong, but in my case I am deriving each contour from different regressions (implying different interactions, but separately for each regression). In the example you refer I see that the two interactions comes from the same regression. In my case I have:
      Code:
      melogit innprod L.offshoring L.coopera L.permanent L.offshoring##L.c.Sec_offshoring##L.c.Sec_offshoring ||sector: ||firms:    
      margins, at(L.offshoring=(0(0.2)1) L.Sec_offshoring=(-6.16 (0.323) -1.32))  saving(predictions1, replace)
      
      melogit innprod L.offshoring L.coopera L.permanent L.c.coopera##L.c.Sec_offshoring##L.c.Sec_offshoring ||sector: ||firms:    
      margins, at(L.coopera=(0(0.2)1) L.Sec_offshoring=(-6.16 (0.323) -1.32))  saving(predictions2, replace)
      The model is bigger (include more variables and random coefficients) but for let you know I thing this is enough, just to show that I am doing different models. I do not see how to combine the graphs using "by".
      Is there somethingI am not seing?

      Comment


      • #4
        I think that if you need specific advice, you need to create a minimal reproducible example (which would include some data). That, or live in hope that someone will figure it out and post a solution.

        Comment


        • #5
          Dear Andrew, thanks for the advice, in fact I totally agree with you; but the dataset is huge (more than 100.000 obs) and have been trying with just 100-200 obs and even the very simple models in #3 do not converge with such small amount of obs. So it is pointless to put an example of the dataset I think.
          Maybe the problem is an option for the zlabel in the way of doing those graph in #1 that I do not know, that allow me to put it at the bottom of the graphs when combined.

          Comment


          • #6
            I think what you need is the margins output from both regressions. That is what you use for plotting.

            Comment


            • #7
              Dear Andrew and Maarten,

              This is what I have accomplished until now (not too much to be honest). First, I have tried to do each contour without the zlegend and to combine them.
              Code:
              cd "C:\Users\dtojeiro\Dropbox\PhD\3er year\Tables\Paper\Principal\Contour_plots"
              use predictions1, clear
              rename _margin pr_innprod
              twoway  (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )),  ///              
                        xlabel(-6.16 (0.323) -1.63, labsize(vsmall))       ///                                         
                        ylabel(0(0.1)1, angle(horizontal))       ///
                        aspect(1) plotregion(margin(zero))     ///
                        zlabel(0.3 (0.03) 1, angle(horizontal) labsize(vsmall))  clegend(off) ///
                        xtitle("offshoring sectorial (% offshorer by sector)", size(small))    ///                      
                        ytitle("Internal R&D (as % of sales)", size(small))                      ///                      
                        ztitle("Predicted probability of product innovation", size(small))         ///         
                        title("Panel B", size(small))  
                        graph save Graph "C:\Users\dtojeiro\Dropbox\PhD\3er year\Tables\Paper\Principal\Contour_plots\off.gph", replace
              
                        
              use predictions2, clear
              rename _margin pr_innprod
              twoway  (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )),  ///              
                        xlabel(-6.16 (0.323) -1.63, labsize(vsmall))       ///                                         
                        ylabel(0(0.1)1, angle(horizontal))       ///
                        zlabel(0.3 (0.03) 1, angle(horizontal) labsize(vsmall)) clegend(off)  ///
                        aspect(1) plotregion(margin(zero))     ///
                        xtitle("offshoring sectorial (% offshorer by sector)", size(small))                      ///                      
                        ytitle("Collaboration (as % of sales)", size(small))                      ///                      
                        ztitle("Predicted probability of product innovation", size(small))         ///         
                        title("Panel B", size(small))
                        graph save Graph "C:\Users\dtojeiro\Dropbox\PhD\3er year\Tables\Paper\Principal\Contour_plots\coll.gph", replace
              
              
              use predictions3, clear
              rename _margin pr_innprod
              twoway  (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )),  ///              
                        xlabel(-6.16(0.323)-1.63, labsize(vsmall))       ///                                         
                        ylabel(0(0.1)1,  angle(horizontal))       ///  
                        zlabel(0.3 (0.03) 1, angle(horizontal) labsize(vsmall)) clegend(off)  ///
                        aspect(1) plotregion(margin(zero))     ///
                        xtitle("Sec. Offshoring (% offshorer by sector)", size(small))                      ///                      
                        ytitle("Permanent experience (firm level)", size(small))                      ///                      
                        ztitle("Predicted probability of product innovation", size(small))  ///
                        title("Panel C", size(small))
              graph save Graph "C:\Users\dtojeiro\Dropbox\PhD\3er year\Tables\Paper\Principal\Contour_plots\permanent.gph", replace
                        
                        
              use predictions4, clear
              rename _margin pr_innprod
              twoway  (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )),  ///              
                        xlabel(-6.16(0.323)-1.63, labsize(vsmall))       ///                                         
                        ylabel(0(0.1)1,  angle(horizontal))       ///  
                        zlabel(0.3 (0.03) 1, angle(horizontal) labsize(vsmall)) clegend(off)  ///
                        aspect(1) plotregion(margin(zero))     ///
                        xtitle("Sec. Offshoring (% offshorer by sector)", size(small))                      ///                      
                        ytitle("Internal R&D (firm level)", size(small))                      ///                      
                        ztitle("Predicted probability of product innovation", size(small))   ///
                        title("Panel B", size(small))
              graph save Graph "C:\Users\dtojeiro\Dropbox\PhD\3er year\Tables\Paper\Principal\Contour_plots\internal.gph", replace
                        
                        
              graph combine "off.gph" "coll.gph" "permanent" "internal",  saving(combined, replace)
              Click image for larger version

Name:	combined.png
Views:	1
Size:	52.7 KB
ID:	1490015

              Thereafter, I have tried to created another graph but this time only with zlegend (in order to combine it with the others) following one of the examples Maarten recommended me.
              Code:
              clear
              use predictions2, clear
              rename _margin pr_innprod
              twoway  (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )),  ///
                        yscale(off) xscale(off)    ///             
                        zlabel(0.3 (0.03) 1, angle(horizontal) labsize(vsmall) )   ///
                        aspect(1) plotregion(margin(zero))     ///
                        ztitle("Predicted probability of product innovation", size(small) )  ///              
                        name(legend, replace)
              
              _gm_edit .legend.plotregion1.draw_view.set_false
              graph display
              
              _gm_edit .legend.xstretch.set fixed
              graph display
              However,
              1) even though the graph with only the legend is created (as you can see)
              Click image for larger version

Name:	legend.png
Views:	1
Size:	18.0 KB
ID:	1490016

              , when I try to save it, it does not save (the graph is empty)
              Code:
               graph use "C:\Users\dtojeiro\Dropbox\PhD\3er year\Tables\Paper\Principal\Contour_plots\legend.gph"
              r(4023);
              2) Therefore, when I combine it with the other 4 graphs, it do no appear and giver error r(4023);
              3) I still do not see how to put the legend in at the bottom (at position 6); and I have been trying with pos(6) option but it give error: option pos() not allowed r(198);

              Please, can anyone point me in the right direction?

              Comment


              • #8
                Code:
                use predictions1, clear
                keep in 1/10
                dataex
                use predictions2, clear
                keep in 1/10
                dataex
                & copy and paste the output of dataex in the 2 cases above.

                Comment


                • #9
                  Here you have first prediction1 and then prediction2:
                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input float _margin double(_at18 _at19)
                  .6287891 0               -6.16
                  .6733495 0              -5.837
                   .708531 0  -5.513999999999999
                  .7351892 0  -5.190999999999999
                  .7541969 0 -4.8679999999999986
                  .7662863 0  -4.544999999999998
                  .7719687 0  -4.221999999999998
                  .7714965 0  -3.898999999999998
                  .7648484 0  -3.575999999999998
                  .7517307 0  -3.252999999999998
                  end
                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input float _margin double(_at18 _at19)
                  .51295537 0               -6.16
                   .5857614 0              -5.837
                    .644515 0  -5.513999999999999
                   .6894835 0  -5.190999999999999
                   .7219275 0 -4.8679999999999986
                   .7432487 0  -4.544999999999998
                   .7545695 0  -4.221999999999998
                   .7565472 0  -3.898999999999998
                   .7493008 0  -3.575999999999998
                   .7324002 0  -3.252999999999998
                  end

                  Comment


                  • #10
                    Thanks for the data example. I do not include options in the twoway command, but hopefully this helps.

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float _margin double(_at18 _at19)
                    .6287891 0               -6.16
                    .6733495 0              -5.837
                     .708531 0  -5.513999999999999
                    .7351892 0  -5.190999999999999
                    .7541969 0 -4.8679999999999986
                    .7662863 0  -4.544999999999998
                    .7719687 0  -4.221999999999998
                    .7714965 0  -3.898999999999998
                    .7648484 0  -3.575999999999998
                    .7517307 0  -3.252999999999998
                    end
                    
                    gen group=1
                    rename _margin pr_innprod
                    tempfile first
                    save `first'
                    
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float _margin double(_at18 _at19)
                    .51295537 0               -6.16
                     .5857614 0              -5.837
                      .644515 0  -5.513999999999999
                     .6894835 0  -5.190999999999999
                     .7219275 0 -4.8679999999999986
                     .7432487 0  -4.544999999999998
                     .7545695 0  -4.221999999999998
                     .7565472 0  -3.898999999999998
                     .7493008 0  -3.575999999999998
                     .7324002 0  -3.252999999999998
                    end
                    
                    gen group=2
                    rename _margin pr_innprod
                    tempfile second
                    save `second'
                    
                    clear
                    use `first'
                    append using `second'
                    save `first', replace
                    
                    twoway  (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )), by(group, clegend(on))

                    Comment


                    • #11
                      Dear Andrew, you just saved my life. It was so simple. Now I have to change that Boffshoring since not all the vertical axis are that variable. Here is what I've got
                      Click image for larger version

Name:	new_combined.png
Views:	1
Size:	40.2 KB
ID:	1490050

                      without bothering you too much, do you know how to put that legend in position 6?

                      I am very grateful, thanks a lot.

                      Comment


                      • #12
                        twoway (contour pr_innprod _at18 _at19, ccuts(0(0.05)1.0 )), by(group, clegend(on position(6)))
                        Last edited by Ben A. Dwamena; 25 Mar 2019, 12:55.

                        Comment


                        • #13
                          Dear Ben, thanks for the help. It worked, even though it keeps the vertical structure.

                          Comment


                          • #14
                            Now I have to change that Boffshoring since not all the vertical axis are that variable.
                            You can remove the axes titles using the options

                            Code:
                            xtitle("") ytitle("") ztitle("")
                            or add your own

                            Code:
                            xtitle("My Xtitle") ytitle("My Ytitle") ztitle("My Ztitle")
                            Because you have several groups, it is better to label the groups beforehand to identify them. Your labels will apper in place of 1, 2, 3, and 4 above. After appending the data,

                            Code:
                            label define group 1 "My Group 1" 2 "My Group 2" 3 "My Group 3" 4 "My Group 4"
                            label values group group
                            <graph command here>
                            I don't know of how you can get a horizontal clegend, perhaps it's not possible. If it is a matter of space, reduce the width. The procedure involves specifying two clegends, one outside the by() option and one inside. Choose values (1, 2,...). Finally, you can remove the text "graphs by ..." using note("") within the by() option.

                            Code:
                            twoway (contour ..., by(group,  clegend(on) note("") ) clegend(off width(2) )
                            Last edited by Andrew Musau; 25 Mar 2019, 14:47.

                            Comment


                            • #15
                              Dear Andrew, I have another question (sorry for asking too much). Not all the variables in the interactions have the same scale and of course the graph uses a single vertical scale. I have tried with the option "graph editor" in the graph window but it only changes the number on the scale, not the blank space in offshoring for example. Is there any way of account for this in the graph?
                              Click image for larger version

Name:	new_combined.png
Views:	1
Size:	37.1 KB
ID:	1490151

                              Comment

                              Working...
                              X