Announcement

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

  • Stacked/Bar Graphs for Multiple Categorical Variables

    Hello,

    I am examining socio-demographic differences in attitudes towards FGC practice. Here is an example of my data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(FGC_GoodPractice Gender DOB RegionBirth)
    0 2 3 1
    0 2 1 1
    0 1 3 1
    0 1 3 1
    0 1 3 1
    0 2 1 1
    0 1 1 1
    0 1 1 1
    0 2 1 1
    0 1 3 1
    end
    label values FGC_GoodPractice labels20
    label def labels20 0 "No", modify
    label values Gender labels1
    label def labels1 1 "Women", modify
    label def labels1 2 "Men", modify
    label values DOB labels0
    label def labels0 1 "< 25", modify
    label def labels0 3 "45-64", modify
    label values RegionBirth labels2
    label def labels2 1 "West Africa", modify



    The following code gave me the relevant crosstabulations, shown below:


    Code:
    tabout Gender DOB RegionBirth FGC_GoodPractice using FGC_1.txt, ///
    c(row ci) sum f(3) ///
    style(tab) stats(chi2) font(bold) npos(col) cisep(-)
    
    
      
    No Yes Don't Know N
    % % % No. %
    Respondent Sex
    Women 77.1 20.8 2.2 371 100
    Men 66.2 32.4 1.4 145 100
    Respondent Date of Birth
    < 25 75.8 23.1 1.1 264 100
    45-64 72.5 24.6 2.8 211 100
    > 64 67.6 29.7 2.7 37 100
    African Region of Birth
    West Africa 73 25.1 1.9 482 100
    East Africa 88.9 7.4 3.7 27 100
    South Africa 80 20 0 5 100
    North Africa 100 0 0 2 100
    I want to convert the results to the bar graph below, produced using Excel ... ( I would actually prefer a stacked bar).



    Click image for larger version

Name:	BarGraph1.jpg
Views:	1
Size:	49.4 KB
ID:	1511832



    graph hbar and catplot are able to produce a graph of one variable at a time, but how do I get a graph showing the distribution of about 6 variables by the DV (approval of FGC practice)?

    Thanks in advance for your assistance.

    best wishes - cY


  • #2
    What code are you using for graph hbar and catplot (SSC, as you are asked to explain)?

    I think you will need to vary your command and use graph combine.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      What code are you using for graph hbar and catplot (SSC, as you are asked to explain)?

      I think you will need to vary your command and use graph combine.

      Code:
      catplot FGC_GoodPractice Gender, percent(Gender) bar(1, bcolor(blue)) blabel(bar, position(outside) ///
              format(%3.0f)) ylabel(none) yscale(r(0,60)) asyvars stack
      However, I would want the bars to be more compact / slimmer, with little space between the various categories of the IV (Genders).


      Thanks - cY

      Comment


      • #4
        @Yawo - The quick -catplot- (from SSC) example below gets you started but it's worth mentioning that
        (1) your data example doesn't vary which makes it difficult to use it to help you (notice I added some fake data to your example to get some figures to run)
        (2) your -tabout- (from SSC, assuming it's not tabout3 from the Watson website) code couldnt have produced the table you shared, the 'sum' option is turned on but in your cells() option you have 'row'.
        (3) I think you may want to dig into Nick's helpful article: https://www.stata-journal.com/sjpdf....iclenum=gr0034
        the figures around page 285 do a great job of tackling this type of challenge with a few different approaches.


        Code:
        clear
        input byte(FGC_GoodPractice Gender DOB RegionBirth)
        0 2 3 1
        0 2 1 1
        0 1 3 1
        2 1 3 1
        1 1 3 1
        0 2 1 1
        0 1 1 1
        0 1 1 1
        0 2 1 1
        0 1 3 1
        1 1 1 1
        1 2 1 1
        2 1 3 1
        0 2 1 1
        0 1 3 1
        1 1 3 1
        1 2 2 1
        2 2 3 1
        0 1 2 1
        0 1 3 1
        1 1 1 1
        1 2 1 1
        2 1 3 1
        end
        label values FGC_GoodPractice labels20
        label def labels20 0 "No", modify
        label def labels20 1 "Yes", modify
        label def labels20 2 "Unk", modify
        label values Gender labels1
        label def labels1 1 "Women", modify
        label def labels1 2 "Men", modify
        label values DOB labels0
        label def labels0 1 "< 25", modify
        label def labels0 3 "45-64", modify
        label values RegionBirth labels2
        label def labels2 1 "West Africa", modify
        label def labels2 2 "Other ", modify
        replace RegionBirth =2 if mod(_n, 5)
        li, nol
        
        tabout Gender DOB RegionBirth FGC_GoodPractice using FGC_1.txt, replace ///
        c(row  )   f(3) ///
        style(tab) stats(chi2) font(bold) npos(col) cisep(-)
        loc i =1
        foreach j in Region DOB Gender {
        catplot FGC_GoodPractice, percent over(`j') name(g`i', replace) ylab(0(10)60)
        loc `++i'
        }
        gr combine g1 g2 g3 , c(1) imargin(zero)

        *edit:

        @Yawo - I didnt see your follow up about shrinking the space between combined graphs. You can remove the axis labels and titles to get rid of some of the space. Also I used imargin(zero) but there are other options in the manuals to shrink the graph region margins for each individual graph being combined.

        Here's an updated/edited example :

        Code:
        label values FGC_GoodPractice labels20
        label def labels20 0 "No", modify
        label def labels20 1 "Yes", modify
        label def labels20 2 "Unk", modify
        label values Gender labels1
        label def labels1 1 "       Women", modify  //I PUT IN SPACES HERE TO BETTER/QUICKLY ALIGN THE AXES, THERE ARE LESS HACK-ISH WAYS TO DO THIS
        label def labels1 2 "       Men", modify
        label values DOB labels0
        label def labels0 1 "< 25", modify
        label def labels0 3 "         45-64", modify
        label values RegionBirth labels2
        label def labels2 1 "West Africa", modify
        label def labels2 2 "Other ", modify
        replace RegionBirth =2 if mod(_n, 5)
        li, nol
        
        tabout Gender DOB RegionBirth FGC_GoodPractice using FGC_1.txt, replace ///
        c(row  )   f(3) ///
        style(tab) stats(chi2) font(bold) npos(col) cisep(-)
        loc i =1
        loc ll " yscale(off)"
        foreach j in Region DOB Gender {
        if `i'==3 loc ll "ylab(0(10)60)"
        catplot FGC_GoodPractice, percent over(`j') name(g`i', replace) ytit("")  `ll'
        loc `++i'
        }
        gr combine g1 g2 g3 , c(1) imargin(zero)


        Click image for larger version

Name:	EXAMPLE1.png
Views:	1
Size:	59.4 KB
ID:	1511897
        Last edited by eric_a_booth; 12 Aug 2019, 07:43.
        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment


        • #5
          Originally posted by eric_a_booth View Post
          @Yawo - The quick -catplot- (from SSC) example below gets you started but it's worth mentioning that
          (1) your data example doesn't vary which makes it difficult to use it to help you (notice I added some fake data to your example to get some figures to run)
          (2) your -tabout- (from SSC, assuming it's not tabout3 from the Watson website) code couldnt have produced the table you shared, the 'sum' option is turned on but in your cells() option you have 'row'.
          (3) I think you may want to dig into Nick's helpful article: https://www.stata-journal.com/sjpdf....iclenum=gr0034
          the figures around page 285 do a great job of tackling this type of challenge with a few different approaches.
          Thanks Eric for your assistance -- re your question about the tabout output, yes, you are right. I removed the 'sum' option before running the model. Running it with it turned on generated errors .. I should have removed it in the command I pasted above.

          with appreciation, cY

          Comment


          • #6
            Hello,

            Could you please help with a similar example. I am completely stuck. I would like to re-create these three attached Excel graphs in Stata. Would the most simple solution be to transform my data to long format, especially for Graph 3 (in order to have a new variable "wave" so I can sort by it?)

            For Graph 2 - how can I add wave 7 depressive cases to the graph: catplot w2cesdbin w2wealthq, percent(w2wealthq) bar(1, bcolor(blue)) blabel(bar, position(outside) format(%9.1f)) ylabel(none) yscale(r(0,60)) asyvars stack

            Any guidance and help would be greatly appreciated. Thank you.


            ----------------------- copy starting from the next line -----------------------
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float id byte(w2wealthq w3wealthq w4wealthq w5wealthq w6wealthq w7wealthq w2cinema w2gallery w2theatre w7cinema w7gallery w7theatre) float(w2cesdbin w3cesdbin w4cesdbin w5cesdbin w6cesdbin w7cesdbin)
             1 2 1 . 1 . . 3 1 2 . . . 0 0 . 0 . .
             2 . 1 1 1 1 1 . . . 0 0 0 . 0 0 0 0 0
             3 5 . . . . . 0 1 0 . . . 0 . . . . .
             4 1 . . 1 1 1 1 2 3 0 3 3 1 . . 1 0 0
             5 . . . . 1 . . . . . . . . . . . 1 .
             6 3 3 3 . 3 3 1 1 1 1 0 0 1 0 0 . 0 0
             7 2 . . . . . 0 0 0 . . . 0 . . . . .
             8 . 1 . 2 . . . . . . . . . 1 . 1 . .
             9 2 1 2 1 1 . 1 1 1 . . . 0 0 0 0 0 .
            10 . 5 5 5 . . . . . . . . . 0 0 0 . .
            11 1 1 1 1 1 1 3 2 2 3 1 0 1 1 0 1 0 0
            12 4 3 4 4 4 4 1 1 3 0 0 3 0 0 0 0 0 0
            13 2 2 2 2 4 4 1 2 2 2 0 1 1 1 1 0 0 1
            14 2 . 2 . . . 0 0 0 . . . 0 . 0 . . .
            15 5 . . 2 5 . 1 2 1 . . . 0 . . 0 0 .
            16 2 . . . . . 0 0 0 . . . 0 . . . . .
            17 1 . 1 1 3 3 0 1 0 2 2 0 0 . 0 0 1 0
            18 3 . . . . . 2 2 0 . . . 0 . . . . .
            19 1 . . . . . 0 0 0 . . . 0 . . . . .
            20 3 3 4 4 . . 2 2 1 . . . 0 0 0 0 . .
            21 . . 2 2 2 . . . . . . . . . 0 0 0 .
            22 3 . . . . . 0 0 0 . . . 1 . . . . .
            23 3 3 3 . . . 1 1 1 . . . 0 0 1 . . .
            24 . . . 3 2 . . . . . . . . . . 0 0 .
            25 2 . 2 . 1 . 4 2 4 . . . 0 . 0 . 0 .
            26 5 . . . . . 0 0 0 . . . 0 . . . . .
            27 3 . 3 3 4 . 1 1 1 . . . 0 . 0 0 1 .
            28 . . . 1 . . . . . . . . . . . 0 . .
            29 5 5 5 5 5 5 1 2 2 1 1 1 0 0 0 0 0 0
            30 3 2 2 2 2 2 0 0 0 0 0 1 0 0 0 0 0 0
            31 . . 1 1 1 . . . . . . . . . 0 1 1 .
            32 2 . . . . . 0 0 0 . . . 0 . . . . .
            33 4 4 . . . . 1 2 0 . . . 0 0 . . . .
            34 1 1 1 1 1 . 0 0 0 . . . 1 1 1 1 1 .
            35 4 4 . 4 . 4 3 3 1 2 4 1 0 0 . 0 . 0
            36 3 . 3 . . . 0 0 1 . . . 0 . 0 . . .
            37 2 4 . 2 . . 3 1 4 . . . 0 0 . 0 . .
            38 3 4 4 4 4 4 4 3 4 0 3 3 0 0 0 0 0 0
            39 5 5 . . . . 3 3 3 . . . 0 0 . . . .
            40 1 . . . . . 0 0 0 . . . 1 . . . . .
            41 5 5 5 5 5 5 3 2 2 3 3 2 0 1 0 0 1 0
            42 3 2 . . . . 0 0 0 . . . 0 0 . . . .
            43 1 1 . . . . 0 0 0 . . . 0 0 . . . .
            44 2 2 . . . . 2 1 1 . . . 1 0 . . . .
            45 . . 3 4 3 3 . . . 1 0 3 . . 0 0 0 1
            46 5 5 5 5 5 5 3 1 2 3 2 2 0 0 0 0 0 0
            47 . 1 . 1 . . . . . . . . . 0 . 1 . .
            48 5 4 4 3 . . 1 2 2 . . . 0 0 0 0 . .
            49 2 2 2 2 . . 0 1 2 . . . 0 0 0 0 . .
            50 3 3 2 . 3 . 0 1 0 . . . 0 0 1 . 1 .
            end
            label values w2cinema arts
            label values w2gallery arts
            label values w2theatre arts
            label values w7cinema arts
            label values w7gallery arts
            label values w7theatre arts
            label def arts 0 "Never", modify
            label def arts 1 "Less than once a year", modify
            label def arts 2 "Once or twice a year", modify
            label def arts 3 "Every few months", modify
            label def arts 4 "Monthly or more", modify
            label values w2cesdbin depcases
            label values w3cesdbin depcases
            label values w4cesdbin depcases
            label values w5cesdbin depcases
            label values w6cesdbin depcases
            label values w7cesdbin depcases
            label def depcases 0 "non-case", modify
            label def depcases 1 "case", modify
            ------------------ copy up to and including the previous line ------------------




            Attached Files

            Comment


            • #7
              Graphs 2 and 3 are contestable designs. You would get clearer graphs just showing the % of cases. Graph 1 is a common kind of design, but in my view there are better ways to do it.

              I agree: you need a different data structure here -- unfortunately two different data structures, if I understand correctly. Many details here are matters of taste, but the following script gives some suggestions.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float id byte(w2wealthq w3wealthq w4wealthq w5wealthq w6wealthq w7wealthq w2cinema w2gallery w2theatre w7cinema w7gallery w7theatre) float(w2cesdbin w3cesdbin w4cesdbin w5cesdbin w6cesdbin w7cesdbin)
               1 2 1 . 1 . . 3 1 2 . . . 0 0 . 0 . .
               2 . 1 1 1 1 1 . . . 0 0 0 . 0 0 0 0 0
               3 5 . . . . . 0 1 0 . . . 0 . . . . .
               4 1 . . 1 1 1 1 2 3 0 3 3 1 . . 1 0 0
               5 . . . . 1 . . . . . . . . . . . 1 .
               6 3 3 3 . 3 3 1 1 1 1 0 0 1 0 0 . 0 0
               7 2 . . . . . 0 0 0 . . . 0 . . . . .
               8 . 1 . 2 . . . . . . . . . 1 . 1 . .
               9 2 1 2 1 1 . 1 1 1 . . . 0 0 0 0 0 .
              10 . 5 5 5 . . . . . . . . . 0 0 0 . .
              11 1 1 1 1 1 1 3 2 2 3 1 0 1 1 0 1 0 0
              12 4 3 4 4 4 4 1 1 3 0 0 3 0 0 0 0 0 0
              13 2 2 2 2 4 4 1 2 2 2 0 1 1 1 1 0 0 1
              14 2 . 2 . . . 0 0 0 . . . 0 . 0 . . .
              15 5 . . 2 5 . 1 2 1 . . . 0 . . 0 0 .
              16 2 . . . . . 0 0 0 . . . 0 . . . . .
              17 1 . 1 1 3 3 0 1 0 2 2 0 0 . 0 0 1 0
              18 3 . . . . . 2 2 0 . . . 0 . . . . .
              19 1 . . . . . 0 0 0 . . . 0 . . . . .
              20 3 3 4 4 . . 2 2 1 . . . 0 0 0 0 . .
              21 . . 2 2 2 . . . . . . . . . 0 0 0 .
              22 3 . . . . . 0 0 0 . . . 1 . . . . .
              23 3 3 3 . . . 1 1 1 . . . 0 0 1 . . .
              24 . . . 3 2 . . . . . . . . . . 0 0 .
              25 2 . 2 . 1 . 4 2 4 . . . 0 . 0 . 0 .
              26 5 . . . . . 0 0 0 . . . 0 . . . . .
              27 3 . 3 3 4 . 1 1 1 . . . 0 . 0 0 1 .
              28 . . . 1 . . . . . . . . . . . 0 . .
              29 5 5 5 5 5 5 1 2 2 1 1 1 0 0 0 0 0 0
              30 3 2 2 2 2 2 0 0 0 0 0 1 0 0 0 0 0 0
              31 . . 1 1 1 . . . . . . . . . 0 1 1 .
              32 2 . . . . . 0 0 0 . . . 0 . . . . .
              33 4 4 . . . . 1 2 0 . . . 0 0 . . . .
              34 1 1 1 1 1 . 0 0 0 . . . 1 1 1 1 1 .
              35 4 4 . 4 . 4 3 3 1 2 4 1 0 0 . 0 . 0
              36 3 . 3 . . . 0 0 1 . . . 0 . 0 . . .
              37 2 4 . 2 . . 3 1 4 . . . 0 0 . 0 . .
              38 3 4 4 4 4 4 4 3 4 0 3 3 0 0 0 0 0 0
              39 5 5 . . . . 3 3 3 . . . 0 0 . . . .
              40 1 . . . . . 0 0 0 . . . 1 . . . . .
              41 5 5 5 5 5 5 3 2 2 3 3 2 0 1 0 0 1 0
              42 3 2 . . . . 0 0 0 . . . 0 0 . . . .
              43 1 1 . . . . 0 0 0 . . . 0 0 . . . .
              44 2 2 . . . . 2 1 1 . . . 1 0 . . . .
              45 . . 3 4 3 3 . . . 1 0 3 . . 0 0 0 1
              46 5 5 5 5 5 5 3 1 2 3 2 2 0 0 0 0 0 0
              47 . 1 . 1 . . . . . . . . . 0 . 1 . .
              48 5 4 4 3 . . 1 2 2 . . . 0 0 0 0 . .
              49 2 2 2 2 . . 0 1 2 . . . 0 0 0 0 . .
              50 3 3 2 . 3 . 0 1 0 . . . 0 0 1 . 1 .
              end
              label values w2cinema arts
              label values w2gallery arts
              label values w2theatre arts
              label values w7cinema arts
              label values w7gallery arts
              label values w7theatre arts
              label def arts 0 "Never", modify
              label def arts 1 "Less than once a year", modify
              label def arts 2 "Once or twice a year", modify
              label def arts 3 "Every few months", modify
              label def arts 4 "Monthly or more", modify
              label values w2cesdbin depcases
              label values w3cesdbin depcases
              label values w4cesdbin depcases
              label values w5cesdbin depcases
              label values w6cesdbin depcases
              label values w7cesdbin depcases
              label def depcases 0 "non-case", modify
              label def depcases 1 "case", modify
              
              reshape long w, i(id) j(which) string 
              gen wave = real(substr(which, 1, 1))
              replace which = substr(which, 2, .)
              rename w y
              reshape wide y, i(id wave) j(which) string
              foreach v of var y* { 
                  label var `v'
              }
              rename y* *
              
              set scheme s1color 
              
              gen cesdpc = 100 * cesdbin 
              
              graph bar (mean) cesdpc, over(wave) blabel(bar, format(%3.1f)) b1title("wave") name(G2, replace) bar(1, color(blue*0.5)) ytitle(% of cases) yla(, ang(h))
              graph export depressives2.png , replace 
              
              label def wave 2 "wave 2" 7 "wave 7"
              label val wave wave 
              
              graph bar (mean) cesdpc if inlist(wave, 2, 7), over(wealthq) t1title(wave and wealth quintiles) over(wave) blabel(bar, format(%3.1f)) bar(1, color(blue*0.5)) ytitle(% of cases) yla(, ang(h)) name(G3, replace)
              graph export depressives3.png , replace 
              
              keep if inlist(wave, 2, 7)
              
              keep id wave cinema theatre gallery 
              
              rename (c t g) (y=)
              
              reshape long y, i(id wave) j(which) string 
              
              label def arts 0 "Never", modify
              label def arts 1 "Less than once a year", modify
              label def arts 2 "Once or twice a year", modify
              label def arts 3 "Every few months", modify
              label def arts 4 "Monthly or more", modify
              
              label val y arts 
              
              replace which = proper(which)
              
              * install from Stata Journal 
              tabplot y which, by(wave, note("percents by wave")) subtitle(, fcolor(green*0.1)) percent(which wave) showval yreverse xreverse sep(y) ytitle("") xtitle("") ///
              bar1(fcolor(red*0.7) lcolor(red)) bar2(fcolor(red*0.3) lcolor(red)) bar3(fcolor(blue*0.1) lcolor(blue)) ///
              bar4(fcolor(blue*0.5) lcolor(blue)) bar5(fcolor(blue*0.9) lcolor(blue)) name(G1, replace)
              
              graph export depressives1.png , replace
              Code:
              
              


              Note the typo "quintlines" for "quintiles".

              Click image for larger version

Name:	depressives1.png
Views:	1
Size:	25.6 KB
ID:	1582759
              Click image for larger version

Name:	depressives2.png
Views:	1
Size:	20.8 KB
ID:	1582760
              Click image for larger version

Name:	depressives3.png
Views:	1
Size:	20.7 KB
ID:	1582761

              Comment


              • #8
                Dear Nick,

                Thank you very much indeed for your help. May I please follow up on one more thing? I'd like to combine graph 3 (depression cases over wealth quintiles per wave) with a graph showing number of arts activities (artscore var) per wealth quintiles per wave. I've been trying to re-use your code for Graph 3, but I can't get it it work. I tried the below without any luck. Could you please help?

                graph bar (count | percent | asis) artscore if inlist(wave, 2, 7), over(wealthq) t1title(number of arts activities per wealth quintiles) over(wave) blabel(bar, format(%9.2f)) bar(1, color(blue*0.5)) ytitle(% of cases) yla(, ang(h)) name(G4, replace)
                graph export depressives3.png , replace

                graph combine G3 G4, ycommon name(combined, replace)
                graph display combined, xsize(10)

                ----------------------- copy starting from the next line -----------------------
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(id wave artscore cesdbin cinema gallery theatre wealthq cesdpc)
                1 2 1 0 3 1 2 2   0
                1 3 2 0 3 1 3 1   0
                1 4 . . . . . .   .
                1 5 2 0 3 1 4 1   0
                1 6 . . . . . .   .
                1 7 . . . . . .   .
                2 2 . . . . . .   .
                2 3 0 0 2 0 0 1   0
                2 4 1 0 3 0 0 1   0
                2 5 0 0 2 0 0 1   0
                2 6 0 0 0 0 0 1   0
                2 7 0 0 0 0 0 1   0
                3 2 0 0 0 1 0 5   0
                3 3 . . . . . .   .
                3 4 . . . . . .   .
                3 5 . . . . . .   .
                3 6 . . . . . .   .
                3 7 . . . . . .   .
                4 2 1 1 1 2 3 1 100
                4 3 . . . . . .   .
                4 4 . . . . . .   .
                4 5 2 1 0 3 3 1 100
                4 6 1 0 0 3 2 1   0
                4 7 2 0 0 3 3 1   0
                5 2 . . . . . .   .
                5 3 . . . . . .   .
                5 4 . . . . . .   .
                5 5 . . . . . .   .
                5 6 0 1 0 0 0 1 100
                5 7 . . . . . .   .
                6 2 0 1 1 1 1 3 100
                6 3 0 0 1 0 2 3   0
                6 4 0 0 0 1 1 3   0
                6 5 . . . . . .   .
                6 6 0 0 0 2 0 3   0
                6 7 0 0 1 0 0 3   0
                7 2 0 0 0 0 0 2   0
                7 3 . . . . . .   .
                7 4 . . . . . .   .
                7 5 . . . . . .   .
                7 6 . . . . . .   .
                7 7 . . . . . .   .
                8 2 . . . . . .   .
                8 3 0 1 1 2 2 1 100
                8 4 . . . . . .   .
                8 5 0 1 0 1 0 2 100
                8 6 . . . . . .   .
                8 7 . . . . . .   .
                9 2 0 0 1 1 1 2   0
                9 3 0 0 0 1 1 1   0
                end
                label values wave wave
                label def wave 2 "wave 2", modify
                label def wave 7 "wave 7", modify
                ------------------ copy up to and including the previous line ------------------


                Thank you and best wishes.
                Attached Files

                Comment


                • #9
                  Some variant on

                  Code:
                  preserve 
                  keep if inlist(wave, 2, 7)
                  label var wealthq "Wealth quintile"
                  label var artscore "Number of arts activities"
                  tabplot artscore wealthq if inlist(wave, 2, 7), by(wave, note("")) percent(wave wealthq) color(blue*0.5) showval yreverse 
                  restore

                  Comment


                  • #10
                    Dear Nick, thank you very much for your fantastic help! Is there a simple way to add % symbol in this command? Best wishes, U.

                    Comment


                    • #11
                      Where do you want % to appear?

                      Comment


                      • #12
                        Apologies, I meant to each data label (in a 9.2f format). Thank you, U.

                        Comment


                        • #13
                          Like this?

                          Code:
                          preserve 
                          
                          keep if inlist(wave, 2, 7)
                          label var wealthq "Wealth quintile"
                          label var artscore "Number of arts activities"
                          bysort wave wealthq (artscore): gen pc = _N 
                          bysort wave wealthq artscore : replace pc = 100 * _N/pc
                          gen toshow = string(pc, "%3.2f")  + "%"
                          
                          tabplot artscore wealthq, by(wave, note("")) percent(wave wealthq) color(blue*0.5) showval(toshow) yreverse 
                          restore

                          Comment

                          Working...
                          X