Skip to content

Instantly share code, notes, and snippets.

@isaacgeng
Last active February 25, 2024 03:28
Show Gist options
  • Save isaacgeng/630f0147f084a9efc0a69d09f98fba6b to your computer and use it in GitHub Desktop.
Save isaacgeng/630f0147f084a9efc0a69d09f98fba6b to your computer and use it in GitHub Desktop.
Bar plot with custom bar label
// see https://www.statalist.org/forums/forum/general-stata-discussion/general/33124-adding-other-data-as-a-label-to-bar-graph
sysuse census, clear
collapse (sum) pop pop5_17, by(region)
generate pr_5_17 = round(pop5_17/pop, 0.01)*100
replace pop = pop/1000000
rename pop pop_millions
graph bar (asis) pop_millions, over(region) ytitle("Population (In Millions)") blabel(bar)
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
.Graph.plotregion1.barlabels[`i'].text[1]=`"`=string(`=pr_5_17[`i']',"%6.1f")'% of `=string(`=pop_mil[`i']',"%6.1f")'mln"'
}
.Graph.drawgraph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment