Announcement

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

  • How to overlay two combomarginsplot

    Hi, I need to overlay two different combomarginsplot but I am struggling to do so because this is not a twoway type graph. Is it possible to overlay it without the "twoway" command? Here is the combine of the two graphs that I want to overlay.

    Click image for larger version

Name:	teste.png
Views:	1
Size:	62.7 KB
ID:	1569115

    The comands I'm using are the following.

    Code:
    combomarginsplot "margins/iso_m0" "margins/iso_m1" "margins/iso_m2" "margins/iso_m3" "margins/iso_m4" ,  ///
        recast(line) recastci(rarea) plotopts(lcolor(ebblue*1.75)) ciopts(color(ebblue*.5%20)) ///
        legend(off)    label("Sem Política" "Dist. 1" "Dist. 2" "Dist. 3" "Dist. 4") ///
        ytitle("Isolamento previsto") xtitle("") ///
        ylab(.4(.05).5, gmin gmax labsize(small)) yscale(range(.4 .52)) graphr(color(white)) ///
        title("Todos os Municípios", size(medium)) name(iso_m, replace)
        
    combomarginsplot "margins/iso_c0" "margins/iso_c1" "margins/iso_c2" "margins/iso_c3" "margins/iso_c4", ///
        recast(line) recastci(rarea) plotopts(lcolor(red*1.75)) ciopts(color(red*.5%20)) legend(off) ///
        label("Sem Política" "Dist. 1" "Dist. 2" "Dist. 3" "Dist. 4") ///
        ytitle("Isolamento previsto") xtitle("") ///
        ylab(.4(.05).5, gmin gmax labsize(small)) yscale(range(.4 .52)) graphr(color(white)) ///
        title("Capitais", size(medium)) name(iso_c, replace)
        
    gr combine iso_m iso_c, rows(1) cols(1) graphr(color(white))
    Last edited by Angelo Marino; 19 Aug 2020, 09:17.

  • #2
    combomarginsplot is from SSC (FAQ Advice #12). Unless the command explicitly has an overlay option, you will need to reach to twoway to recreate and overlay the graphs. As advised in FAQ Advice # 12, a data example is required for anything concrete.

    Comment


    • #3
      The data that I am using is a time series of an isolation index for multiple municipalities in Brazil and also policies implemented by the states, represented by dummies "de_somenthing". Here's an example:

      Code:
          date    nomemun        iso     de_dt  de_ldp  de_ld  de_ldt
      13apr2020    ariquemes    .3980798    0    0    1    0
      14apr2020    ariquemes    .3840362    0    0    1    0
      15apr2020    ariquemes    .3981349    0    0    1    0
      16apr2020    ariquemes    .398827     0    0    1    0
      17apr2020    ariquemes    .3594378    0    0    1    0
      18apr2020    ariquemes    .4170891    0    0    1    0
      19apr2020    ariquemes    .5064935    0    0    1    0
      20apr2020    ariquemes    .3976436    0    0    1    0
      21apr2020    ariquemes    .4929993    0    0    1    0
      22apr2020    ariquemes    .384017     0    0    1    0
      23apr2020    ariquemes    .4032496    0    0    1    0
      24apr2020    ariquemes    .3859649    0    0    1    0
      I want to plot the margins for each scenario, I am using the following code to produce the margins files:

      Code:
          reghdfe iso de_* l1.obitos if week>=11 [aw=pop], a(i.date cidade) vce(cluster uf)
          margins, at(de_dt=0 de_ldp=0 de_ld=0 de_ldt=0) over(date) saving("margins/iso_m0", replace)
          margins, at(de_dt=1 de_ldp=0 de_ld=0 de_ldt=0) over(date) saving("margins/iso_m1", replace)
          margins, at(de_dt=0 de_ldp=1 de_ld=0 de_ldt=0) over(date) saving("margins/iso_m2", replace)
          margins, at(de_dt=0 de_ldp=0 de_ld=1 de_ldt=0) over(date) saving("margins/iso_m3", replace)
          margins, at(de_dt=0 de_ldp=0 de_ld=0 de_ldt=1) over(date) saving("margins/iso_m4", replace)
          
          reghdfe iso de_* l1.obitos if week>=11 & capital==1, a(i.date cidade) vce(cluster uf)
          margins, at(de_dt=0 de_ldp=0 de_ld=0 de_ldt=0) over(date) saving("margins/iso_c0", replace)
          margins, at(de_dt=1 de_ldp=0 de_ld=0 de_ldt=0) over(date) saving("margins/iso_c1", replace)
          margins, at(de_dt=0 de_ldp=1 de_ld=0 de_ldt=0) over(date) saving("margins/iso_c2", replace)
          margins, at(de_dt=0 de_ldp=0 de_ld=1 de_ldt=0) over(date) saving("margins/iso_c3", replace)
          margins, at(de_dt=0 de_ldp=0 de_ld=0 de_ldt=1) over(date) saving("margins/iso_c4", replace)
      I think I cannot construct those graphs with a twoway command.
      Last edited by Angelo Marino; 19 Aug 2020, 09:59.

      Comment


      • #4
        I think I cannot construct those graphs with a twoway command.
        I am sure you can. Can you upload iso_m.gph and iso_c.gph from #1 (in .gph) format here?

        Comment


        • #5
          Hi, Andrew. Thank you for your attention. Here are the two graphs in the .gph format.

          I am sorry if this is a simple issue, I am not used to this type of graphs and I am kind of new in Stata.

          Best regards.
          Attached Files

          Comment


          • #6
            Thanks. This is the data I was able to extract from the graphs and the combined graphs. The first set of code shows how to extract the data and the second how to combine the datasets and graph using twoway.

            Code:
            serset clear
            graph use "iso_c.gph" 
            serset 0
            serset use, clear
            dataex
            clear
            serset clear
            graph use "iso_m.gph" 
            serset 0
            serset use, clear
            dataex



            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(_ci_lb _ci_ub __000000 _margin)
            .4248816  .456547 1 .4407143
            .4314164 .4457927 2 .4386045
            .4464332 .4619349 3 .4541841
            .4581107 .4690964 4 .4636036
            .4931114 .5176205 5 .5053659
            end
            gen capitals=1
            tempfile capitals
            save `capitals'
            clear
            input float(_ci_lb _ci_ub __000000 _margin)
            .4289632 .4507284 1  .4398458
            .4169057 .4283024 2  .4226041
            .4299054 .4433759 3 .43664065
            .4309383 .4432397 4   .437089
            .4645864 .4892667 5  .4769265
            end
            gen capitals=0
            append using `capitals'
            rename __000000 id
            
            twoway  (line _margin id if !capitals, lcolor(blue) lwidth(medthick)) ///
            (rarea _ci_lb _ci_ub id if !capitals, color(blue%50)) ///
            (line _margin id if capitals, lcolor(red) lwidth(medthick)) ///
            (rarea _ci_lb _ci_ub id if capitals, color(red%50) scheme(s1color) ///
            xlab( 1 "Sem Política" 2 "Dist. 1"  3 "Dist. 2" 4 "Dist. 3" 5 "Dist. 4") ///
            xtitle("") ytitle("Isolamento previsto") ylab(0.4 (0.05) 0.5, grid) ///
            title(Todos os Municípios (azul); Capitais (vermelha)) leg(off) )
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	80.4 KB
ID:	1569175

            Comment


            • #7
              That's amazing, thank you so much Andrew. I will take a careful look at the code and try to understand everything, I wouldn't be able to do it by myself.

              Best,

              Angelo.

              Comment


              • #8
                I'm not totally clear what's going on here, but I have an updated version of combomarginsplot that allows you to save the combined margins file, and then use that in another call to combomarginsplot. I think that might do what you want. Here's an example using the auto dataset that should correspond to the important aspects of your problem:


                Code:
                sysuse auto, clear
                reg mpg price i.rep78
                forval i=1/5 {
                    margins, at(rep78=`i' price=1000) saving(r`i', replace)
                    margins, at(rep78=`i' price=3000) saving(s`i', replace)
                }
                combomarginsplot r1 r2 r3 r4 r5, filevarname(fnum) label(D1 D2 D3 D4 D5) savefile(rmargins, replace)
                combomarginsplot s1 s2 s3 s4 s5, filevarname(fnum) label(D1 D2 D3 D4 D5) savefile(smargins, replace)
                combomarginsplot rmargins smargins, label("R models" "S models") xtitle("Distribution")
                This produces the following:
                Click image for larger version

Name:	combocombomp.jpg
Views:	1
Size:	26.3 KB
ID:	1585732


                I have sent the updated version of the program to be posted on SSC. In the meantime you can install if from my website if you wish:

                Code:
                net from https://www.nicholasjgwinter.com/Stata
                net describe combomarginsplot

                Comment


                • #9
                  Quick update to say that the updated combomarginsplot is now available from SSC, so no need to get from my website.

                  Comment

                  Working...
                  X