Announcement

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

  • #16
    It is very foolish to argue with the Dutch about Dutch, but https://en.wikipedia.org/wiki/Piet_Mondrian is behind what I have been accustomed to....

    Comment


    • #17
      Hello Everyone,

      I am back with a query/question and will appreciate if someone can help me out.

      So I am thinking of automating the entire graph making process where it takes data from online and updates it into the form of graph that was originally done. Essentially I am having trouble replicating dates on the X-Axis. The particular problem I am facing is that I am unable to make an X-Axis label (along with ticks on axis(1)) similar to that on the X-Axis above (axis(2)).

      It will be nice if someone can help me resolve this problem. The code section where I am trying to add axis is in red. Also, please let me know how I can improve my method of automating such process as I would love to hear from the experts!

      Thanks once again


      Code:
          local url = "https://raw.githubusercontent.com/TheEconomist/covid-19-excess-deaths-tracker/master/output-data/excess-deaths/"
          local filemonthly = "all_monthly_excess_deaths.csv"
          local fileweekly = "all_weekly_excess_deaths.csv"
          local fileqtrly = "all_quarterly_excess_deaths.csv"
      
      
          insheet using "`url'`filemonthly'", clear
          generate monthly = 1
          
          /* Install user written commands:
          ssc install heatplot, replace
          ssc install blindschemes, replace
          */
      
          * Keeping Selected regions from each country
          egen tag = tag( country region)
          bysort country: egen count = total(tag)
          generate ctr_reg = 1 if (country==region) & count>1
          drop if count>1 & ctr_reg==.
          drop country tag count ctr_reg
          
          
          * Saving todays date in local
          local date: di %tdM_D_CY daily("$S_DATE", "DMY")
          
      
      
          * Generate year month indicator
          encode region, gen(Region)
          bysort Region: generate yrmth = _n
          bysort Region: generate yrmth1 = _n-0.5
          
          #delimit ;
          label define yrmth 
                              1 "Jan 2020"        2 "Feb" 
                              3 "Mar"             4 "Apr" 
                              5 "May"             6 "Jun" 
                              7 "Jul"             8 "Aug" 
                              9 "Sep"             10 "Oct" 
                              11 "Nov"            12 "Dec" 
                              13 "Jan 2021"       14 "Feb" 
                              15 "Mar"            16 "Apr"
                              17 "May"            18 "Jun"
                              19 "Jul"            20 "Aug"
                              21 "Sep"            22 "Oct"
                              23 "Nov"            24 "Dec"
                              25 "Jan 2022"       26 "Feb"
                  , replace ;
          #delimit cr
      
          
          label values yrmth yrmth
      
          
          * Drawing the Visual
          replace excess_deaths_pct_change = round(excess_deaths_pct_change*100, 1)
          
          quietly: summarize yrmth
          local count = `r(max)' + 1
      
          #delimit ;
          heatplot excess_deaths_pct_change i.Region i.yrmth, 
          
                  ytitle("")                                                             
                  xtitle("") 
                  
                  legend(subtitle("{bf}Deviation from expected deaths, %                  ", 
                                      span 
                                      size(1.75)
                                  ) 
                          pos(1) 
                          ring(1) 
                          rows(1) 
                          keygap(0.5) 
                          colgap(0) 
                          size(1.5) 
                          symysize(0.85) 
                          symxsize(7) 
                          order(1 "" 2 "0" 3 "+25" 4 "+50" 5 "+100" 6 "+200") 
                          stack
                      ) 
                  
                  ylabel(, 
                          angle(horizontal) 
                          labgap(-145) 
                          labsize(2) 
                          noticks 
                          labcolor(gs5) 
                          nogrid
                      ) 
                  
                  yscale(
                          noline 
                          alt 
                          reverse
                      ) 
                      
                  xscale(extend)
                  
                  xlabel(, nolabels tlength(0) nogrid
                      )
                          
                  
                  graphregion(margin(22=2)) 
                  plotregion(margin(b=0 t=0)) 
                  
                  color("234 242 245" "254 239 216" "253 204 138" "252 140 89" "227 73 51" "179 0 1") 
                  cuts(@min 0 25 50 100 200 @max) 
                  
                  p(
                      lcolor(white) 
                      lwidth(0.1) 
                      lalign(center)
                  ) 
                  
                  addplot( 
                          scatter Region yrmth, 
                          color(white%0) 
                          xaxis(1) 
                          xtitle("", axis(1)) 
                          xlabel(
                                  1/`count', 
                                  valuelabels 
                                  labsize(1.75) 
                                  labcolor(gs5) 
                                  nogrid 
                                  axis(1)
                              )
                              
                          ||
                          
                          scatter Region yrmth, 
                          color(white%0)
                          xaxis(2)
                          xtitle("", axis(2)) 
                          xlabel(
                                  1/`count', 
                                  valuelabels 
                                  labsize(1.75) 
                                  labcolor(gs5) 
                                  nogrid 
                                  axis(2)
                              )
                      ) 
                      
                  title("{bf}Excess deaths by country or city", 
                          pos(11) 
                          size(2.25) 
                          margin(l=-20 b=-10 t=2)
                      ) 
                      
                  subtitle("Last updated on - `c(current_date)'", 
                          pos(11) 
                          size(2) 
                          margin(l=-20 b=-10 t=5)
                      ) 
                      
                  scheme(plotplain);
          #delimit cr

      Click image for larger version

Name:	Screenshot 2021-05-29 194828.png
Views:	1
Size:	50.0 KB
ID:	1612218

      * As you can see I am unable to get any labels on the bottom of the visual.

      Comment


      • #18
        You should remove

        xlabel(, nolabels tlength(0) nogrid
        )
        Otherwise, you cannot both ask for labels and no labels.

        Comment


        • #19
          Andrew Musau thank you for the reply unfortunately the problem is that I still am not able to replicate the exact label as on axis(2). I am not sure why but the axis always seems to be centered and I am not able to figure out how to automate this since I can not apply value labels to decimals (would be nice if this feature was added to Stata). The following is the resulting image
          Click image for larger version

Name:	Screenshot 2021-05-30 171617.png
Views:	1
Size:	53.1 KB
ID:	1612308

          Last edited by Fahad Mirza; 30 May 2021, 07:10.

          Comment


          • #20
            Code:
            local url = "https://raw.githubusercontent.com/TheEconomist/covid-19-excess-deaths-tracker/master/output-data/excess-deaths/"
                local filemonthly = "all_monthly_excess_deaths.csv"
                local fileweekly = "all_weekly_excess_deaths.csv"
                local fileqtrly = "all_quarterly_excess_deaths.csv"
            
            
                insheet using "`url'`filemonthly'", clear
                generate monthly = 1
                
                /* Install user written commands:
                ssc install heatplot, replace
                ssc install blindschemes, replace
                */
            
                * Keeping Selected regions from each country
                egen tag = tag( country region)
                bysort country: egen count = total(tag)
                generate ctr_reg = 1 if (country==region) & count>1
                drop if count>1 & ctr_reg==.
                drop country tag count ctr_reg
                
                
                * Saving todays date in local
                local date: di %tdM_D_CY daily("$S_DATE", "DMY")
                
            
            
                * Generate year month indicator
                encode region, gen(Region)
                bysort Region: generate yrmth = _n
                
                
                #delimit ;
                label define yrmth 
                                    1 "Jan 2020"        2 "Feb" 
                                    3 "Mar"             4 "Apr" 
                                    5 "May"             6 "Jun" 
                                    7 "Jul"             8 "Aug" 
                                    9 "Sep"             10 "Oct" 
                                    11 "Nov"            12 "Dec" 
                                    13 "Jan 2021"       14 "Feb" 
                                    15 "Mar"            16 "Apr"
                                    17 "May"            18 "Jun"
                                    19 "Jul"            20 "Aug"
                                    21 "Sep"            22 "Oct"
                                    23 "Nov"            24 "Dec"
                                    25 "Jan 2022"       26 "Feb"
                        , replace ;
                #delimit cr
            
                
                label values yrmth yrmth
            
                
                * Drawing the Visual
                replace excess_deaths_pct_change = round(excess_deaths_pct_change*100, 1)
                
                quietly: summarize yrmth
                local count = `r(max)' + 1
                local xlab
                forval i=1/`count'{
                    local xlab "`xlab' `=`i'-0.5' `" "`:lab (yrmth) `i''" "'"
                } 
                #delimit ;
                heatplot excess_deaths_pct_change i.Region i.yrmth, 
                
                        ytitle("")                                                             
                        xtitle("") 
                        
                        legend(subtitle("{bf}Deviation from expected deaths, %                  ", 
                                            span 
                                            size(1.75)
                                        ) 
                                pos(1) 
                                ring(1) 
                                rows(1) 
                                keygap(0.5) 
                                colgap(0) 
                                size(1.5) 
                                symysize(0.85) 
                                symxsize(7) 
                                order(1 "" 2 "0" 3 "+25" 4 "+50" 5 "+100" 6 "+200") 
                                stack
                            ) 
                        
                        ylabel(, 
                                angle(horizontal) 
                                labgap(-145) 
                                labsize(2) 
                                noticks 
                                labcolor(gs5) 
                                nogrid
                            ) 
                        
                        yscale(
                                noline 
                                alt 
                                reverse
                            ) 
                            
                        xscale(extend)
                        xlab(`xlab', labsize(1.75) labcolor(gs5))
                                
                        graphregion(margin(22=2)) 
                        plotregion(margin(b=0 t=0)) 
                        
                        color("234 242 245" "254 239 216" "253 204 138" "252 140 89" "227 73 51" "179 0 1") 
                        cuts(@min 0 25 50 100 200 @max) 
                        
                        p(
                            lcolor(white) 
                            lwidth(0.1) 
                            lalign(center)
                        ) 
                                
                                addplot(scatter Region yrmth, 
                                color(white%0)
                                xaxis(2)
                                xtitle("", axis(2)) 
                                xlabel(
                                        1/`count', 
                                        valuelabels 
                                        labsize(1.75) 
                                        labcolor(gs5) 
                                        nogrid 
                                        axis(2)
                                    )
                            ) 
                            
                        title("{bf}Excess deaths by country or city", 
                                pos(11) 
                                size(2.25) 
                                margin(l=-20 b=-10 t=2)
                            ) 
                            
                        subtitle("Last updated on - `c(current_date)'", 
                                pos(11) 
                                size(2) 
                                margin(l=-20 b=-10 t=5)
                            ) 
                            
                        scheme(plotplain);
                #delimit cr

            Comment


            • #21
              This is brilliant! I think i need to really learn Stata and looking at the forum it sometimes (well almost always) feels like I know nothing. I will really appreciate if you can help me understand this line of code better because I am really not familiar with the entire syntax

              Code:
              
                      local xlab "`xlab' `=`i'-0.5' `" "`:lab (yrmth) `i''" "'"

              Comment


              • #22
                I first define an empty local "xlab"

                local xlab
                Then I loop, manually defining the labels. All you need is to offset the values by 0.5, so in the first iteration of

                local xlab "`xlab' `=`i'-0.5' `" "`:lab (yrmth) `i''" "'" }
                you have

                Code:
                xlab " 0.5 "Jan 2020" "
                since 1-0.5= 0.5 and the value label "`:lab (yrmth) 1'" is January 2020 as you have previously defined.

                The next iteration will take the local as is and add the value and label corresponding to two, so that is why the local is defined as

                local xlab `xlab' + something else
                The something else is 2-0.5 = 1.5 and the value label "`:lab (yrmth) 2'" is Feb. So this becomes

                Code:
                xlab " 0.5 "Jan 2020" 1.5 "Feb" "
                and so on. As I advised previously, also look at mylabels from SSC that can create the entire label for you.


                Comment


                • #23
                  How is the health of friends. One question please: How to change the color on the x, y axes

                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input long cdpto int año double fgt float(war line004 line008)
                  1 2010 50.45358460905211 . 20 30
                  1 2011 44.56099581988956 . 20 30
                  1 2012 44.47837710187593 . 20 30
                  1 2013 47.31285045180137 . 20 30
                  1 2014 51.30921677752137 . 20 30
                  1 2015 42.28657959492714 . 20 30
                  1 2016 39.70988726634459 30 20 30
                  1 2017 34.880030642116495 30 20 30
                  1 2018 33.512735579668714 30 20 30
                  1 2019 30.489214000418517 30 20 30
                  1 2020 36.11814774703212 30 . .
                  2 2010 27.077980420718667 . 20 30
                  2 2011 27.208232751348955 . 20 30
                  2 2012 27.43309610380656 . 20 30
                  2 2013 23.50490002935602 . 20 30
                  2 2014 23.943306383398966 . 20 30
                  2 2015 24.045336829414683 . 20 30
                  2 2016 21.678544672839646 30 20 30
                  2 2017 22.35510931322693 30 20 30
                  2 2018 20.314534403524014 30 20 30
                  2 2019 17.542837190216733 30 20 30
                  2 2020 29.828357804443456 30 . .
                  3 2010 62.007047142956374 . 20 30
                  3 2011 56.97382050433454 . 20 30
                  3 2012 55.51031629935422 . 20 30
                  3 2013 42.75892149756413 . 20 30
                  3 2014 42.58870065185263 . 20 30
                  3 2015 38.66464161399215 . 20 30
                  3 2016 38.16283765602253 30 20 30
                  3 2017 35.901719276674235 30 20 30
                  3 2018 31.76823388092783 30 20 30
                  3 2019 29.083975520359353 30 20 30
                  3 2020 35.466443416632806 30 . .
                  4 2010 13.629401380405556 . 20 30
                  4 2011 11.498256991311331 . 20 30
                  4 2012 11.902182508303016 . 20 30
                  4 2013 9.05182005357761 . 20 30
                  4 2014 7.7725206563572 . 20 30
                  4 2015 8.236622318116543 . 20 30
                  4 2016 8.307346250191708 30 20 30
                  4 2017 8.060889341870077 30 20 30
                  4 2018 8.57927134832722 30 20 30
                  4 2019 6.009584356431234 30 20 30
                  4 2020 18.56475931932497 30 . .
                  5 2010 48.20205685538229 . 20 30
                  5 2011 52.72353395422843 . 20 30
                  5 2012 52.62304727893432 . 20 30
                  5 2013 51.9211272739699 . 20 30
                  5 2014 47.36932675528731 . 20 30
                  5 2015 40.72472379640764 . 20 30
                  5 2016 37.46039747557665 30 20 30
                  5 2017 35.60754170496008 30 20 30
                  5 2018 37.46445455085329 30 20 30
                  5 2019 39.35614657470858 30 20 30
                  5 2020 46.36770931245763 30 . .
                  6 2010 55.169304134697384 . 20 30
                  6 2011 55.81783589741568 . 20 30
                  6 2012 54.16541547749293 . 20 30
                  6 2013 52.894036074963594 . 20 30
                  6 2014 50.65247850608024 . 20 30
                  6 2015 50.848954748850595 . 20 30
                  6 2016 48.234070991691794 30 20 30
                  6 2017 47.53355107806847 30 20 30
                  6 2018 41.88344834716078 30 20 30
                  6 2019 37.95968893948596 30 20 30
                  6 2020 42.53306148262104 30 . .
                  7 2010 42.73270699463585 . 20 30
                  7 2011 29.6540822376318 . 20 30
                  7 2012 21.917150859253635 . 20 30
                  7 2013 18.779599485923995 . 20 30
                  7 2014 18.68396377103102 . 20 30
                  7 2015 17.595765361344498 . 20 30
                  7 2016 20.434424785703616 30 20 30
                  7 2017 25.24985359848939 30 20 30
                  7 2018 22.942914321027107 30 20 30
                  7 2019 23.00061850204696 30 20 30
                  7 2020 32.140259112336956 30 . .
                  8 2010 62.89379587227899 . 20 30
                  8 2011 54.636502453802414 . 20 30
                  8 2012 49.46823829297042 . 20 30
                  8 2013 46.55611811478474 . 20 30
                  8 2014 52.27952705513421 . 20 30
                  8 2015 45.15868524675204 . 20 30
                  8 2016 44.68028431359114 30 20 30
                  8 2017 38.882650526043975 30 20 30
                  8 2018 38.723295356869336 30 20 30
                  8 2019 36.91251588322443 30 20 30
                  8 2020 47.66737954856063 30 . .
                  9 2010 54.68130318318205 . 20 30
                  9 2011 54.12536199436243 . 20 30
                  9 2012 44.91591657113688 . 20 30
                  9 2013 40.107441268795405 . 20 30
                  9 2014 39.95968997216527 . 20 30
                  9 2015 35.60231945319434 . 20 30
                  9 2016 32.65402291966365 30 20 30
                  9 2017 34.29539868175905 30 20 30
                  9 2018 29.861101735722034 30 20 30
                  9 2019 29.36796102256392 30 20 30
                  9 2020 42.56290405667885 30 . .
                  end
                  label values cdpto cdpto
                  label def cdpto 1 "Amazonas", modify
                  label def cdpto 2 "Ancash", modify
                  label def cdpto 3 "Apurimac", modify
                  label def cdpto 4 "Arequipa", modify
                  label def cdpto 5 "Ayacucho", modify
                  label def cdpto 6 "Cajamarca", modify
                  label def cdpto 7 "Cusco", modify
                  label def cdpto 8 "Huancavelica", modify
                  label def cdpto 9 "Huanuco", modify


                  **gráfico N° 1
                  summarize año
                  local xmax `r(max)'
                  #delimit ;
                  local opts "legend(off) graphregion(color(0 28 112))
                  ylabel(0.0 "0" 10 "10" 20 "20" 30 "30" 40 "40" 50 "50" 60 "60", ang(h) nogri) xlabel(`r(min)'(2)`r(max)', ang(h) nogri) ytitle("%",color("255 255 255")) xtitle("Año", color("252 203 2"%80))" ;
                  #delimit cr
                  *set scheme cleanplots
                  *set scheme s2color
                  graph set window fontface "Garamond Bold"
                  la var año "Año"

                  graph twoway (area fgt año, base(0.0) ysc(r(0.0 0.15)) fcolor("252 228 44") lwidth(*0.0 ..) lcolor("252 228 44")), by(cdpto, graphregion(color(0 28 112)) plotregion(fcolor("0 28 112") lcol(0 28 112)) ) `opts' ///
                  plotregion(fcolor(0 28 112) margin(zero)) legend(off) graphregion(color(0 28 112) margin(zero))


                  **gráfico N° 2
                  **fabplot ssc
                  ssc install fabplot
                  set scheme s1color
                  summarize año
                  local xmax `r(max)'
                  local min = `r(min)'
                  local max = `r(max)'
                  fabplot line fgt año, by(cdpto) frontopts(lw(thick)) xla(`min'(2)`max', grid) xtitle("año 2010 2020")

                  Comment


                  • #24
                    Andrés Lahur Talavera Cuya, please start a new thread as your question is different from the topic addressed in this thread. When doing so, specify what colors you want on the axes and whether you are referring to the axes lines or axes labels.

                    Comment


                    • #25
                      Hello everyone,

                      I have a similar question. I am using the Heatplot from SSC. My dates in X axis are messed up. Andrew Musau do you have any thoughts?

                      Figure 1

                      Click image for larger version

Name:	workplace_1.png
Views:	1
Size:	53.1 KB
ID:	1720986

                      Code:
                      heatplot c2m_workplaceclosing i.state2 i.dm,   xlabel(, labsize(*0.6) angle(vertical) format(%tm) nogrid) ///
                       color(viridis, reverse) ///   
                       cuts(0(1)4)  ///
                        legend(subtitle("{bf} Workplace Closures",  span  size(1.75)) ///
                         pos(1) ring(1) rows(1) keygap(0.5) colgap(0) size(1.5) symysize(0.85) symxsize(7) ///
                         order(0 1 "Open" 2 "rec. close" 3 "Part. Close" 4 "Fully Close" ) stack ) ///
                        p(lcolor(black%10) lwidth(*0.15)) ///
                       ytitle("") ///
                       xtitle("", size(vsmall)) ///
                        title("{bf}Workplace closure policy", pos(11) size(2.25) margin(b=-10 t=2))
                      When is use the following code. The dates are correctly shown but the data is not at the monthly level. Any idea how to fix this? I want a graph similar to Figure 1 but the x axis shouls look like figure 2.

                      Code:
                        summ dm
                        local x1 = `r(min)'
                        local x2 = `r(max)'
                      heatplot c2m_workplaceclosing i.state2 dm, ///
                       yscale(noline) ///
                       ylabel(, nogrid labsize(*0.7)) ///
                       xlabel(`x1'(1)`x2', labsize(*0.6) angle(vertical) format(%tm) nogrid) ///
                       color(viridis, reverse) ///   
                       cuts(0(1)4)  ///
                        legend(subtitle("{bf} Workplace Closures",  span  size(1.75)) ///
                         pos(1) ring(1) rows(1) keygap(0.5) colgap(0) size(1.5) symysize(0.85) symxsize(7) ///
                         order(0 1 "Open" 2 "rec. close" 3 "Part. Close" 4 "Fully Close" ) stack ) ///
                        p(lcolor(black%10) lwidth(*0.15)) ///
                       ytitle("") ///
                       xtitle("", size(vsmall)) ///
                        title("{bf}Workplace closure policy", pos(11) size(2.25) margin(b=-10 t=2))
                      Click image for larger version

Name:	workplace_2.png
Views:	1
Size:	39.4 KB
ID:	1720987




                      Code:
                      * Example generated by -dataex-. For more info, type help dataex
                      clear
                      input long state2 double(c2m_workplaceclosing dm)
                       5 2 722
                       5 0 748
                       5 1 730
                       5 2 737
                       5 0 753
                       5 0 732
                       5 3 736
                       5 0 720
                       5 1 742
                       5 1 735
                       5 0 734
                       5 0 754
                       5 2 726
                       5 2 738
                       5 1 744
                       5 2 725
                       5 0 752
                       5 0 751
                       5 0 747
                       5 1 740
                       5 3 724
                       5 0 721
                       5 0 743
                       5 3 723
                       5 0 750
                       5 0 733
                       5 0 749
                       5 1 731
                       5 1 729
                       5 0 755
                       5 0 745
                       5 0 746
                       5 3 727
                       5 1 741
                       5 1 739
                       5 3 728
                      18 1 734
                      18 1 748
                      18 2 727
                      18 1 729
                      18 2 732
                      18 1 749
                      18 1 753
                      18 2 725
                      18 0 720
                      18 0 741
                      18 2 745
                      18 3 737
                      18 3 722
                      18 1 755
                      18 2 728
                      18 1 752
                      18 2 726
                      18 3 738
                      18 1 733
                      18 1 730
                      18 2 739
                      18 3 736
                      18 1 750
                      18 0 721
                      18 2 744
                      18 1 754
                      18 0 743
                      18 1 751
                      18 1 731
                      18 0 746
                      18 2 740
                      18 0 742
                      18 3 723
                      18 1 735
                      18 3 724
                      18 0 747
                      22 1 722
                      22 0 730
                      22 2 725
                      22 2 727
                      22 2 742
                      22 3 736
                      22 1 743
                      22 1 733
                      22 2 728
                      22 3 724
                      22 0 752
                      22 2 726
                      22 0 749
                      22 0 720
                      22 2 740
                      22 1 744
                      22 1 729
                      22 3 737
                      22 2 741
                      22 0 754
                      22 1 734
                      22 0 755
                      22 1 732
                      22 2 738
                      22 0 747
                      22 3 723
                      22 1 745
                      22 0 721
                      end
                      format %tm state2
                      format %tm dm
                      label values state2 state2
                      label def state2 5 "Bihar", modify
                      label def state2 18 "Kerala", modify
                      label def state2 22 "Maharashtra", modify

                      Comment


                      • #26
                        Your axis consists of indicators "i.dm" and not values of the continuous variable "dm". So these are numbered 1/35 instead of 720/755. You don't get the same graph with

                        Code:
                        heatplot c2m_workplaceclosing i.state2 dm, xlabel(, labsize(*0.6) angle(vertical) format(%tm) nogrid) ///
                        but this should work. In your case, assign the labels yourself. Below, I use labmask from the Stata Journal.

                        Code:
                        net install gr0034, from(http://www.stata-journal.com/software/sj8-2)
                        Code:
                        gen lab= string(dm, "%tm")
                        gen dm2=dm
                        labmask dm2, values(lab)
                        heatplot c2m_workplaceclosing i.state2 i.dm2,   xlabel(,val labsize(*0.5) angle(vertical) nogrid) ///
                        color(viridis, reverse) ///  
                        cuts(0(1)4)  ///
                        legend(subtitle("{bf} Workplace Closures",  span  size(1.75)) ///
                        pos(1) ring(1) rows(1) keygap(0.5) colgap(0) size(1.5) symysize(0.85) symxsize(7) ///
                        order(0 1 "Open" 2 "rec. close" 3 "Part. Close" 4 "Fully Close" ) stack ) ///
                        p(lcolor(black%10) lwidth(*0.15)) ///
                        ytitle("") ///
                        xtitle("", size(vsmall))  ///
                        title("{bf}Workplace closure policy", pos(11) size(2.25) margin(b=-10 t=2))
                        Res.:
                        Click image for larger version

Name:	Graph.png
Views:	1
Size:	28.6 KB
ID:	1720997

                        Comment


                        • #27
                          Thanks a lot Andrew! Really appreciate your help. This works a like a charm, labmask completely slipped my mind. Now I am trying to make a loop of the following codes but I get the following error

                          program error: code follows on the same line as close brace

                          Code:
                          gen lab= string(dm, "%tm")
                          gen dm2=dm
                          labmask dm2, values(lab)
                          ********************************************************************************    
                          heatplot c2m_workplaceclosing i.state2 i.dm2,  ///
                              xlabel(,val labsize(*0.5) angle(vertical) nogrid) ///
                              color(viridis, reverse) ///  
                              cuts(0(1)4)  ///
                              legend(subtitle("{bf} Workplace Closures",  span  size(1.75)) ///
                             pos(1) ring(1) rows(1) keygap(0.5) colgap(0) size(1.5) symysize(0.85) symxsize(9) /// *change symxsize to increase legendsize
                             order(0 1 "Open" 2 "rec. close" 3 "Part. Close" 4 "Fully Close" ) stack ) ///
                              p(lcolor(black%10) lwidth(*0.15)) ///
                              ytitle("") ///
                              xtitle("", size(vsmall)) ///
                              title("{bf}Workplace closure policy", pos(11) size(2.25) margin(b=-10 t=2))
                            
                            
                            
                            heatplot c4m_restrictionsongatherings i.state2 i.dm2, ///
                              xlabel(,val labsize(*0.5) angle(vertical) nogrid) ///
                              color(viridis, reverse) ///  
                              cuts(0(1)4)  ///
                            legend(subtitle("{bf} Restrict gatherings",  span  size(1.75)) ///
                             pos(1) ring(1) rows(1) keygap(0.5) colgap(0) size(1.5) symysize(0.85) symxsize(9) ///
                             order(0 1 "No res." 2 "res. >1000" 3 "res 101-1000" 4 "res 11-101" ) stack ) ///
                            p(lcolor(black%10) lwidth(*0.15)) ///
                           ytitle("") ///
                           xtitle("", size(vsmall)) ///
                            title("{bf}Restrict gatherings policy", pos(11) size(2.25) margin(b=-10 t=2))

                          the loop

                          Code:
                          local varlist c2m_workplaceclosing c4m_restrictionsongatherings
                          
                          foreach var of local varlist {
                              local title
                              local legend_title
                              local xlabel_option
                              local order
                              if "`var'" == "c2m_workplaceclosing" {
                                  local title "Workplace closure policy"
                                  local legend_title "Workplace Closures"
                                  local xlabel_option "i.dm2"
                                  local order 0 1 "Open" 2 "rec. close" 3 "Part. Close" 4 "Fully Close"
                              } else if "`var'" == "c4m_restrictionsongatherings" {
                                  local title "Restrict gatherings policy"
                                  local legend_title "Restrict gatherings"
                                  local xlabel_option "i.dm2"
                                  local order 0 1 "No res." 2 "res. >1000" 3 "res 101-1000" 4 "res 11-101"
                              }
                          
                              heatplot `var' i.state2 `xlabel_option', ///
                                  xlabel(,val labsize(*0.5) angle(vertical) nogrid) ///
                                  color(viridis, reverse) ///
                                  cuts(0(1)4) ///
                                  legend(subtitle("{bf} `legend_title'", span size(1.75)) ///
                                  pos(1) ring(1) rows(1) keygap(0.5) colgap(0) size(1.5) symysize(0.85) symxsize(9) ///
                                  order(`order') stack) ///
                                  p(lcolor(black%10) lwidth(*0.15)) ///
                                  ytitle("") ///
                                  xtitle("", size(vsmall)) ///
                                  title("{bf}`title'", pos(11) size(2.25) margin(b=-10 t=2))
                          }

                          Code:
                          * Example generated by -dataex-. For more info, type help dataex
                          clear
                          input long state2 double(c2m_workplaceclosing c4m_restrictionsongatherings dm) float dm2
                           5 2 1 722 722
                           5 0 0 748 748
                           5 1 2 730 730
                           5 2 3 737 737
                           5 0 0 753 753
                           5 0 2 732 732
                           5 3 3 736 736
                           5 0 0 720 720
                           5 1 1 742 742
                           5 1 3 735 735
                           5 0 2 734 734
                           5 0 0 754 754
                           5 2 3 726 726
                           5 2 3 738 738
                           5 1 2 744 744
                           5 2 3 725 725
                           5 0 0 752 752
                           5 0 0 751 751
                           5 0 0 747 747
                           5 1 1 740 740
                           5 3 4 724 724
                           5 0 0 721 721
                           5 0 0 743 743
                           5 3 4 723 723
                           5 0 0 750 750
                           5 0 2 733 733
                           5 0 0 749 749
                           5 1 2 731 731
                           5 1 2 729 729
                           5 0 0 755 755
                           5 0 1 745 745
                           5 0 0 746 746
                           5 3 4 727 727
                           5 1 1 741 741
                           5 1 3 739 739
                           5 3 4 728 728
                          18 1 0 734 734
                          18 1 0 748 748
                          18 2 3 727 727
                          18 1 4 729 729
                          18 2 2 732 732
                          18 1 0 749 749
                          18 1 0 753 753
                          18 2 3 725 725
                          18 0 0 720 720
                          18 0 3 741 741
                          18 2 3 745 745
                          18 3 3 737 737
                          18 3 4 722 722
                          18 1 0 755 755
                          18 2 3 728 728
                          18 1 0 752 752
                          18 2 3 726 726
                          18 3 4 738 738
                          18 1 2 733 733
                          18 1 3 730 730
                          18 2 3 739 739
                          18 3 3 736 736
                          18 1 0 750 750
                          18 0 0 721 721
                          18 2 3 744 744
                          18 1 0 754 754
                          18 0 2 743 743
                          18 1 0 751 751
                          18 1 2 731 731
                          18 0 0 746 746
                          18 2 2 740 740
                          18 0 2 742 742
                          18 3 4 723 723
                          18 1 2 735 735
                          18 3 4 724 724
                          18 0 0 747 747
                          22 1 1 722 722
                          22 0 2 730 730
                          22 2 3 725 725
                          22 2 3 727 727
                          22 2 3 742 742
                          22 3 4 736 736
                          22 1 3 743 743
                          22 1 2 733 733
                          22 2 3 728 728
                          22 3 3 724 724
                          22 0 0 752 752
                          22 2 3 726 726
                          22 0 0 749 749
                          22 0 0 720 720
                          22 2 3 740 740
                          22 1 4 744 744
                          22 1 3 729 729
                          22 3 4 737 737
                          22 2 3 741 741
                          22 0 0 754 754
                          22 1 2 734 734
                          22 0 0 755 755
                          22 1 2 732 732
                          22 2 3 738 738
                          22 0 0 747 747
                          22 3 4 723 723
                          22 1 0 745 745
                          22 0 0 721 721
                          end
                          format %tm state2
                          format %tm dm
                          label values state2 state2
                          label def state2 5 "Bihar", modify
                          label def state2 18 "Kerala", modify
                          label def state2 22 "Maharashtra", modify
                          label values dm2 dm2
                          label def dm2 720 "2020m1", modify
                          label def dm2 721 "2020m2", modify
                          label def dm2 722 "2020m3", modify
                          label def dm2 723 "2020m4", modify
                          label def dm2 724 "2020m5", modify
                          label def dm2 725 "2020m6", modify
                          label def dm2 726 "2020m7", modify
                          label def dm2 727 "2020m8", modify
                          label def dm2 728 "2020m9", modify
                          label def dm2 729 "2020m10", modify
                          label def dm2 730 "2020m11", modify
                          label def dm2 731 "2020m12", modify
                          label def dm2 732 "2021m1", modify
                          label def dm2 733 "2021m2", modify
                          label def dm2 734 "2021m3", modify
                          label def dm2 735 "2021m4", modify
                          label def dm2 736 "2021m5", modify
                          label def dm2 737 "2021m6", modify
                          label def dm2 738 "2021m7", modify
                          label def dm2 739 "2021m8", modify
                          label def dm2 740 "2021m9", modify
                          label def dm2 741 "2021m10", modify
                          label def dm2 742 "2021m11", modify
                          label def dm2 743 "2021m12", modify
                          label def dm2 744 "2022m1", modify
                          label def dm2 745 "2022m2", modify
                          label def dm2 746 "2022m3", modify
                          label def dm2 747 "2022m4", modify
                          label def dm2 748 "2022m5", modify
                          label def dm2 749 "2022m6", modify
                          label def dm2 750 "2022m7", modify
                          label def dm2 751 "2022m8", modify
                          label def dm2 752 "2022m9", modify
                          label def dm2 753 "2022m10", modify
                          label def dm2 754 "2022m11", modify
                          label def dm2 755 "2022m12", modify

                          Comment


                          • #28
                            if "`var'" == "c2m_workplaceclosing" {
                            local title "Workplace closure policy"
                            local legend_title "Workplace Closures"
                            local xlabel_option "i.dm2" local order 0 1 "Open" 2 "rec. close" 3 "Part. Close" 4 "Fully Close"
                            } else if "`var'" == "c4m_restrictionsongatherings" {
                            You cannot close an -if- and start another -if- or -else- in the same line. You must split these over separate lines, as the error message indicates. Also -else- immediately followed by -if- makes no sense. There may be other errors, but you need something like

                            Code:
                            if "`var'" == "c2m_workplaceclosing" {
                                    local title "Workplace closure policy"        
                                    local legend_title "Workplace Closures"        
                                    local xlabel_option "i.dm2"        
                                    local order 0 1 "Open" 2 "rec. close" 3 "Part. Close" 4 "Fully Close"    
                                }  
                            if "`var'" == "c4m_restrictionsongatherings" {
                            Last edited by Andrew Musau; 18 Jul 2023, 15:18.

                            Comment


                            • #29
                              Thanks a lot Andrew! Changing the lines solved the issue.

                              Comment

                              Working...
                              X