Announcement

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

  • Graph bar axis and labels issues

    I am trying to create a graphbar using

    Code:
    graph bar (percent) if account_formal==1, ///
    over(female) over(account_formal) over(year) ///
    title("Owning a Bank Account by Gender") ///
    subtitle("Years 2011, 2014, and 2017") ///
    ytitle("Percent of Individuals Owning a Bank Account") ///
    blabel(bar,position(outside) size(vsmall)) ylabel(, labsize(vsmall)) ///
    legend(rows(1) ring(6) position(6) label(1 "Men") label(2 "Women")) ///
    note("Source: World Bank FINDEX Database")
    I get the following:
    Click image for larger version

Name:	Graph1 - draft.jpg
Views:	1
Size:	30.2 KB
ID:	1684326



    I am not able to remove the number of “1” on the x axis, where I only want to keep the years (2011, 2014, and 2017). 1 here stands for “owning a bank account” (dummy variable with 0 being does not own a bank account).

    Moreover, I would like to reduce the numbers labelled on the graph bar with only two decimals after comma and add a percentage sign – is this possible?

    I would highly appreciate your help!

    Best,
    Julie
    Attached Files

  • #2
    I think you need to remove
    Code:
    over(account_formal)
    from your command - it is not needed because you have already included
    Code:
    if account_formal==1
    so the over() option has no effect.

    I think you want
    Code:
    blabel(bar,position(outside) size(vsmall) format(%9.2f))
    to get 2 decimal digits. There is no simple way to include a percent sign - Stata number formats are generally not as rich as Excel number formats, if that's what you're used to.

    Comment


    • #3
      Dear William,

      Thank you very much for your response!

      I removed
      Code:
      over(account_formal
      and it unfortunately gives me the following (completely ignoring the legend in the code):
      Click image for larger version

Name:	Graph1 - draft2.jpg
Views:	1
Size:	58.8 KB
ID:	1684342


      Moreover, when trying
      Code:
      format(%9.2f))
      I receive the following message on Stata:
      option format() not allowed r(198);
      Would highly appreciate your help again!

      Comment


      • #4
        Hello Julie,

        Can you post the complete code for your graph? I am guessing that the error you received has to do with how you told it to format. I just ran the code William suggested and it worked for my random dataset.

        Code:
        graph bar  (percent) if m3==1, ///
        over(gender) over(m3) over(avg_pedc) blabel(bar,position(outside) size(vsmall) format(%9.2f))

        Comment


        • #5
          Hello Meghan,

          Thank you for your response!

          Here is my complete code:
          Code:
          graph bar (percent) if account_formal==1, ///
          over(female) over(year) ///
          title("Owning a Bank Account by Gender") ///
          subtitle("Years 2011, 2014, and 2017") ///
          ytitle("Percent of Individuals Owning a Bank Account") ///
          blabel(bar,position(outside) size(vsmall)  format(%9.2f)) ylabel(, labsize(vsmall)) ///
          legend(rows(1) ring(6) position(6) label(1 "Men") label(2 "Women")) ///
          note("Source: World Bank FINDEX Database")
          Many thanks and best regards,
          Julie

          Comment


          • #6
            Dear Meghan and William,

            I retried and noticed that I set my parentheses wrongly. The format command works now. Thank you very much both of you!

            Yet, unfortunately, the legend is still gone, do you by any chance know why this is the case?


            Thank you and kind regards,
            Julie
            Attached Files
            Last edited by Julie Illoul; 04 Oct 2022, 18:51.

            Comment


            • #7
              I have never used the ring() option and have no idea how it works, but I wonder if it is really necessary, or if reducing it from ring(6) to ring(5) would help, since eliminating the account_formal line of 1's. reduced the number of rings.

              Comment


              • #8
                In #6 just one outcome variable is being shown, the percentage calculated by the command, so no legend is shown, at least by default. You need to define value labels for female to get a more informative display.

                Comment


                • #9
                  Originally posted by William Lisowski View Post
                  I have never used the ring() option and have no idea how it works, but I wonder if it is really necessary, or if reducing it from ring(6) to ring(5) would help, since eliminating the account_formal line of 1's. reduced the number of rings.
                  No, the only value of ring() which would make a difference to the way her graph looks is if it were set to 0 (which would bring the legend into the plot region). Positive values of ring only have an ordinal interpretation, so if she had something else that had another positive ring value, Stata would compare the two to determine which to place further away from the plot region (the one with the higher value).

                  Comment


                  • #10
                    Thank you all so much! It worked!

                    I defined value labels of female as follows:
                    Code:
                    label variable female "the gender of the individual"
                    label define femalex 0 "Men" 1 "Women"
                    label values female femalex
                    Here, I got the following graphbar:
                    Click image for larger version

Name:	Graph1.jpg
Views:	1
Size:	32.2 KB
ID:	1684490


                    Then, I found the
                    Code:
                    asyvars
                    command online, which added the legend (with colors):
                    Click image for larger version

Name:	Graph2.jpg
Views:	1
Size:	28.5 KB
ID:	1684491


                    My final code is the following:
                    Code:
                    graph bar (percent) if account_formal==1, ///
                    over(female) over(year) ///
                    title("Owning a Bank Account by Gender") ///
                    subtitle("Years 2011, 2014, and 2017") ///
                    ytitle("Percent of Individuals Owning a Bank Account") ///
                    blabel(bar,position(outside) size(vsmall)  format(%9.2f)) ylabel(, labsize(vsmall)) ///
                    asyvars ///
                    note("Source: World Bank FINDEX Database")
                    Thank you again and best regards,
                    Julie

                    Comment

                    Working...
                    X