|
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: adding info to bar graph
works great -- thanks Nick
Rich
Nick Cox wrote:
Here is an independent stab at the problem:
*! 1.0.0 NJC Statalist 9 April 2008
* varname supplied must be 0, 1
* variable in -over- may be string or numeric
program goldbar
version 9
syntax varname(numeric) [if] [in] , over(str) [ * ]
quietly {
marksample touse
count if `touse'
if r(N) == 0 error 2000
capture assert `varlist' == 0 | `varlist' == 1 if
`touse'
if _rc {
di as err "`varlist' contains values other than
0/1"
exit 498
}
gettoken overvar : over, parse(" ,")
capture confirm numeric var `overvar'
if _rc {
tempvar encoded
encode `overvar' if `touse', gen(`encoded')
local overvar `encoded'
}
levelsof `overvar' if `touse', local(levels)
local i = 1
foreach l of local levels {
count if `touse' & `varlist' == 1 & `overvar' ==
`l'
local num = r(N)
count if `touse' & `overvar' == `l'
local den = r(N)
local la : label (`overvar') `l'
local xla `xla' `i' `" "`la'" "`num'/`den'" "'
local ++i
}
if strpos("`over'", ",") == 0 local xla , relabel(`xla')
else local xla relabel(`xla')
}
graph bar `varlist' if `touse', over(`over' `xla') `options'
end
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Richard
Goldstein
Sent: 09 April 2008 16:13
To: [email protected]
Subject: Re: st: adding info to bar graph
That appears to work great -- thanks
Scott Merryman wrote:
Something like this?
sysuse auto,clear
levelsof rep, local(levels)
foreach l of local levels {
qui count if fore == 1 & rep == `l'
local n = r(N)
qui count if rep == `l'
local d = r(N)
local mytext "`mytext' 5 `l' "`n'/`d'" "
}
egen percent = mean(100 * foreign), by(rep78)
egen total = sum(1), by(rep78)
twoway bar percent rep78,barw(0.5) text(`mytext', box bc(white) )
ylabel(,angle(h)) ytitle(Percent Foreign)
Scott
On Wed, Apr 9, 2008 at 7:02 AM, Richard Goldstein
<[email protected]> wrote:
I want to produce a bar graph where the numeric variable is a 0/1
variable
(so the means are proportions) over a grouping variable -- that part
is
easy. I would like to automatically add, either as a bar label or
under the
group id (e.g., the "5", etc. if I typed -graph bar foreign,
over(rep78)-
for the auto data), the numerator and denominator for this proportion
(mean)
-- is there a way to do that?
Note that I do know that I can do this with graph edit; however, I
need to
produce dozens of these graphs so I want an automatic way to do it.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/