Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: bihistogram


From   "Austin Nichols" <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: bihistogram
Date   Tue, 26 Aug 2008 13:54:28 -0400

See also
http://www.stata.com/support/faqs/graphics/gph/graphdocs/twbar3.html
but I would fix the negative labels--no reason not to make them
positive in both directions.

Also try this program out:

*! bihist 1.0 26 Aug 2008 Austin Nichols
prog bihist
version 8.2
syntax varname [fw aw pw], by(varname) [*]
qui insp `by'
if r(N_unique)!=2 {
 di as err "Variable `by' should have two distinct values"
 err 198
 }
loc v `varlist'
tempvar x y
twoway__histogram_gen `v',gen(`x' `y') `options'
loc s=r(start)
loc w=r(width)
loc b=r(bin)+1
qui replace `x'=.
qui forv i=1/`b' {
replace `x'=`s'+`i'*`w'/2 if `v'>`s'+(`i'-1)*`w'&`v'<`s'+(`i')*`w'
}
qui replace `y'=(`v'<.)
loc xl "`:var lab `v''"
preserve
collapse (sum) `y' [`weight'`exp'], by(`by' `x') fast
qui levelsof `by', loc(bl)
tokenize `bl'
su `y', meanonly
loc m=r(max)
replace `y'=-`y' if `by'==`1'
loc o=int(log10(`m'))
loc M=ceil(`m'/10^`o')*10^`o'
loc p=min(length("`:di `M'/3'"),length("`:di `M'/4'"))
loc l "ylabel("
forv i=-`p'/`p' {
 loc l `"`l' `=`M'*`i'/`p'' "`=`M'*abs(`i')/`p''" "'
}
loc l `"`l')"'
loc l `"`l' yti(Freq in `=trim("`: di %20.0gc 10^`o''")'s)"'
loc l `"`l' xti(`xl')"'
loc l `"`l' leg(label(1 "`:var lab `by'' = `:label (`by') `1''")"'
loc l `"`l' label(2 "`:var lab `by'' = `:label (`by') `2''"))"'
tw bar `y' `x' if `by'==`1' || bar `y' `x' if `by'==`2', `l'
end

sysuse auto, clear
bihist mpg, by(for)

On Tue, Aug 26, 2008 at 7:50 AM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> A bihistogram -- I didn't know before searching -- is a dual histogram
> with one group's frequencies shown as negative.
>
> Rajesh found nothing, but -search histogram- points to this:
>
> SJ-5-2  gr0014  . . . . . . . Stata tip 20: Generating histogram bin
> variables
>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . D. A.
> Harrison
>        Q2/05   SJ 5(2):280--281                                 (no
> commands)
>        tip illustrating the use of twoway__histogram_gen for
>        creation of complex histograms and other graphs or tables
>
> The text is accessible to all via
> <http://www.stata-journal.com/sjpdf.html?articlenum=gr0014>
>
> That includes a worked example.
>
> Here is another:
>
> sysuse auto, clear
> twoway__histogram_gen mpg if foreign, width(2) start(10) freq ///
> gen(freq_foreign mpg_foreign)
> twoway__histogram_gen mpg if !foreign, width(2) start(10) freq ///
> gen(freq_domestic mpg_domestic)
> replace freq_foreign = -freq_foreign
> twoway bar freq_foreign mpg_foreign, barw(2) || ///
> bar freq_domestic mpg_domestic, barw(2) ///
> legend(order(1 "Foreign" 2 "Domestic"))
>
> Nick
> n.j.cox@durham.ac.uk
>
> Rajesh Tharyan
>
> Is it possible to produce a bihistogram in stata? Nothing turns up on
> findit
> or search. I was trying to write a program to do that. But can one get
> stata
> to produce histogram with -ve frequencies?
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index