Hi Stata community,
I am using the data set below to create bar charts.
This is the code that I am using to create the bar charts.
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.
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
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)

Comment