Announcement

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

  • is there a way to enforce a common range on a series of heatplots in Stata 17

    Dear Stateliest members,

    I am making graphs with a heatplot in Stata 17.0


    I created a series of heat plots and I would like to specify the range in them to be the same across the underlying z variable.



    Here is one heat plot. It shows the proportion of housework wives do within couples by spousal education. The size of the circle is proportionate of the frequency of cases in the data.

    Click image for larger version

Name:	hwsh_edu_1991.png
Views:	1
Size:	429.0 KB
ID:	1655076




    I reproduced the same graph for 4 years and combined it:

    Click image for larger version

Name:	hwsh_edu_comb.png
Views:	1
Size:	216.9 KB
ID:	1655077


    The main problem with the combined graph is that the colours mean different things on each graph because the range of values plotted is different on each graph. The heat plot then reflects smallest the largest values on each graph and we get 90 as green on one graph and as purple on another one. I am looking for a way to either force the range, i.e. each graph sets colours distributed between 70% and 98% even if in a given graph the actual range is 87% to 96%. Alternatively if I could specify colours to represent specific percentages (say 96 to 98 is red, 94 to 96 is blue etc), that would also work.



    Another issue is the “His education” axis labels. Is there a way to alternate them? At the moment on the combined graph they all overlap.

    the variables used are:

    own_shsumhousework_pc = wife's share of housework

    educomp = wife's education

    sp_educomp_rev = husband's education

    pin_gend = sex

    sure_year = survey year



    Here is my code:


    forvalues i = 1991(5)2006 {


    #delimit;

    heatplot own_shsumhousework_pc i.educomp i.sp_educomp_rev if psn_gend==0 & surv_year==`i', // not weighted

    sizeprop //sizeprop scales the size of the color fields in proportion to the relative frequency of the underlying data

    colors(viridis, intensity(0.8) reverse)

    scatter(circle)

    values(format(%9.0f) label(own_shsumhousework_pc) size(vsmall))

    xlabel(, nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) xscale(lcolor(none) alt)

    ylabel(, nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) yscale(lcolor(none))

    ytitle("{bf:Her education}") xtitle("{bf:His education}")

    p(lalign(inside) lwidth(vthin) msize(large))

    graphregion(color(white))

    legend(subtitle("Housework" "share"))

    addplot(function y = 1.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) ||

    function y = 2.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) ||

    function y = 3.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) ||

    function y = 1.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) ||

    function y = 2.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) ||

    function y = 3.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin)

    )

    text(4.6 -0.36 "`i'", size(small) /*margin(0 0 0 0)*/ justification(left))

    ;

    #delimit cr



    graph save "$FigTemp/hwsh_edu_`i'", replace

    graph export "$FigTemp/hwsh_edu_`i'.png", replace



    }



    graph combine "$FigTemp/hwsh_edu_1991" "$FigTemp/hwsh_edu_1996" "$FigTemp/hwsh_edu_2001" "$FigTemp/hwsh_edu_2006"

    graph save "$FigTemp/hwsh_edu_comb", replace

    graph export "$FigTemp/hwsh_edu_comb.png", replace



    I'd be grateful for any advice,

    Thank you,

    Ekaterina

  • #2
    heatplot is from SSC (FAQ Advice #12). You should provide a reproducible example that generates at least 2 heat plots to increase your chances of making progress. See the referenced FAQ Advice for details.

    Comment


    • #3
      and see -help heatplot##cuts- cuts(numlist) specifies the thresholds used to categorize z, ...

      Comment


      • #4
        Another good reason to give a data example is that heat plots, like anything else graphical, can vary in effectiveness. The graphic in #1 tries to show two quite different variables without showing either at all well. So, let us show some alternatives.



        Comment


        • #5
          Dear all, thank you for offering your help.
          below is a sample of 100 obs from my data (total dataset contains 200 000 obs) created with dataex. I know the Stateliest guidelines say usually 20 obs is sufficient, but I was worried about having at least a few obs in my 4 years of interest.
          Also just to say, thank you for mentioning the cuts(numlist) option, I tried, but still cannot make it work.

          When I added - cuts(70(2)98) keylabels(, interval) - to my code all the circles on my heat plot just became yellow.

          I would be grateful for any suggestions to solve my problem and/ or for more effective visualisations.
          Thank you,
          Ekaterina

          clear
          input float(own_shsumhousework1 own_shsumhousework_pc educomp sp_educomp_rev) int surv_year
          1 100 3 1 2006
          .9166667 91.66667 2 3 2006
          1 100 4 3 2006
          .7857143 78.57143 3 3 2006
          1 100 2 3 2006
          .8048781 80.48781 2 3 2006
          .375 37.5 2 4 2006
          1 100 2 1 2006
          .8095238 80.95238 2 3 2006
          1 100 2 4 2006
          .5555556 55.55556 2 3 2006
          .8 80 3 1 2006
          1 100 2 3 2006
          .625 62.5 4 1 2006
          1 100 1 3 2006
          1 100 2 3 2006
          .6875 68.75 2 3 2006
          1 100 3 1 2006
          1 100 3 3 2006
          .5714286 57.14286 4 1 2006
          1 100 2 1 2006
          .75 75 2 3 2001
          1 100 2 3 2001
          1 100 3 1 2001
          .8571429 85.71429 2 3 2001
          1 100 2 4 2001
          .6144578 61.44579 2 1 2001
          1 100 2 3 2001
          .6363636 63.63636 2 3 2001
          . . 2 2 2001
          1 100 3 3 2001
          1 100 2 3 2001
          1 100 2 3 2001
          1 100 4 1 2001
          .952381 95.2381 2 3 2001
          .8095238 80.95238 3 1 2001
          1 100 4 3 2001
          1 100 3 3 2001
          1 100 2 . 1996
          1 100 2 3 1996
          1 100 2 4 1996
          1 100 2 3 1996
          1 100 2 3 1996
          1 100 1 4 1996
          1 100 2 4 1996
          1 100 4 1 1996
          1 100 2 3 1996
          1 100 2 4 1996
          1 100 2 3 1996
          1 100 2 3 1996
          .6923077 69.230774 2 3 1996
          .8260869 82.6087 3 4 1996
          1 100 1 4 1996
          1 100 2 4 1996
          1 100 2 4 1996
          1 100 1 3 1996
          0 0 4 1 1996
          1 100 3 3 1996
          1 100 2 3 1996
          . . 1 4 1996
          .7692308 76.92308 2 4 1996
          1 100 2 3 1996
          1 100 2 4 1996
          1 100 3 1 1996
          1 100 4 3 1996
          .7142857 71.42857 2 3 1996
          . . 2 3 1996
          1 100 3 1 1996
          1 100 1 3 1996
          .52873564 52.87357 1 4 1996
          1 100 3 3 1996
          1 100 2 3 1996
          1 100 2 3 1996
          1 100 2 3 1991
          .8 80 2 3 1991
          .6 60 1 3 1991
          .8666667 86.66666 3 4 1991
          . . 2 4 1991
          1 100 3 2 1991
          .7272727 72.72727 2 4 1991
          1 100 4 3 1991
          1 100 1 3 1991
          1 100 1 4 1991
          1 100 2 4 1991
          1 100 1 4 1991
          1 100 1 4 1991
          1 100 2 3 1991
          .5 50 3 3 1991
          1 100 1 4 1991
          1 100 1 4 1991
          1 100 2 3 1991
          . . 1 4 1991
          1 100 2 3 1991
          1 100 2 3 1991
          1 100 1 4 1991
          1 100 2 3 1991
          1 100 3 1 1991
          .7272727 72.72727 3 2 1991
          .75 75 2 3 1991
          . . 2 3 1991
          end
          label values educomp educomp
          label def educomp 1 "Junior High School", modify
          label def educomp 2 "High school", modify
          label def educomp 3 "College or Professional School", modify
          label def educomp 4 "University", modify
          label values sp_educomp_rev sp_educomp_rev
          label def sp_educomp_rev 1 "University", modify
          label def sp_educomp_rev 2 "College or Professional School", modify
          label def sp_educomp_rev 3 "High school", modify
          label def sp_educomp_rev 4 "Junior High School", modify
          [/CODE]

          attached is my sample do-file I used to create the plots.
          10_1_period_wrk_exggraph.do

          Comment


          • #6
            As long as the units are the same across all graphs, the option -cut()- works to specify the upper and lower bounds and will do what you want. For integer values in the key, the lower bound should be below the lowest value if it has a fractional part whereas the upper bound should be above.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(own_shsumhousework1 own_shsumhousework_pc educomp sp_educomp_rev) int surv_year
                    1       100 3 1 2006
             .9166667  91.66667 2 3 2006
                    1       100 4 3 2006
             .7857143  78.57143 3 3 2006
                    1       100 2 3 2006
             .8048781  80.48781 2 3 2006
                 .375      37.5 2 4 2006
                    1       100 2 1 2006
             .8095238  80.95238 2 3 2006
                    1       100 2 4 2006
             .5555556  55.55556 2 3 2006
                   .8        80 3 1 2006
                    1       100 2 3 2006
                 .625      62.5 4 1 2006
                    1       100 1 3 2006
                    1       100 2 3 2006
                .6875     68.75 2 3 2006
                    1       100 3 1 2006
                    1       100 3 3 2006
             .5714286  57.14286 4 1 2006
                    1       100 2 1 2006
                  .75        75 2 3 2001
                    1       100 2 3 2001
                    1       100 3 1 2001
             .8571429  85.71429 2 3 2001
                    1       100 2 4 2001
             .6144578  61.44579 2 1 2001
                    1       100 2 3 2001
             .6363636  63.63636 2 3 2001
                    .         . 2 2 2001
                    1       100 3 3 2001
                    1       100 2 3 2001
                    1       100 2 3 2001
                    1       100 4 1 2001
              .952381   95.2381 2 3 2001
             .8095238  80.95238 3 1 2001
                    1       100 4 3 2001
                    1       100 3 3 2001
                    1       100 2 . 1996
                    1       100 2 3 1996
                    1       100 2 4 1996
                    1       100 2 3 1996
                    1       100 2 3 1996
                    1       100 1 4 1996
                    1       100 2 4 1996
                    1       100 4 1 1996
                    1       100 2 3 1996
                    1       100 2 4 1996
                    1       100 2 3 1996
                    1       100 2 3 1996
             .6923077 69.230774 2 3 1996
             .8260869   82.6087 3 4 1996
                    1       100 1 4 1996
                    1       100 2 4 1996
                    1       100 2 4 1996
                    1       100 1 3 1996
                    0         0 4 1 1996
                    1       100 3 3 1996
                    1       100 2 3 1996
                    .         . 1 4 1996
             .7692308  76.92308 2 4 1996
                    1       100 2 3 1996
                    1       100 2 4 1996
                    1       100 3 1 1996
                    1       100 4 3 1996
             .7142857  71.42857 2 3 1996
                    .         . 2 3 1996
                    1       100 3 1 1996
                    1       100 1 3 1996
            .52873564  52.87357 1 4 1996
                    1       100 3 3 1996
                    1       100 2 3 1996
                    1       100 2 3 1996
                    1       100 2 3 1991
                   .8        80 2 3 1991
                   .6        60 1 3 1991
             .8666667  86.66666 3 4 1991
                    .         . 2 4 1991
                    1       100 3 2 1991
             .7272727  72.72727 2 4 1991
                    1       100 4 3 1991
                    1       100 1 3 1991
                    1       100 1 4 1991
                    1       100 2 4 1991
                    1       100 1 4 1991
                    1       100 1 4 1991
                    1       100 2 3 1991
                   .5        50 3 3 1991
                    1       100 1 4 1991
                    1       100 1 4 1991
                    1       100 2 3 1991
                    .         . 1 4 1991
                    1       100 2 3 1991
                    1       100 2 3 1991
                    1       100 1 4 1991
                    1       100 2 3 1991
                    1       100 3 1 1991
             .7272727  72.72727 3 2 1991
                  .75        75 2 3 1991
                    .         . 2 3 1991
            end
            label values educomp educomp
            label def educomp 1 "Junior High School", modify
            label def educomp 2 "High school", modify
            label def educomp 3 "College or Professional School", modify
            label def educomp 4 "University", modify
            label values sp_educomp_rev sp_educomp_rev
            label def sp_educomp_rev 1 "University", modify
            label def sp_educomp_rev 2 "College or Professional School", modify
            label def sp_educomp_rev 3 "High school", modify
            label def sp_educomp_rev 4 "Junior High School", modify
            
            *DEFAULT
            heatplot own_shsumhousework_pc i.educomp i.sp_educomp_rev if surv_year==1991, sizeprop ///
            colors(viridis, intensity(0.8) reverse) ///
            scatter(circle) ///
            values(format(%9.0f) label(own_shsumhousework_pc) size(vsmall)) ///
            xlabel(, alt nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) xscale(lcolor(none) alt) ///
            ylabel(, nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) yscale(lcolor(none)) ///
            ytitle("{bf:Her education}") xtitle("{bf:His education}") ///
            p(lalign(inside) lwidth(vthin) msize(large)) ///
            graphregion(color(white))   ///
            legend(subtitle("Housework" "share (%)")) ///
            addplot(function y = 1.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 2.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 3.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 1.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) || ///        
                    function y = 2.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 3.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin)) ///
            text(4.6 -0.36 "`i'", size(small) justification(left))  
            
            
            *RANGE 50/100 WITH DELTA=5
            heatplot own_shsumhousework_pc i.educomp i.sp_educomp_rev if surv_year==1991, sizeprop ///
            colors(viridis, intensity(0.8) reverse) ///
            scatter(circle) ///
            values(format(%9.0f) label(own_shsumhousework_pc) size(vsmall)) ///
            xlabel(, alt nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) xscale(lcolor(none) alt) ///
            ylabel(, nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) yscale(lcolor(none)) ///
            ytitle("{bf:Her education}") xtitle("{bf:His education}") ///
            p(lalign(inside) lwidth(vthin) msize(large)) ///
            graphregion(color(white))   ///
            legend(subtitle("Housework" "share (%)")) ///
            addplot(function y = 1.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 2.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 3.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 1.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) || ///        
                    function y = 2.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 3.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin)) ///
             text(4.6 -0.36 "`i'", size(small) justification(left)) cut(47.5(5)102.5) keylabels(all)
            
            
            *RANGE 49/99 WITH DELTA=2
            heatplot own_shsumhousework_pc i.educomp i.sp_educomp_rev if surv_year==1991, sizeprop ///
            colors(viridis, intensity(0.8) reverse) ///
            scatter(circle) ///
            values(format(%9.0f) label(own_shsumhousework_pc) size(vsmall)) ///
            xlabel(, alt nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) xscale(lcolor(none) alt) ///
            ylabel(, nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) yscale(lcolor(none)) ///
            ytitle("{bf:Her education}") xtitle("{bf:His education}") ///
            p(lalign(inside) lwidth(vthin) msize(large)) ///
            graphregion(color(white))   ///
            legend(subtitle("Housework" "share (%)")) ///
            addplot(function y = 1.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 2.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 3.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 1.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) || ///        
                    function y = 2.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) || ///
                    function y = 3.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin)) ///
            text(4.6 -0.36 "`i'", size(small) justification(left)) cut(48(2)100) keylabels(all)
            Click image for larger version

Name:	Graph1.png
Views:	1
Size:	46.6 KB
ID:	1655386

            Click image for larger version

Name:	Graph2.png
Views:	1
Size:	43.2 KB
ID:	1655387


            Click image for larger version

Name:	Graph3.png
Views:	1
Size:	49.4 KB
ID:	1655389

            Last edited by Andrew Musau; 21 Mar 2022, 03:21.

            Comment


            • #7
              Thank you very much Andrew Mussau! It works!!!

              Comment


              • #8
                Although Andrew Musau's fix to syntax got you closer to where you want to be, as a spectator I can't say that these heat plots work for me at all.

                A reader is expected to decode circle size as representing joint predictor frequency and circle colour as representing the response variable that is of most interest. I suggest that is backwards in terms of representing the response variable by the most decorative graph element.

                Also scaling to have a range of circle size seems to overstate vastly the range of frequencies. At any rate. what dominates visual perception is something secondary.

                Last but not least, changes from year to year have to be inferred by really detailed scrutiny of a series of plots. Yet my guess is that these are very important and interesting for your project.

                Starting from a principle that showing the response is first priority, I worked from the first heatplot in #1 to get this.

                A positive of this design is that you could show other years on the graph. It would get crowded but could hardly be more complex than your present series of plots.

                Click image for larger version

Name:	1991.png
Views:	1
Size:	29.0 KB
ID:	1655408



                Here's the code, noting that there was a following tweak in the Graph Editor to drag the subtitle to the left.

                Code:
                clear
                input own_shsumhousework_pc educomp sp_educomp_rev
                90 1 4
                92 2 4
                91 3 4
                72 4 4
                90 1 3
                91 2 3
                92 3 3
                93 4 3
                92 1 2
                92 2 2
                93 3 2
                93 4 2
                88 1 1
                91 2 1
                93 3 1
                93 4 1
                end
                
                gen surv_year = 1991
                gen sp_educomp = 5 - sp_educomp_rev
                label values educomp educomp
                label def educomp 1 "Junior high", modify
                label def educomp 2 "High school", modify
                label def educomp 3 "College or Professional School", modify
                label def educomp 4 "University", modify
                label values sp_educomp educomp
                
                label def shorter 1 "Junior high", modify
                label def shorter 2 "High school", modify
                label def shorter 3 "College", modify
                label def shorter 4 "University", modify
                
                label val *educomp shorter
                
                set scheme s1color
                separate own, by(surv_year) veryshortlabel
                local vars `r(varlist)'
                
                local title % of housework by wives
                graph dot (asis) `vars', title(`title') over(educomp) over(sp_educomp) exclude0 linetype(line) lines(lc(gs12) lw(thin)) subtitle(His education Her education, place(left))
                Evidently this graph does nothing to show cell frequencies, for which I suspect that you need a different and complementary graph.


                Sometimes it's only when a data example is given that you can work out what an even better data example would have been. In your case this would be the ideal data example to give me if you are interested that I keep pushing at a different line of enquiry.


                Code:
                gen freq = 1
                collapse (sum) freq (mean) own_shsumhousework_pc, by(educomp sp_educomp_rev  surv_year)
                dataex
                These papers have some general bearing on associated issues.


                https://www.stata-journal.com/articl...article=gr0034

                https://www.stata-journal.com/articl...article=gr0061
                Last edited by Nick Cox; 21 Mar 2022, 06:39.

                Comment


                • #9
                  I have a follow on question, I just created graphs using my full dataset (around 200,000 observations) and in one of the years I have a very atypical housework sharing in a small sample of vary atypical couples (wife has university education, husband only graduated from compulsory school). While across all years women do between 70 and 97% of all housework in these couples they seem to only do 33% of housework in 1991. So in my heat plots, I would like to reserve a colour for this, but I do not want to have a continuous range of colours starting from 32 as in this case most of the differences will become invisible.
                  So I modified my do file as follows:

                  forvalues i = 1991(5)2006 {
                  * 1991, 1996, 2001, 2006
                  #delimit;
                  heatplot own_shsumhousework_pc i.educomp i.sp_educomp_rev if psn_gend==0 & surv_year==`i' [aw=weight], // not weighted
                  sizeprop //sizeprop scales the size of the color fields in proportion to the relative frequency of the underlying data
                  colors(viridis, intensity(0.8) reverse)
                  scatter(circle)
                  values(format(%9.0f) label(own_shsumhousework_pc) size(vsmall))
                  xlabel(, alt nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) xscale(lcolor(none) alt)
                  ylabel(, nogrid tlcolor(none) tposition(crossing) labsize(vsmall)) yscale(lcolor(none))
                  ytitle("{bf:Her education}") xtitle("{bf:His education}")
                  p(lalign(inside) lwidth(vthin) msize(small))
                  graphregion(color(white))
                  legend(subtitle("Housework" "share (%)"))
                  addplot(function y = 1.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) ||
                  function y = 2.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) ||
                  function y = 3.5, range(0.5 4.5) lcolor(gs14) lpattern(solid) lwidth(thin) ||
                  function y = 1.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) ||
                  function y = 2.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin) ||
                  function y = 3.5, range(0.5 4.5) horizontal lcolor(gs14) lpattern(solid) lwidth(thin))
                  text(5 -0.7 "`i'", size(small) justification(left)) cut(32 71(2)97) keylabels(all)
                  ;
                  #delimit cr

                  graph save "$FigTemp/hwsh_edu_`i'", replace
                  graph export "$FigTemp/hwsh_edu_`i'.png", replace

                  }

                  As a result I am getting this combined graph and I am not sure why do I not get 32 as a stand alone point before the continuous colours, but rather 51.5.
                  Click image for larger version

Name:	hwsh_edu_comb.png
Views:	1
Size:	596.4 KB
ID:	1655412

                  Comment


                  • #10
                    Nick Cox Thank you very much for your alternative suggestion and for engaging so thoughtfully with my graph. I will definitely explore the option you are suggesting. I understand what you mean in that the circle sizes are at best indicative as to which educational combinations are more and which are less common within the couples, and my main point is really about the inequality in sharing housework within couples.
                    I would love any further suggestions from you.
                    I ran the dataex code you suggested, just one note, what did you mean when you added pc to the list of variables to collapse by? I do not have a separate pc variable.

                    clear
                    input int surv_year float(educomp sp_educomp_rev) double freq float own_shsumhousework_pc
                    1991 1 1 110 78.60501
                    1996 1 1 94 76.4147
                    2001 1 1 44 49.00457
                    2006 1 1 56 62.95454
                    1991 1 2 398 25.403927
                    1996 1 2 369 21.93094
                    2001 1 2 251 28.23403
                    2006 1 2 244 19.82709
                    1991 1 3 5193 43.21383
                    1996 1 3 4203 39.58006
                    2001 1 3 2706 36.655983
                    2006 1 3 1767 35.75012
                    1991 1 4 10824 52.71196
                    1996 1 4 6613 53.80909
                    2001 1 4 2954 55.63626
                    2006 1 4 1519 54.99672
                    1991 1 . 52 33.14906
                    1996 1 . 37 42.54944
                    2001 1 . 34 28.61878
                    2006 1 . 24 38.74025
                    1991 2 1 2212 85.48348
                    1996 2 1 2297 82.12411
                    2001 2 1 1647 79.11248
                    2006 2 1 1743 73.51701
                    1991 2 2 2574 32.67799
                    1996 2 2 2759 31.134447
                    2001 2 2 2426 29.834135
                    2006 2 2 2806 30.47795
                    1991 2 3 23141 50.6507
                    1996 2 3 21260 51.08346
                    2001 2 3 14884 51.30815
                    2006 2 3 13380 51.41951
                    1991 2 4 5204 59.23846
                    1996 2 4 4246 64.04886
                    2001 2 4 2688 67.39347
                    2006 2 4 1878 70.433815
                    1991 2 . 110 39.33166
                    1996 2 . 127 52.67152
                    2001 2 . 105 30.247505
                    2006 2 . 132 46.70614
                    1991 3 1 1801 87.56026
                    1996 3 1 1981 87.0605
                    2001 3 1 1678 84.99903
                    2006 3 1 1939 83.84691
                    1991 3 2 1035 50.63071
                    1996 3 2 1023 50.31315
                    2001 3 2 1064 50.20925
                    2006 3 2 1333 50.80986
                    1991 3 3 2558 68.463875
                    1996 3 3 2756 69.82942
                    2001 3 3 2445 70.91792
                    2006 3 3 2855 70.32854
                    1991 3 4 382 77.91422
                    1996 3 4 368 78.50014
                    2001 3 4 255 73.42243
                    2006 3 4 260 82.51982
                    1991 3 . 10 56.66667
                    1996 3 . 34 66.92579
                    2001 3 . 40 66.78243
                    2006 3 . 24 68.35892
                    1991 4 1 3231 50.33433
                    1996 4 1 3630 50.34898
                    2001 4 1 2745 50.21304
                    2006 4 1 3287 50.17751
                    1991 4 2 1777 12.570534
                    1996 4 2 1943 13.086422
                    2001 4 2 1620 14.975884
                    2006 4 2 1885 16.594227
                    1991 4 3 2117 14.865718
                    1996 4 3 2212 18.00061
                    2001 4 3 1571 21.96676
                    2006 4 3 1660 27.63035
                    1991 4 4 100 20.887564
                    1996 4 4 81 25.534216
                    2001 4 4 40 53.75194
                    2006 4 4 56 37.04546
                    1991 4 . 14 .7738096
                    1996 4 . 51 26.43149
                    2001 4 . 64 37.43227
                    2006 4 . 30 30.76391
                    1991 . 3 4 50
                    2006 . 3 10 52.9304
                    1991 . 4 8 64.28571
                    2006 . 4 2 100
                    1991 . . 2 .
                    2006 . . 4 50
                    end
                    label values educomp educomp
                    label def educomp 1 "J-High School", modify
                    label def educomp 2 "High school", modify
                    label def educomp 3 "Non-uni tertiary *", modify
                    label def educomp 4 "University", modify
                    label values sp_educomp_rev sp_educomp_rev
                    label def sp_educomp_rev 1 "University", modify
                    label def sp_educomp_rev 2 "Non-uni tertiary *", modify
                    label def sp_educomp_rev 3 "High school", modify
                    label def sp_educomp_rev 4 "J-High School", modify
                    [/CODE]
                    ------------------ copy up to and including the previous line ------------------
                    Last edited by Ekaterina Hertog; 21 Mar 2022, 07:02.

                    Comment


                    • #11
                      pc was a typo in #8. Now fixed. Sorry about that.

                      #10 was what I was seeking. So, thanks, and I will get back to you.

                      Comment


                      • #12
                        Nick Cox Thank you.
                        I tried the graph dot code you suggested, but am running into some difficulties.
                        If I specify (asis) after graph dot I get an error message.

                        my code:
                        set scheme cleanplots
                        separate own_shsumhousework_pc, by(surv_year) veryshortlabel
                        local vars `r(varlist)'

                        local title % of housework by wives
                        graph dot (asis) `vars', title(`title') over(educomp) over(sp_educomp) exclude0 linetype(line) ///
                        lines(lc(gs12) lw(thin)) subtitle(His education Her education, place(left)) ///
                        note("* Non-uni tertiary = College or professional school education", size(vsmall) span)

                        graph export "$FigTemp/hwsh_edu_dot.png", replace

                        error:
                        variables sp_educomp, educomp, and _variables do not uniquely identify the observations
                        r(459);

                        If I run the same code without (asis), it would not pick up legend labels from variable labels. I tried to force it with legend(label) option, but it would not do it as expected (see attached graph.

                        my code without (asis):

                        set scheme cleanplots
                        separate own_shsumhousework_pc, by(surv_year) veryshortlabel
                        local vars `r(varlist)'

                        local title % of housework by wives
                        graph dot (asis) `vars', title(`title') over(educomp) over(sp_educomp) exclude0 linetype(line) ///
                        lines(lc(gs12) lw(thin)) subtitle(His education Her education, place(left)) ///
                        legend(label(1 "1991" 2 "1996" 3 "2001" 4 "2006")) ///
                        note("* Non-uni tertiary = College or professional school education", size(vsmall) span)

                        graph export "$FigTemp/hwsh_edu_dot.png", replace

                        Resulting graph:
                        Click image for larger version

Name:	hwsh_edu_dot.png
Views:	1
Size:	304.7 KB
ID:	1655424

                        Comment


                        • #13
                          (This should answer #12 by accident.)

                          This is where I got to. I used tabplot from the Stata Journal for the first graph.

                          Some of the data look puzzling: lots of means near 50%!

                          Code:
                          clear
                          input int surv_year float(educomp sp_educomp_rev) double freq float own_shsumhousework_pc
                          1991 1 1 110 78.60501
                          1996 1 1 94 76.4147
                          2001 1 1 44 49.00457
                          2006 1 1 56 62.95454
                          1991 1 2 398 25.403927
                          1996 1 2 369 21.93094
                          2001 1 2 251 28.23403
                          2006 1 2 244 19.82709
                          1991 1 3 5193 43.21383
                          1996 1 3 4203 39.58006
                          2001 1 3 2706 36.655983
                          2006 1 3 1767 35.75012
                          1991 1 4 10824 52.71196
                          1996 1 4 6613 53.80909
                          2001 1 4 2954 55.63626
                          2006 1 4 1519 54.99672
                          1991 1 . 52 33.14906
                          1996 1 . 37 42.54944
                          2001 1 . 34 28.61878
                          2006 1 . 24 38.74025
                          1991 2 1 2212 85.48348
                          1996 2 1 2297 82.12411
                          2001 2 1 1647 79.11248
                          2006 2 1 1743 73.51701
                          1991 2 2 2574 32.67799
                          1996 2 2 2759 31.134447
                          2001 2 2 2426 29.834135
                          2006 2 2 2806 30.47795
                          1991 2 3 23141 50.6507
                          1996 2 3 21260 51.08346
                          2001 2 3 14884 51.30815
                          2006 2 3 13380 51.41951
                          1991 2 4 5204 59.23846
                          1996 2 4 4246 64.04886
                          2001 2 4 2688 67.39347
                          2006 2 4 1878 70.433815
                          1991 2 . 110 39.33166
                          1996 2 . 127 52.67152
                          2001 2 . 105 30.247505
                          2006 2 . 132 46.70614
                          1991 3 1 1801 87.56026
                          1996 3 1 1981 87.0605
                          2001 3 1 1678 84.99903
                          2006 3 1 1939 83.84691
                          1991 3 2 1035 50.63071
                          1996 3 2 1023 50.31315
                          2001 3 2 1064 50.20925
                          2006 3 2 1333 50.80986
                          1991 3 3 2558 68.463875
                          1996 3 3 2756 69.82942
                          2001 3 3 2445 70.91792
                          2006 3 3 2855 70.32854
                          1991 3 4 382 77.91422
                          1996 3 4 368 78.50014
                          2001 3 4 255 73.42243
                          2006 3 4 260 82.51982
                          1991 3 . 10 56.66667
                          1996 3 . 34 66.92579
                          2001 3 . 40 66.78243
                          2006 3 . 24 68.35892
                          1991 4 1 3231 50.33433
                          1996 4 1 3630 50.34898
                          2001 4 1 2745 50.21304
                          2006 4 1 3287 50.17751
                          1991 4 2 1777 12.570534
                          1996 4 2 1943 13.086422
                          2001 4 2 1620 14.975884
                          2006 4 2 1885 16.594227
                          1991 4 3 2117 14.865718
                          1996 4 3 2212 18.00061
                          2001 4 3 1571 21.96676
                          2006 4 3 1660 27.63035
                          1991 4 4 100 20.887564
                          1996 4 4 81 25.534216
                          2001 4 4 40 53.75194
                          2006 4 4 56 37.04546
                          1991 4 . 14 .7738096
                          1996 4 . 51 26.43149
                          2001 4 . 64 37.43227
                          2006 4 . 30 30.76391
                          1991 . 3 4 50
                          2006 . 3 10 52.9304
                          1991 . 4 8 64.28571
                          2006 . 4 2 100
                          1991 . . 2 .
                          2006 . . 4 50
                          end
                          
                          label def shorter 1 "Junior high", modify
                          label def shorter 2 "High school", modify
                          label def shorter 3 "College", modify
                          label def shorter 4 "University", modify
                          
                          gen sp_educomp = 5 - sp_educomp_rev
                          label val *educomp shorter 
                          
                          save ekaterina, replace 
                          
                          drop if missing(educomp, sp_educomp)
                          
                          set scheme s1color
                          
                          tabplot educomp sp_educomp [fw=freq], by(surv_year, compact note("")) showval name(freq, replace) ytitle(His education) xtitle(Her education) height(0.7) ylabel(, labsize(small)) xlabel(, labsize(small)) subtitle(, fcolor(none))
                          
                          drop sp_educomp_rev freq 
                          rename own own 
                          reshape wide own, i(educomp sp_educomp) j(surv_year) 
                          foreach v of var own* { 
                              local year = substr("`v'", -4, 4)
                              label var `v' "`year'"
                          }
                          
                          local title % of housework by wives 
                          graph dot (asis) own*, title(`title')  over(educomp) over(sp_educomp) linetype(line) lines(lc(gs12) lw(thin)) subtitle(His education Her education, place(left)) marker(1, ms(Oh)) marker(2, ms(+)) marker(3, ms(Th)) marker(4, ms(X)) legend(row(1))
                          Click image for larger version

Name:	freq.png
Views:	1
Size:	46.6 KB
ID:	1655432
                          Click image for larger version

Name:	allyears.png
Views:	1
Size:	58.5 KB
ID:	1655433

                          Comment


                          • #14
                            Originally posted by Ekaterina Hertog View Post
                            text(5 -0.7 "`i'", size(small) justification(left)) cut(32 71(2)97) keylabels(all)
                            ;
                            #delimit cr

                            graph save "$FigTemp/hwsh_edu_`i'", replace
                            graph export "$FigTemp/hwsh_edu_`i'.png", replace

                            }

                            As a result I am getting this combined graph and I am not sure why do I not get 32 as a stand alone point before the continuous colours, but rather 51.5.
                            The point displayed is the midpoint between 32 and 71, i.e., 32+ [(71-32)/2]= 51.5. If you want 32, you need to solve for \(x\) in the equation: \( x+[(71-x)/2]= 32\) and then specify

                            Code:
                            cut(x 71(2)97)

                            Comment


                            • #15
                              Thank you Nick Cox and Andrew Musau. Will study more closely tomorrow.

                              Comment

                              Working...
                              X