Announcement

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

  • Problem with xlabel's size

    Hi Statalisters,
    Using the dataset shown at the bottom, I have created the following plot which is so close to what I want but with some problems as follows:
    1) The label values for the x-axis are too big,
    2) The label values for the y-axis do not look good,
    3) The value labels on the bars do not fit in the bar and overlap,

    Here is the code which I used to create the plot. Please advise how can I customize the plot.

    Code:
    graph bar Healthy Unhealthy Dead , over(category_num, label(angle(45))) by(gender, note("")) stack ///
    legend(order(1 "Healthy" 2 "Unhealthy" 3 "Dead") rows(1) size(small) region(lcolor(white))) ///
    ylabel(0 "0" .2 "20%" .4  "40%" .6 "60%" .8 "80%" 1 "100%", noticks nogrid angle(0) labsize(small)) ///
    graphregion(color(white)) ///
    blabel(bar, color(white) position(inside) format(%2.1f))
    Thanks,
    Nader

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	168.3 KB
ID:	1623111




    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(gender category_num Dead Healthy Unhealthy)
    0 0 4.4 73.2 22.5
    0 1 3.2 75.8   21
    0 2 2.4   79 18.6
    0 3 2.2 77.6 20.2
    0 4 2.4 71.1 26.5
    0 5 2.2 81.1 16.7
    0 6 2.6 76.9 20.5
    0 7 3.3 71.1 25.6
    0 8 1.9   78 20.1
    1 0 1.9 79.3 18.8
    1 1 1.5 80.4 18.1
    1 2   1 83.9   15
    1 3 1.1 80.7 18.2
    1 4 1.3 81.5 17.2
    1 5  .6 86.2 13.3
    1 6 1.7 79.6 18.7
    1 7  .8 78.1 21.1
    1 8 1.1 82.9 16.1
    end
    label values gender gender
    label def gender 0 "Male", modify
    label def gender 1 "Female", modify
    label values category_num category_num
    label def category_num 0 "NCG(never)", modify
    label def category_num 1 "NCG(<weekly)", modify
    label def category_num 2 "NCG(weekly+)", modify
    label def category_num 3 "Low-intensity CG(never)", modify
    label def category_num 4 "Low-intensity CG(<weekly)", modify
    label def category_num 5 "Low-intensity CG(weekly+)", modify
    label def category_num 6 "High-intensity CG(never)", modify
    label def category_num 7 "High-intensity CG(<weekly)", modify
    label def category_num 8 "High-intensity CG(weekly+)", modify
    Last edited by Nader Mehri; 12 Aug 2021, 19:50.

  • #2
    I recommend something quite different as

    1. your category labels are at best awkward to read

    2. your bar text labels are really hard to read

    3. the default colours (here using scheme s2color) are heavy on the eye and not obviously ordered

    Tinkering with your design could solve #3 but not #1 or #2.

    Also, your y axis labels confuse proportions and percents: that could be fixed.


    I inserted an extra space in the category value labels and first used tabplot from the Stata Journal. See gr0066 for the write-up and gr0066_2 for the latest (public) version of code and help file.
    Alternatively https://www.statalist.org/forums/for...updated-on-ssc gives an overview.

    Then it seemed that a much clearer graph would result from omitting the Healthy category. Sure, they are good news but the percents are implicit as the sum of the other two.

    Then I played around with graph dot.

    The fact that females do better than males, systematically, is clearest in the last two graphs here.


    Code:
    . search tabplot, sj
    
    Search of official help files, FAQs, Examples, and Stata Journals
    
    SJ-20-3 gr0066_2  . . . . . . . . . . . . . . . .  Software update for tabplot
            (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
            Q3/20   SJ 20(3):757--758
            added new options frame() and frameopts() allowing framing
            of bars and so-called thermometer plots or charts
    
    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
    
    SJ-16-2 gr0066  . . . . . .  Speaking Stata: Multiple bar charts in table form
            (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
            Q2/16   SJ 16(2):491--510
            provides multiple bar charts in table form representing
            contingency tables for one, two, or three categorical variables


    Here is all the code starting with your data example (but with category labels tweaked as already mentioned.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(gender category_num Dead Healthy Unhealthy)
    0 0 4.4 73.2 22.5
    0 1 3.2 75.8   21
    0 2 2.4   79 18.6
    0 3 2.2 77.6 20.2
    0 4 2.4 71.1 26.5
    0 5 2.2 81.1 16.7
    0 6 2.6 76.9 20.5
    0 7 3.3 71.1 25.6
    0 8 1.9   78 20.1
    1 0 1.9 79.3 18.8
    1 1 1.5 80.4 18.1
    1 2   1 83.9   15
    1 3 1.1 80.7 18.2
    1 4 1.3 81.5 17.2
    1 5  .6 86.2 13.3
    1 6 1.7 79.6 18.7
    1 7  .8 78.1 21.1
    1 8 1.1 82.9 16.1
    end
    label values gender gender
    label def gender 0 "Male", modify
    label def gender 1 "Female", modify
    label values category_num category_num
    label def category_num 0 "NCG (never)", modify
    label def category_num 1 "NCG (&lt;weekly)", modify
    label def category_num 2 "NCG (weekly+)", modify
    label def category_num 3 "Low-intensity CG never)", modify
    label def category_num 4 "Low-intensity CG (&lt;weekly)", modify
    label def category_num 5 "Low-intensity CG (weekly+)", modify
    label def category_num 6 "High-intensity CG (never)", modify
    label def category_num 7 "High-intensity CG (&lt;weekly)", modify
    label def category_num 8 "High-intensity CG (weekly+)", modify
    
    set scheme s1color
    
    graph bar Healthy Unhealthy Dead , over(category_num, label(angle(45))) by(gender, note("")) stack ///
    legend(order(1 "Healthy" 2 "Unhealthy" 3 "Dead") rows(1) size(small) region(lcolor(white))) ///
    ylabel(0 "0" .2 "20%" .4  "40%" .6 "60%" .8 "80%" 1 "100%", noticks nogrid angle(0) labsize(small)) ///
    graphregion(color(white)) ///
    blabel(bar, color(white) position(inside) format(%2.1f))
    
    rename (Healthy Unhealthy Dead) (percent1 percent2 percent3)
    
    reshape long percent, i(gender category_num) j(which)
    label def which 1 Healthy 2 Unhealthy 3 Dead
    label val which which
    
    tabplot category_num which [iw=percent], height(0.63) by(gender, note("percent")) separate(which) bar1(blcolor(blue) bfcolor(blue*0.5)) bar2(blcolor(blue) bfcolor(blue*0.1)) bar3(bcolor(red)) showval(offset(0.2) format(%2.1f)) horizontal ytitle("") xtitle("") xscale(r(0.7 3.4)) subtitle(, fcolor(none)) name(G1, replace)
    
    tabplot category_num which if which &gt; 1 [iw=percent], height(0.63) by(gender, note("percent")) separate(which) bar1(blcolor(blue) bfcolor(blue*0.1)) bar2(bcolor(red)) showval(offset(0.2) format(%2.1f)) horizontal ytitle("") xtitle("") xscale(r(0.7 2.4)) subtitle(, fcolor(none)) name(G2, replace)
    
    drop if which == 1
    tabplot category_num gender [iw=percent], height(0.63) by(which, note("percent")) separate(gender) bar1(blcolor(blue) bfcolor(blue*0.1)) bar2(bcolor(red)) showval(offset(0.2) format(%2.1f)) horizontal ytitle("") xtitle("") xscale(r(0.7 2.4)) subtitle(, fcolor(none)) name(G3, replace)
    
    separate percent, by(which)
    graph dot (mean) percent2 percent3, over(gender) over(category_num)  asyvars marker(1, ms(Oh)) marker(2, ms(+)) legend(order(2 "Dead" 1 "Unhealthy")) linetype(line) lines(lc(gs12) lw(vthin)) t1title(percent) name(G4, replace)
    
    drop percent2 percent3
    separate percent, by(gender)
    
    graph dot (mean) percent0 percent1, over(which) over(category_num)  asyvars marker(1, ms(Oh)) marker(2, ms(+))  linetype(line) lines(lc(gs12) lw(vthin)) t1title(percent) name(G5, replace) legend(order(2 "Female" 1 "Male"))

    Click image for larger version

Name:	mehri_G1.png
Views:	1
Size:	38.9 KB
ID:	1623132

    Click image for larger version

Name:	mehri_G2.png
Views:	1
Size:	36.4 KB
ID:	1623133

    Click image for larger version

Name:	mehri_G3.png
Views:	1
Size:	34.3 KB
ID:	1623134

    Click image for larger version

Name:	mehri_G4.png
Views:	1
Size:	50.2 KB
ID:	1623135

    Click image for larger version

Name:	mehri_G5.png
Views:	1
Size:	53.6 KB
ID:	1623136

    Comment


    • #3
      Thanks for your great solution! The plot couldn't be any better. I please need help with the below issues:
      For the second plot, I got an error of "gt; invalid name".
      I wonder to know how the graph's background color could be removed which appears when the plots are inserted into Microsoft Word?
      Also, I wonder to know how one could show significant results by adding symbols such as *** around the bars (assuming that the first bar is the reference group).
      Thanks,
      Nader
      Last edited by Nader Mehri; 13 Aug 2021, 08:15.

      Comment


      • #4
        Sorry,

        Code:
         
         if which &gt; 1
        is the forum's software producing HTML which should be

        Code:
         
         if which > 1
        MS Word? Well, I am using Stata scheme s1color which has no background colour. If you are using scheme s2color, I recommend otherwise.

        Stars for significance? To cite Blazing Saddles out of context, "Son; You're on your own".

        Comment


        • #5
          LoL; Thanks.

          Comment

          Working...
          X