Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Stacked bar graph with twoway__histogram_gen
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Stacked bar graph with twoway__histogram_gen
Date
Wed, 7 Mar 2012 18:43:35 +0000
I wouldn't do this. You want to subdivide a histogram according to two
categories.
My recipe for such cases is
1. Lay down a histogram for everything.
2. Lay down a histogram for one category on top.
Then the difference is just the histogram for the other category, stacked.
Here is some example code:
sysuse auto
twoway histogram mpg , width(2) start(10) barw(1.98) freq
bfcolor(red*0.6) blcolor(gs12)
|| histogram mpg if foreign , width(2) barw(1.98) start(10) freq
blcolor(gs12) bfcolor(blue*0.6)
legend(order(1 "Domestic" 2 "Foreign") col(1) ring(0) pos(1))
See also
Cox, N.J. 2009.
Stata tip 78: Going gray gracefully: Highlighting subsets and
downplaying substrates
The Stata Journal 9(3): 499-503
On Wed, Mar 7, 2012 at 6:28 PM, Stefano Lombardi
<[email protected]> wrote:
> I have a problem in plotting a stacked bar graph. I have read some
> documentation on the Internet and I am really close to obtain what I need,
> but still I cannot manage to solve one issue (I haven't found any similar
> problem on the Statalist archive..)
>
> In practice, I have one duration variable in months (overall survival, "os")
> which as usual can be either censored or uncensored (the failure dummy is
> "status_os"; 1 = os is uncensored). I need to create a stacked bar graph
> with different colors for censored and uncensored data (blue and red,
> respectively). My code is the following:
>
> use "epd0", clear
> twoway__histogram_gen os if status_os==0, gen(freq_1 x, replace) ///
> freq start(0) width(5)
> drop if missing(x)
> keep freq_1 x
> save f1, replace
>
> use "epd0", clear
> twoway__histogram_gen os if status_os==1, gen(freq_2 x, replace) ///
> freq start(0) width(5)
> drop if missing(x)
> keep freq_2 x os
>
> merge 1:1 x using f1
> mvencode freq_1 freq_2, mv(. = 0)
>
> egen freq = rowtotal(freq_1 freq_2)
>
> twoway ///
> (bar freq_1 x, barw(5) bcolor(red)) ///
> rbar freq_2 freq x, barw(5) bcolor(blue) ///
> legend(order(1 "Uncensored" 2 "Censored")) ///
> xtitle("`: var label os'") ytitle(Frequency)
>
> Everything looks perfect but for one thing. In one bin there is a blank gap
> between the lower red bar (which counts for two observations) and the upper
> blue bar (other two observations).
> Actually -- also by checking the previously generated frequencies -- the
> lower bar should correspond to three observations, hence the blank gap
> should be red.
>
> Can you suggest any solution? It may be a coincidence, but that bin is the
> only one where there should be plotted more uncensored (red) data than
> censored (blue) ones.
*
* 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/