Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: RE: Graph bar with label wtih % symbol at the top of each bar

From   Nick Cox <>
Subject   Re: st: RE: RE: Graph bar with label wtih % symbol at the top of each bar
Date   Fri, 11 May 2012 01:08:01 +0100

An alternative is to switch to -twoway bar-. There is a little work in
getting a dataset of results first, but it is not too difficult to
make progress fairly quickly.

set obs 100
forval j = 1/3 {
	gen var`j' = runiform() < 0.`j'

collapse (mean) var*
gen id = 1
reshape long var, i(id)
gen label = string(var, "%3.2f") + "%"

twoway bar var _j, barw(0.8) base(0) || scatter var _j, ms(none)
mla(label) mlabpos(12) legend(off) xla(1/3) xtitle("")

On Fri, May 11, 2012 at 12:48 AM, Phil Clayton
<> wrote:

> Just a note of caution - the bars are numbered from 1 to n, left to right. In my example below those numbers corresponded exactly with the levels of rep78, but that often won't be the case. So you need to be careful that you label the correct bars.

On 11/05/2012, at 9:39 AM, Phil Clayton wrote:

>> On 11/05/2012, at 8:21 AM, David Radwin wrote:
>>> ... I don't think you can get percentage symbols to show as bar labels without the graph editor
>> You can, however, invoke the graph editor from within your do-file to automate the process of changing the bar labels to percentages. Here's a simple example:
>> ------- begin example -------
>> sysuse auto, clear
>> graph bar (mean) foreign, over(rep78) blabel(bar) ///
>>       ytitle(% foreign) ylab(0 .2 "20" .4 "40" .6 "60" .8 "80" 1 "100")
>> * for each level of rep78, replace the proportion in the bar label with a %
>> foreach rep of numlist 1/5 {
>>       sum foreign if rep78==`rep', meanonly
>>       local lab: di %1.0f 100*r(mean)
>>       _gm_edit .Graph.plotregion1.barlabels[`rep'].text = {}
>>       _gm_edit .Graph.plotregion1.barlabels[`rep'].text.Arrpush `lab'%
>> }
>> * now re-draw the graph with the new labels
>> graph display Graph
>> -------end example -------
>> Of course, if you gave your graph a name then you should use that name rather than Graph in the _gm_edit and -graph display- commands.
>> How to determine the commands to use? If you record your edits using the GUI graph editor, you can open the recording file (which is just a text file) and see what's been recorded.

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index