Announcement

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

  • Adding a percent sign for values in a bar chart

    Hi Stata community,

    I am using the data set below to create bar charts.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long macroregion float(year wbl_year_per_wbl wbl_year_per_no_wbl) str10(wbl_per wbl_no_per)
    6 1  56.33803   2.71372 "56.3%" "2.7%" 
    5 1 12.519562  9.509666 "12.5%" "9.5%" 
    3 1 .46948355 2.4584596 "0.5%"  "2.5%" 
    1 1  23.63067  19.38621 "23.6%" "19.4%"
    2 1 .15649453 16.267761 "0.2%"  "16.3%"
    7 1         0 12.185534 "0.0%"  "12.2%"
    4 1  6.885759  37.47865 "6.9%"  "37.5%"
    1 2 25.638407  19.30821 "25.6%" "19.3%"
    3 2 1.7364658   2.70988 "1.7%"  "2.7%" 
    2 2  .2042901  16.70188 "0.2%"  "16.7%"
    5 2 18.692543 10.428654 "18.7%" "10.4%"
    6 2  45.65884  2.496096 "45.7%" "2.5%" 
    7 2         0 11.923472 "0.0%"  "11.9%"
    4 2  8.069459 36.431812 "8.1%"  "36.4%"
    4 3  3.976436  34.95898 "4.0%"  "35.0%"
    2 3  .0981836  19.46533 "0.1%"  "19.5%"
    6 3  30.73147   2.65897 "30.7%" "2.7%" 
    3 3 15.954836   2.73361 "16.0%" "2.7%" 
    5 3  39.17526 10.894038 "39.2%" "10.9%"
    1 3 10.014728 17.241875 "10.0%" "17.2%"
    7 3  .0490918 12.047194 "0.0%"  "12.0%"
    end
    label values macroregion macroregion
    label def macroregion 1 "Bay Area", modify
    label def macroregion 2 "Central/Mother Lode", modify
    label def macroregion 3 "Inland Empire/Desert", modify
    label def macroregion 4 "Los Angeles/Orange County", modify
    label def macroregion 5 "North/Far North", modify
    label def macroregion 6 "San Diego/Imperial Counties", modify
    label def macroregion 7 "South Central Coast", modify
    label values year year
    label def year 1 "2021-2022", modify
    label def year 2 "2022-2023", modify
    label def year 3 "2023-2024", modify
    This is the code that I am using to create the bar charts.

    Code:
        
    * First graph: 2021–2022
    graph hbar wbl_year_per_wbl wbl_year_per_no_wbl  if year == 1, ///
        over(macroregion, label(labsize(vsmall))) ///
        blabel(bar, size(vsmall) format(%9.2fc)) ///
        bar(1, color("31 42 68")) ///
        bar(2, color("118 136 26")) ///
        bar(3, color("209 204 189")) ///
        title("2021–2022") ///
        ytitle("Percentage") ///
        ylabel(, format(%9.0fc)) ///
        yscale(range(0 70)) ///
        legend(label(1 "Work-based learning") label(2 "Not work-based learning"))
    graph save year_region_wbl_2021.gph, replace
    
    * Second graph: 2022–2023
    graph hbar wbl_year_per_wbl wbl_year_per_no_wbl if year == 2, ///
        over(macroregion, lab(nolab)) ///
        blabel(bar, size(vsmall) format(%9.2fc)) ///
        bar(1, color("31 42 68")) ///
        bar(2, color("118 136 26")) ///
        bar(3, color("209 204 189")) ///
        title("2022–2023") ///
        ytitle("Percentage") ///
        ylabel(, format(%9.0fc)) ///
        yscale(range(0 50)) ///
        legend(label(1 "Work-based learning") label(2 "Not work-based learning"))
    graph save year_region_wbl_2022.gph, replace
    
    * Third graph: 2023–2024
    graph hbar wbl_year_per_wbl wbl_year_per_no_wbl  if year == 3, ///
        over(macroregion, lab(nolab)) ///
        blabel(bar, size(vsmall) format(%9.2fc)) ///
        bar(1, color("31 42 68")) ///
        bar(2, color("118 136 26")) ///
        bar(3, color("209 204 189")) ///
        title("2023–2024") ///
        ytitle("Percentage") ///
        ylabel(, format(%9.0fc)) ///
        yscale(range(0 40)) ///
        legend(label(1 "Work-based learning") label(2 "Not work-based learning"))
    graph save year_region_wbl_2023.gph, replace
    
    * Combine the three graphs with shared legend
    grc1leg year_region_wbl_2021.gph year_region_wbl_2022.gph year_region_wbl_2023.gph, ///
    rows(1) ///
    l1(Macroregions) ////
    name(combined_graph, replace)
    I am trying to add a percent sign next to each value. However, Stata says I can't use strings with the hbar command. Does anyone have an alternative approach? Thanks.

  • #2
    Code:
    graph hbar wbl_year_per_wbl wbl_year_per_no_wbl  if year == 1, ///
        over(macroregion, label(labsize(vsmall))) ///
        blabel(bar, suffix("%") size(vsmall) format(%9.2fc)) ///
        bar(1, color("31 42 68")) ///
        bar(2, color("118 136 26")) ///
        bar(3, color("209 204 189")) ///
        title("2021–2022") ///
        ytitle("Percentage") ///
        ylabel(, format(%9.0fc)) ///
        yscale(range(0 70)) ///
        legend(label(1 "Work-based learning") label(2 "Not work-based learning"))
    and similarly in the other graphs.

    Comment


    • #3
      @Clyde Schechter Thanks for the suggestion. I tried the suffix option and an error message pops up. It says "option suffix() not allowed" which is interesting since the Stata manual says otherwise. Do you know why this error popped up? For the record, I am using StataNow/MP 18.5

      Comment


      • #4
        I don't know why you are getting this. I ran my code on your example data on my own setup (Stata MP4 version 19.5, Windows 10 64-bit) and it both ran without any error message and produced the result you are looking for.

        Sometimes, when people add another suboption to an option that already has other suboptions specified, they mistakenly add an additional comma--that will cause Stata's parser to throw the kind of error message you got. Another possibility is that instead of putting the -suffix()- suboption inside the -blabel()- option, you might have simply put it as an option in its own right. That, too, would lead to the error message you got. But if you typed it exactly as I showed it, I can think of no reason for a properly installed Stata to do this.

        If you really did use the code exactly as shown in #2, then I would suggest:
        1. Be sure your Stata installation is fully up to date by running -update all, force-.
        2. If that doesn't work, reboot your computer and try again.
        3. If that doesn't work, uninstall Stata, re-install it, and then -update- the new installation.
        If none of that helps, then I think you need to send your example to Stata Technical Support for help. In doing that, don't forget to provide them with the details of your setup. The easiest way to do that is to copy the results of running -about-.

        Added: I just tried this in version 17, and I get the -option suffix() not allowed- error message. So apparently this suboption was introduced later than version 17. I no longer have version 18 (nor 18.5) on my system, so I can't be sure if perhaps the problem is just that it is not supported in your version of Stata. But the fact that you say that "the Stata manual says otherwise" suggests that this is not your issue. It would be interesting if the -suffix()- option was introduced sometime during the Stata 18.5 era and you have an incompletely updated installation, one whose documentation files mention -suffix()- but whose -graph- routines do not support it. If so, my suggestion above to update your installation will solve your problem. If not, try 2, and 3, and if those also fail, I think you need Stata Tech Support.
        Last edited by Clyde Schechter; 03 Oct 2025, 17:04.

        Comment


        • #5
          The suboption suffix() in blabel() is added in StataNow 19.5 and Stata 19. It is not supported in StataNow 18.5. But I believe StataNow 18.5 is eligible for free uprade to StataNow 19.5. Please contact us at [email protected] for assistance.
          Last edited by Hua Peng (StataCorp); 03 Oct 2025, 18:21.

          Comment


          • #6
            @Hua Peng is naturally correct. So how else to reconcile the small puzzlement in #1 to #4?

            My guess:

            joseph wells is using 18.5 and telling us that. But I think he must be looking at the online manual at www.stata.com/manuals, which is always up-to-date, and so as of now referring to 19.5. The manuals (PDF documentation) bundled with his copy of Stata should tell him what is possible in 18.5 (in this case by being silent about an option not available). (Or else someone is advising Joseph and subject to the same tiny confusion.)

            Clyde Schechter is using 19.5 and no longer using 18 or 18.5. But Clyde can look at the help pages indexed under

            Code:
            help whatsnewtoc
            to see what was introduced when. And these help pages are also on the web.

            Looking through those help pages one by one can be tedious, so why doesn't StataCorp produce a master document simply documenting what was introduced when?

            I am still guessing, but I surmise that there is a mundane reason and a subtle reason why not.

            That would be quite a lot of work and right now there is always something more interesting or more important to do.

            More subtle: In many cases, commands, functions and other functionality have changed in minor or even major details since they were first introduced. So, just knowing that a command was first introduced in a particular version is only part of the information that people, especially Stata programmers. may need.

            Comment


            • #7
              The issue has been resolved. My IT department upgraded my Stata edition to version 19.5 and Clyde's suggestion works perfectly. Thank you @Clyde Schechter

              Regarding the disconnect between the manual and the versions of Stata, Nick is absolutely correctly. When I was trying to implement Clyde's suggestion, I was using version 18.5 but I was looking at the online manual which is the up-to-date. Thank you @Nick Cox

              Moral of the story. Always stay current with your Stata license to gain access to the amazing features.

              Comment

              Working...
              X