Announcement

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

  • Customize graph bar by or over option

    Hello everyone,

    I have a problem with my graphics.
    Objective: a bar chart that shows the differences in the correlation between income and obesity depending on gender.
    Problem: I don't want the labeling of the Y axis twice for each chart but one below the whole plot.
    1st -by option
    graph bar X1 X2, over(incomeq, relabel(1"1" 2" 2" 3"3" 4"4" 5"5") ) by(sex, title("Proportion of obesity in income quintiles", box)) percent ytitle("Percentage") b1title("Income Quintiles") legend(order(1 "Under/Normal Weight" 2 "Overweight/Obesity") col(1) pos(2)) leg(size(small)) scheme(s2color) blabel(total, format(%9. 2f)) bargap(5)

    With the -over option it works, but there I don't know how to put the descriptions "man" "woman" at the top of the diagrams, I also prefer the by option for the display if this is possible.



    2nd -over option
    graph bar X1 X2, over(incomeq, relabel(1"1" 2" 2" 3"3" 4"4" 5"5") ) over(sex) title("Proportion of obesity in income quintiles", box) percent ytitle("Share in percent") b1title("Income quintiles") legend(order(1 "Under/normal weight" 2 "Overweight/obese") col(1)) leg(size(small)) scheme(s2color) blabel(total, format(%9. 2f)) bargap(5)

    3. problem: regardless of the choice of option, how can I customize the percentages so that they are easier to read.

    thanks for your help.

  • #2
    i hope you understand my problem without attaching the graphic
    Last edited by Julian Ruf; 04 May 2024, 01:31.

    Comment


    • #3
      i hope you understand my problem without attaching the graphic
      Sorry, but no. No data example and no graph here. Sometimes people can read code and imagine the result, but hoping for that may be optimistic here.

      But it is easy for you to make the question easier for us. I think your graphs show 10 means, so tell us what they are.

      Code:
      collapse X1 X2, by(incomeq sex) 
      
      dataex 

      Comment


      • #4
        Click image for larger version

Name:	By.png
Views:	1
Size:	212.7 KB
ID:	1752306
        Here is an example of the -By option.
        I want to merge the X-axis titles into one title in the middle of the axis.
        Last edited by Julian Ruf; 05 May 2024, 01:40.

        Comment


        • #5
          Thanks for the graph. You didn't give a data example and although with some work I can construct a simple one that leads (I think) to a good graph, I suspect that you don't have this data layout. It is an expectation here that you do the work of showing us a proper data example. Please see https://www.statalist.org/forums/help#stata

          Incidentally your use of the default scheme s2color suggests that are not using Stata 18. See the FAQ Advice on why you should explain if you are not using the latest version. I recommend use of almost any scheme but s2color. Use of the default scheme is often mocked on social media, often by people who don't seem to know that it was just the default from Stata 8 to Stata 17 and that other schemes were always available. s1color served me well for many years as a starting point.

          I have comments on quite different levels, in rising order of importance (although in presentation as anywhere else details matter and some supervisor or examiner or reviewer can easily insist that you shouldn't do that!).

          1. Although people (academics too) with English as first language are often sloppy about this, proportion and percentage are not best treated as synonyms. A proportion is on the scale 0 to 1 and a percentage is on the scale 0 to 100. Although 12.34% should be understood as just a way to display 12.34/100 = 0.1234, I recommend keeping this verbal distinction. However, I can't comment on German usage.

          2. Whether it is over the top to show 2 decimal places is open. My code shows that you have a choice on rounding.

          3. You have pairs of percentages that add to 100 and show bars for both. I see little or no point in doing that.

          4. I don't see that a by() option is the best choice. You want to compare males and females and across income quintiles. That is best done when small differences are next to each other.

          If I have your two categories on weight and on sex the wrong way round, you know how to reverse them.

          The code gives two graphs. I show the second, which I think is better and more likely to satisfy those examiners and reviewers.

          Code:
          clear
          input Y X1 X2
          1   94.62 95.28
          2   91.43 91.53
          3   86.89 85.63
          4   85.16 85.44
          5   79.12 80.00
          end
          
          label var X1 "males"
          label var X2 "females"
          label var Y "income quintiles"
          
          graph bar (asis) X1 X2, over(Y) blabel(bar, format(%2.1f)) name(G1, replace) ///
          ytitle("% under or normal weight") b1title(Income quintiles)
          
          replace X1 = 100 - X1
          replace X2 = 100 - X2
          
          graph bar (asis) X1 X2, over(Y) blabel(bar, format(%2.1f)) name(G2, replace) ///
          ytitle("% over weight or obese") b1title(Income quintiles)
          Click image for larger version

Name:	obese_G2.png
Views:	1
Size:	39.4 KB
ID:	1752309

          Last edited by Nick Cox; 05 May 2024, 03:42.

          Comment


          • #6
            Okey thanks for the tip about using the schema and the roundings.I also like your approach. Here is the code for the graphic shown above.

            graph bar X1 X2, over(Y, relabel(1"1" 2" 2" 3"3" 4"4" 5"5") ) by(V, title("Titel ", box)) percent ytitle("Percentage") b1title("Y") legend(order(1 "X1" 2 "X2") col(1) pos(2)) leg(size(small)) scheme(s2color) blabel(total, format(%9. 2f)) bargap(5)

            Here I would like to have the title of the Y axis (Y) to a title in the middle under both graphs, is that possible?

            Comment


            • #7
              I can't run your code. There is no V variable in any data example.

              Otherwise, I think you're ignoring my advice on several fronts. You're totally free to do that, but equally I won't write code to make a graph worse. I suggest that you explore the help for title options to find ways to get what you want.

              Comment

              Working...
              X