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]

st: Stacked bar graph with twoway__histogram_gen

From   Stefano Lombardi <[email protected]>
To   [email protected]
Subject   st: Stacked bar graph with twoway__histogram_gen
Date   Wed, 07 Mar 2012 19:28:08 +0100

Dear Statalisters,

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.

Thank you very much for any help,

Stefano Lombardi
*   For searches and help try:

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