Announcement

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

  • Multiple graphs combined with the over option (and only one legend)

    Dear all,
    I would like to know your feedback about the following issue.
    I need to realize the graph as the one I attach, that I obtained combining three different graphs realized with three different datasets (using the script below).

    Code:
    *
    la def uomo ///
                    0 "Donne" ///
                    1 "Uomini", modify
                    
    la val  uomo uomo
    la var uomo "uomo"
    
    *
    la def setto ///
                    1 "Legislatori, ufficiali e managers" ///
                    2 "Professionisti" ///
                    3 "Tecnici e professionisti associati" ///
                    4 "Impiegati" ///
                    5 "Addetti ai servizi e alle vendite" ///
                    6 "Qualificati nella pesca e agricoltura" ///
                    7 "Artigiani e operai" ///
                    8 "Operatori di macchina e di impianti e assemblatori" ///
                    9 "Occupazioni dequalificate", modify
                    
    la val  setto setto
    la var setto "settore"
    
    graph hbar perc, over(uomo) over(setto, gap(*.1) relabel(1 "" 2 "" 3 "" 4 "" 5 "" 6 "" 7 "" 8 "" 9 "")) ///
    asyvars stack ///
    title("Distribuzione di genere nelle professioni - 2015", size(small)) ///
    ylabel(0(5)20,labsize(small)) ///
    leg(rows(1)) ///
    blabel(bar,position(outside)) ///
    scheme(s1mono)
    graph save isco15.gph, replace
    What I do not like is that I have to fix 'manually' the size of each graph in order to make them more or less of the equal size (and still the result is not the best). So, I created a unique dataset and I run the same script, adding -over anno-, but the result is not what I would like to have. Do you have any clue on how can I run this? Below the new dataset on which I would like to have the graph.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(uomo setto anno) float perc
    0 1 1   .5
    0 2 1    4
    0 3 1  4.4
    0 4 1  6.7
    0 5 1  7.3
    0 6 1  1.9
    0 7 1  3.4
    0 8 1  1.5
    0 9 1  4.2
    1 1 1  2.4
    1 2 1  3.5
    1 3 1  8.8
    1 4 1  6.6
    1 5 1  8.7
    1 6 1  3.7
    1 7 1 18.4
    1 8 1  7.5
    1 9 1  6.5
    0 1 2    3
    0 2 2  3.8
    0 3 2 10.6
    0 4 2    7
    0 5 2  6.5
    0 6 2   .7
    0 7 2  2.3
    0 8 2  1.7
    0 9 2    4
    1 1 2  5.7
    1 2 2  5.1
    1 3 2 10.9
    1 4 2  4.6
    1 5 2  4.7
    1 6 2    2
    1 7 2 14.9
    1 8 2  7.4
    1 9 2  5.3
    0 1 3  5.1
    0 2 3  6.9
    0 3 3  7.1
    0 4 3  8.7
    0 5 3  6.5
    0 6 3   .7
    0 7 3  1.4
    0 8 3  1.5
    0 9 3  4.3
    1 1 3  6.2
    1 2 3  5.7
    1 3 3  9.7
    1 4 3  4.6
    1 5 3    4
    1 6 3  2.1
    1 7 3   13
    1 8 3  6.9
    1 9 3  5.8
    end
    label values uomo uomo
    label def uomo 0 "Donne", modify
    label def uomo 1 "Uomini", modify
    label values setto setto
    label def setto 1 "Legislatori, ufficiali e managers", modify
    label def setto 2 "Professionisti", modify
    label def setto 3 "Tecnici e professionisti associati", modify
    label def setto 4 "Impiegati", modify
    label def setto 5 "Addetti ai servizi e alle vendite", modify
    label def setto 6 "Qualificati nella pesca e agricoltura", modify
    label def setto 7 "Artigiani e operai", modify
    label def setto 8 "Operatori di macchina e di impianti e assemblatori", modify
    label def setto 9 "Occupazioni dequalificate", modify
    Thanks a lot, best, G

    Click image for larger version

Name:	a.png
Views:	1
Size:	269.1 KB
ID:	1473681

  • #2
    Thanks for the data example. This falls under "a classic use of the by() option".

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(uomo setto anno) float perc
    0 1 1   .5
    0 2 1    4
    0 3 1  4.4
    0 4 1  6.7
    0 5 1  7.3
    0 6 1  1.9
    0 7 1  3.4
    0 8 1  1.5
    0 9 1  4.2
    1 1 1  2.4
    1 2 1  3.5
    1 3 1  8.8
    1 4 1  6.6
    1 5 1  8.7
    1 6 1  3.7
    1 7 1 18.4
    1 8 1  7.5
    1 9 1  6.5
    0 1 2    3
    0 2 2  3.8
    0 3 2 10.6
    0 4 2    7
    0 5 2  6.5
    0 6 2   .7
    0 7 2  2.3
    0 8 2  1.7
    0 9 2    4
    1 1 2  5.7
    1 2 2  5.1
    1 3 2 10.9
    1 4 2  4.6
    1 5 2  4.7
    1 6 2    2
    1 7 2 14.9
    1 8 2  7.4
    1 9 2  5.3
    0 1 3  5.1
    0 2 3  6.9
    0 3 3  7.1
    0 4 3  8.7
    0 5 3  6.5
    0 6 3   .7
    0 7 3  1.4
    0 8 3  1.5
    0 9 3  4.3
    1 1 3  6.2
    1 2 3  5.7
    1 3 3  9.7
    1 4 3  4.6
    1 5 3    4
    1 6 3  2.1
    1 7 3   13
    1 8 3  6.9
    1 9 3  5.8
    end
    label values uomo uomo
    label def uomo 0 "Donne", modify
    label def uomo 1 "Uomini", modify
    label values setto setto
    label def setto 1 "Legislatori, ufficiali e managers", modify
    label def setto 2 "Professionisti", modify
    label def setto 3 "Tecnici e professionisti associati", modify
    label def setto 4 "Impiegati", modify
    label def setto 5 "Addetti ai servizi e alle vendite", modify
    label def setto 6 "Qualificati nella pesca e agricoltura", modify
    label def setto 7 "Artigiani e operai", modify
    label def setto 8 "Operatori di macchina e di impianti e assemblatori", modify
    label def setto 9 "Occupazioni dequalificate", modif
    
    label define anno 1 "1992" 2 "2008" 3 "2015"
    label values anno anno
    
    graph hbar perc,  over(uomo) over(setto, gap(*.1) ///
    relabel(1 "" 2 "" 3 "" 4 "" 5 "" 6 "" 7 "" 8 "" 9 "")) ///
    by(anno, row(1) title("Distribuzione di genere nelle professioni")) /// 
    asyvars stack  ylabel(0(5)20,labsize(small)) leg(rows(1)) ///
    blabel(bar,position(outside)) scheme(s1mono)  ytitle("") horiz

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	85.0 KB
ID:	1473759

    Comment


    • #3
      Dear Andrew,
      thanks a lot for your answer, I often have trouble in putting the option -by- in the right place.

      The only thinkg I would like to add to your graphs is the fact that (as in my example attached) I need to show the labels only once (in the first graph to the left).

      How could I do?

      Thanks, G

      Comment


      • #4
        Hi Giorgio

        I thought there was an easy option to do this, but it eludes me. The issue is that Stata rescales the widths of the last by() group or groups, leading to something similar to what you had when combining the graphs. I think that I have understood the default behavior, leading to the following work-around. The idea is to expand the number of by() groups to the required plus one, and assign different values to each group. Then you can use labmask (Stata Journal; Nick Cox) to assign consistent labels across groups.


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(uomo setto anno) float perc
        0 1 1   .5
        0 2 1    4
        0 3 1  4.4
        0 4 1  6.7
        0 5 1  7.3
        0 6 1  1.9
        0 7 1  3.4
        0 8 1  1.5
        0 9 1  4.2
        1 1 1  2.4
        1 2 1  3.5
        1 3 1  8.8
        1 4 1  6.6
        1 5 1  8.7
        1 6 1  3.7
        1 7 1 18.4
        1 8 1  7.5
        1 9 1  6.5
        0 1 2    3
        0 2 2  3.8
        0 3 2 10.6
        0 4 2    7
        0 5 2  6.5
        0 6 2   .7
        0 7 2  2.3
        0 8 2  1.7
        0 9 2    4
        1 1 2  5.7
        1 2 2  5.1
        1 3 2 10.9
        1 4 2  4.6
        1 5 2  4.7
        1 6 2    2
        1 7 2 14.9
        1 8 2  7.4
        1 9 2  5.3
        0 1 3  5.1
        0 2 3  6.9
        0 3 3  7.1
        0 4 3  8.7
        0 5 3  6.5
        0 6 3   .7
        0 7 3  1.4
        0 8 3  1.5
        0 9 3  4.3
        1 1 3  6.2
        1 2 3  5.7
        1 3 3  9.7
        1 4 3  4.6
        1 5 3    4
        1 6 3  2.1
        1 7 3   13
        1 8 3  6.9
        1 9 3  5.8
        end
        label values uomo uomo
        label def uomo 0 "Donne", modify
        label def uomo 1 "Uomini", modify
        label values setto setto
        label def setto 1 "Legislatori, ufficiali e managers", modify
        label def setto 2 "Professionisti", modify
        label def setto 3 "Tecnici e professionisti associati", modify
        label def setto 4 "Impiegati", modify
        label def setto 5 "Addetti ai servizi e alle vendite", modify
        label def setto 6 "Qualificati nella pesca e agricoltura", modify
        label def setto 7 "Artigiani e operai", modify
        label def setto 8 "Operatori di macchina e di impianti e assemblatori", modify
        label def setto 9 "Occupazioni dequalificate", modify
        
        expand 2, gen(x)
        replace anno= anno*100 if x
        keep if anno<101
        
        
        label define anno 1 "1992" 2 "2008" 3 "2015" 4"what"
        label values anno anno
        
        *ORIGINAL RUNS 1-9. REPLACE 10-18, SECOND, 19-27, THIRD, ...
        replace setto= setto+9 if anno==2
        replace setto= setto+18 if anno==3
        replace setto= setto+27 if anno==100
        
        *CREATE STRING VARIABLE TO INPUT TO LABMASK
        decode setto, gen(lab)
        replace lab= string(setto-9) if anno==2
        replace lab= string(setto-18) if anno==3
        replace lab= string(setto-27) if anno==100
        
        *LABMASK THE VALUES
        *findit labmask to install
        labmask setto, values(lab)
        
        *PLOT EXCLUDING LAST -BY() GROUP- TO GET EQUAL BAR WIDTHS
        graph hbar perc if anno<100, over(uomo) over(setto, gap(*0.1) ) ///
        by(anno, row(1) title("Distribuzione di genere nelle professioni") note("")) ///
        asyvars stack  ylabel(0(5)20,labsize(small)) leg(rows(1)) ///
        blabel(bar,position(outside))  ytitle("") horiz nofill scheme(s1mono)

        Here is the graph
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	51.0 KB
ID:	1473880

        Comment


        • #5
          Dear Andrew, when running your script at #2 Stata incurrs in an errore "type mismatch".

          How is it possible? Does it work to you?

          Thanks, G

          Comment


          • #6
            Andrew's code has some nice effects, but note e.g.

            Code:
            .5 2.4
            awkwardly placed at top left.

            Here is a different take with tabplot (Stata Journal). I made one change only in the Graph Editor, moving the title.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input byte(uomo setto anno) float perc
            0 1 1   .5
            0 2 1    4
            0 3 1  4.4
            0 4 1  6.7
            0 5 1  7.3
            0 6 1  1.9
            0 7 1  3.4
            0 8 1  1.5
            0 9 1  4.2
            1 1 1  2.4
            1 2 1  3.5
            1 3 1  8.8
            1 4 1  6.6
            1 5 1  8.7
            1 6 1  3.7
            1 7 1 18.4
            1 8 1  7.5
            1 9 1  6.5
            0 1 2    3
            0 2 2  3.8
            0 3 2 10.6
            0 4 2    7
            0 5 2  6.5
            0 6 2   .7
            0 7 2  2.3
            0 8 2  1.7
            0 9 2    4
            1 1 2  5.7
            1 2 2  5.1
            1 3 2 10.9
            1 4 2  4.6
            1 5 2  4.7
            1 6 2    2
            1 7 2 14.9
            1 8 2  7.4
            1 9 2  5.3
            0 1 3  5.1
            0 2 3  6.9
            0 3 3  7.1
            0 4 3  8.7
            0 5 3  6.5
            0 6 3   .7
            0 7 3  1.4
            0 8 3  1.5
            0 9 3  4.3
            1 1 3  6.2
            1 2 3  5.7
            1 3 3  9.7
            1 4 3  4.6
            1 5 3    4
            1 6 3  2.1
            1 7 3   13
            1 8 3  6.9
            1 9 3  5.8
            end
            label values uomo uomo
            label def uomo 0 "Donne", modify
            label def uomo 1 "Uomini", modify
            label values setto setto
            label def setto 1 "Legislatori, ufficiali e managers", modify
            label def setto 2 "Professionisti", modify
            label def setto 3 "Tecnici e professionisti associati", modify
            label def setto 4 "Impiegati", modify
            label def setto 5 "Addetti ai servizi e alle vendite", modify
            label def setto 6 `" "Qualificati nella pesca" "e agricoltura" "', modify
            label def setto 7 "Artigiani e operai", modify
            label def setto 8 `" "Operatori di macchina e" "di impianti e assemblatori" "', modify
            label def setto 9 "Occupazioni dequalificate", modify
            
            label define anno 1 "1992" 2 "2008" 3 "2015" 4"what"
            label values anno anno
            
            tabplot setto anno [iw=perc], by(uomo, compact note("") title("Distribuzione di genere nelle professioni", place(e))) ///
            subtitle(, fcolor(none)) showval(mlabsize(vsmall)) ///
            xtitle("") ytitle("") yla(, labsize(medsmall)) /// 
            separate(uomo) bar1(bfcolor(red*0.2) blcolor(red)) bar2(bfcolor(blue*0.2) blcolor(blue))

            Click image for larger version

Name:	another_tabplot.png
Views:	1
Size:	34.3 KB
ID:	1473908

            Comment


            • #7
              Dear Nick, I don t know why but with your script Stata says "invalid syntax"

              Comment


              • #8
                The is an error in #2, the following line

                label def setto 9 "Occupazioni dequalificate", modif
                should be

                Code:
                label def setto 9 "Occupazioni dequalificate", modify
                Once corrected, run the full code on a do file. I suspect the same is true of Nick's code.

                Comment


                • #9
                  Without more information I can't help on #7, assuming that you installed tabplot from Stata Journal.

                  It runs for me; else how would the graphs have been produced?

                  Which command is it that triggers the error message?
                  Last edited by Nick Cox; 07 Dec 2018, 10:23.

                  Comment


                  • #10
                    Dear Andrew, yes, I noticed your error and I fixed, thanks a lot for having pointed it.

                    I believe that the error in Nick's code is in the label part: when I substituted that part of code with yours, everything works.

                    Thanks a lot to both, your help is really appreciated.

                    Best, G

                    Comment


                    • #11
                      But I do not succeed in making the labels in two rows, as in the Nick's example

                      Comment


                      • #12
                        Dear Nick, I do not really know, the command is installed

                        Comment


                        • #13
                          In response to #9
                          I think the problem is in the command label, specifically in the lines to put in two rows the labels 3, 6 and 8

                          Comment


                          • #14
                            Actually no, when I run only the labels script there is no error, but once I run

                            - tabplot setto anno [iw=perc], by(uomo, compact note("") title("Distribuzione di genere nelle professioni", place(e))) ///
                            subtitle(, fcolor(none)) showval(mlabsize(vsmall)) ///
                            xtitle("") ytitle("") yla(, labsize(medsmall)) ///
                            separate(uomo) bar1(bfcolor(red*0.2) blcolor(red)) bar2(bfcolor(blue*0.2) blcolor(blue)) -

                            Stata signals the error


                            The problem are the labels set to stay in two rows: if I put them in a unique row, the script goes normally and I obtain the graph.

                            Is that weird, isn't it?
                            Last edited by Giorgio Piccitto; 07 Dec 2018, 11:01.

                            Comment


                            • #15
                              There is no error in my code as it runs and produces the desired result. Please show the results of

                              Code:
                              which tabplot 
                              to check you have the most up-to-date version from


                              SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
                              (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
                              Q3/17 SJ 17(3):779
                              added options for reversing axis scales; improved handling of
                              axis labels containing quotation marks

                              Comment

                              Working...
                              X