Announcement

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

  • Title secondary y-axis when using two-way a and by

    I am producing the plot below, using the code below. The title for the secondaty y-axis does not show. I think this is because of the name of the axis when working with by and two-way, but I can't find the reply in the documentation. I would appreciate any advice.

    best,
    Matías

    Code:
     twoway (bar tot_inc wealth_quintile,  barw(0.6) bfcolor(eltblue*0.5) blcolor(eltblue) ) ///
           (scatter share_207 wealth_quintile, msymbol(O) mcolor(erose) yaxis(2)) ///
           (line share_207 wealth_quintile, lcolor(erose) yaxis(2)) ///
           (scatter share_208 wealth_quintile, msymbol(O) mcolor(grey*0.2) yaxis(2)) ///
           (line share_208 wealth_quintile, lcolor(grey*0.2) yaxis(2)) ///
            (scatter share_pot_inc_wood_1 wealth_quintile, msymbol(O) mcolor(eltgreen) yaxis(2)) ///
           (line share_pot_inc_wood_1 wealth_quintile, lcolor(eltgreen) yaxis(2)), ///       
           ytitle("Household average monthly expenditure" "(₦, thousand)") ytitle("% of household income", axis(2) orient(rvertical)) xtitle(Wealth decile) xlab(1(1)5) ///
           ylabel(0 "0" 500000 "500" 1000000 "1,000" 1500000 "1,500" 2000000 "2,000" 2500000 "2,500", axis(1) format(%12.0fc)) yscale(titlegap(*20)) ///
           ylabel(0 "0" 0.0002 "0.2" 0.0004 "0.4" 0.0006 "0.6" 0.0008 "0.8" 0.001 "1.0", axis(2) angle(horizontal)) ///
           legend(order(1 "Average income" 2 "Wood purch." 4 "Charcoal" 6 "Wood collected") region(lstyle(none)) size(small) col(4)) by(sector, note(""))

    Click image for larger version

Name:	Wood all and charcoal combined expenditure by income Rural urban.png
Views:	1
Size:	100.3 KB
ID:	1765409


  • #2
    Could you please post a minimal reproducible example (with data) that shows the problem? https://en.wikipedia.org/wiki/Minima...ucible_example explains.

    Quintiles, not deciles?

    Comment


    • #3
      Thanks for the quick reply. Yes, "quintiles", sorry the mistake. I was building on a code I had from before showing deciles.

      Please, find below a toy dataset like the one I am using. Is produced after collapsing the micro data.

      The figure was use using color scheme s1color (i.e., set scheme s1color).


      thanks,
      Matías
      wealth_quintile sector share_207 share_208 share_pot_inc_wood_1
      1 Urban 4.18E-05 0.000801 5.58E-06 547526
      1 Rural 0.000607 7.04E-05 1.08E-05 973667.9
      2 Urban 0.003328 5.37E-05 5.9E-06 877958.7
      2 Rural 0.00853 1.8E-05 8.74E-09 873080.8
      3 Urban 0.012604 0.000174 1.01E-05 1487076
      3 Rural 0.016411 8.98E-05 1.59E-05 1071905
      4 Urban 0.024498 0.000456 3.88E-06 1500782
      4 Rural 0.028919 5.55E-06 2.11E-07 1141885
      5 Urban 0.038069 0.00014 2.26E-06 1518364
      5 Rural 0.039885 0.000143 6.24E-06 1714759
      Last edited by Matias Piaggio; 09 Oct 2024, 09:35.

      Comment


      • #4
        I count 6 variables and 5 names. Should the last variable be called totinc ?

        Comment


        • #5
          Yes, that is right.

          thanks,
          Matías
          wealth_quintile sector share_207 share_208 share_pot_inc_wood_1 tot_inc
          1 Urban 4.18E-05 0.000801 5.58E-06 547526
          1 Rural 0.000607 7.04E-05 1.08E-05 973667.9
          2 Urban 0.003328 5.37E-05 5.90E-06 877958.7
          2 Rural 0.00853 1.80E-05 8.74E-09 873080.8
          3 Urban 0.012604 0.000174 1.01E-05 1487076
          3 Rural 0.016411 8.98E-05 1.59E-05 1071905
          4 Urban 0.024498 0.000456 3.88E-06 1500782
          4 Rural 0.028919 5.55E-06 2.11E-07 1141885
          5 Urban 0.038069 0.00014 2.26E-06 1518364
          5 Rural 0.039885 0.000143 6.24E-06 1714759

          Comment


          • #6
            Thanks for the revised data example, which can be coaxed into dataex form as requested in FAQ Advice #12.

            However, with that and the code in #1 I can't reproduce the graph in #1. I haven't tried to work out why, as to me the design looks too complicated to be successful. Here's another take. There are still some problems, such as the repeated legend.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte wealth_quintile str5 sector float(share_207 share_208 share_pot_inc_wood_1) double tot_inc
            1 "Urban" .0000418  .000801 5.58e-06   547526
            1 "Rural"  .000607 .0000704 .0000108 973667.9
            2 "Urban"  .003328 .0000537 5.90e-06 877958.7
            2 "Rural"   .00853  .000018 8.74e-09 873080.8
            3 "Urban"  .012604  .000174 .0000101  1487076
            3 "Rural"  .016411 .0000898 .0000159  1071905
            4 "Urban"  .024498  .000456 3.88e-06  1500782
            4 "Rural"  .028919 5.55e-06 2.11e-07  1141885
            5 "Urban"  .038069   .00014 2.26e-06  1518364
            5 "Rural"  .039885  .000143 6.24e-06  1714759
            end
            
            set scheme s1color 
            
            twoway (bar tot_inc wealth_quintile,  barw(0.6) bfcolor(eltblue*0.5) blcolor(eltblue) ) ///
                   (scatter share_207 wealth_quintile, msymbol(O) mcolor(erose) yaxis(2)) ///
                   (line share_207 wealth_quintile, lcolor(erose) yaxis(2)) ///
                   (scatter share_208 wealth_quintile, msymbol(O) mcolor(grey*0.2) yaxis(2)) ///
                   (line share_208 wealth_quintile, lcolor(grey*0.2) yaxis(2)) ///
                    (scatter share_pot_inc_wood_1 wealth_quintile, msymbol(O) mcolor(eltgreen) yaxis(2)) ///
                   (line share_pot_inc_wood_1 wealth_quintile, lcolor(eltgreen) yaxis(1 2)), ///       
                   ytitle("Household average monthly expenditure" "(, thousand)") ytitle("% of household income", axis(2) orient(rvertical)) xtitle(Wealth decile) xlab(1(1)5) ///
                   ylabel(0 "0" 500000 "500" 1000000 "1,000" 1500000 "1,500" 2000000 "2,000" 2500000 "2,500", axis(1) format(%12.0fc)) yscale(titlegap(*20)) ///
                   ylabel(0 "0" 0.0002 "0.2" 0.0004 "0.4" 0.0006 "0.6" 0.0008 "0.8" 0.001 "1.0", axis(2) angle(horizontal)) ///
                   legend(order(1 "Average income" 2 "Wood purch." 4 "Charcoal" 6 "Wood collected") region(lstyle(none)) size(small) col(4)) by(sector, note("")) name(original, replace)
                   
            gen tot_inc_2 = tot_inc / 1000 
            label var tot_inc_2 "Household expenditure" 
            
            foreach v of var share* { 
                gen `v'_2 = `v' * 1000 
            }
            
            label var share_207_2 "Wood purchased"
            label var share_208_2 "Charcoal"
            label var share_pot_inc_wood_1_2 "Wood collected"
            
            set scheme stcolor 
            local g = 1 
            foreach v of var *_2 { 
                twoway connect `v' wealth_quintile if sector == "Rural" || connect `v' wealth_quintile if sector == "Urban", name(G`g', replace) legend(pos(12) order(1 "Rural" 2 "Urban") row(1)) xtitle("")
                local graphs `graphs' G`g'
                local ++g 
            }
            
            graph combine `graphs', b2title(Wealth quintile)
            Click image for larger version

Name:	quintiles.png
Views:	1
Size:	57.1 KB
ID:	1765461

            Comment


            • #7
              Thanks Nick, this is helpful, but quite different.

              I end up solving it using the documentation, and using "r1title("% of household income", orient(rvertical))" instead of "ytitle("% of household income", axis(2) orient(rvertical))"

              best,
              Matías

              Comment


              • #8
                Indeed; it is deliberately quite different because I don't think your design is especially effective. Clearly the four outcomes have very different values, but yet it still makes sense to compare urban and rural values. That alone implies a design in which urban and rural are plotted together.

                Comment


                • #9
                  Here's another take. multiline is from SSC.


                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input byte wealth_quintile str5 sector float(share_207 share_208 share_pot_inc_wood_1) double tot_inc
                  1 "Urban" .0000418  .000801 5.58e-06   547526
                  1 "Rural"  .000607 .0000704 .0000108 973667.9
                  2 "Urban"  .003328 .0000537 5.90e-06 877958.7
                  2 "Rural"   .00853  .000018 8.74e-09 873080.8
                  3 "Urban"  .012604  .000174 .0000101  1487076
                  3 "Rural"  .016411 .0000898 .0000159  1071905
                  4 "Urban"  .024498  .000456 3.88e-06  1500782
                  4 "Rural"  .028919 5.55e-06 2.11e-07  1141885
                  5 "Urban"  .038069   .00014 2.26e-06  1518364
                  5 "Rural"  .039885  .000143 6.24e-06  1714759
                  end
                  
                      
                  gen tot_inc_2 = tot_inc / 1000
                  label var tot_inc_2 "Household expenditure"
                  
                  foreach v of var share* {
                      gen `v'_2 = `v' * 1000
                  }
                  
                  label var share_207_2 "Wood purchased"
                  label var share_208_2 "Charcoal"
                  label var share_pot_inc_wood_1_2 "Wood collected"
                  
                  set scheme stcolor
                  
                  multiline *_2 wealth_quintile, sepby(sector) xtitle(Wealth quintile) recast(connect)
                  Click image for larger version

Name:	quintiles.png
Views:	1
Size:	80.9 KB
ID:	1765479

                  Comment


                  • #10
                    The tiny story on #9 compared with #6: My recollection of multiline was that it allowed just one line in a panel. When I checked I saw that, on the contrary, I had already implemented an option to support two or more lines in each panel. So, #6 is considered by me to be superseded by #9. If you want a 2 x 2 display rather than a 4 x 1 display, that is possible.

                    A quite different comment is that the total income for rural quintile 1 exceeds that for rural quintile 2. This may be done to some nuance as between income and wealth, or a side-effect of tied values, but it seems puzzling.

                    Comment

                    Working...
                    X