Bookmark and Share

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: twoway stacked bar charts


From   "Impavido, Gregorio" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: twoway stacked bar charts
Date   Wed, 12 Mar 2014 17:55:36 +0000

Nick, Yes. Your understanding is correct: it is the equivalent of -graph bar, stack- but with the possibility of overlaying something else. 

The complication (as you rightly point out) stems from the fact that one needs to sort out positive from negative values to stack. With only positive or only negative values the problem is trivial.

Thank you for your suggestion.  I will look at -slideplot- next.

Gregorio

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Wednesday, March 12, 2014 1:25 PM
To: [email protected]
Subject: Re: st: twoway stacked bar charts

As I understand it, you want to stack components upwards from zero when positive and downwards when negative and to put a line plot on top. In principle that's just [!] a series of -twoway- calls but indeed sufficiently complicated in practice to demand a program. Your chance of StataCorp implementing this as a special command I rate as zero after prolonged inspection of tea leaves, entrails, etc., etc.

Whenever I've done this I've reached for -twoway rbar-. There is a program -slideplot- (SSC) which has precisely one very loyal fan, but otherwise I'm not fond of it. Nevertheless it shows how I approached the most similar problem I can recall.

Nick
[email protected]


On 12 March 2014 14:27, Impavido, Gregorio <[email protected]> wrote:
> The following stems from my frustration that -graph bar- does not allow for overlaying. I noticed few posts on the same subject so I presume my same interest is shared, but no actual solution to the problem (until STATA implements it (will it?)).
>
> The example For a set of k variables (the example uses k=4 but it can be increased with obvious diminishing marginal benefits) the code below calculates stacked values that can be overalyed in a -twoway graph bar- (for the economists, an example is the classic chart on contributions to GDP growth). Unfortunately, despite the fact that "stacked bar charts are usually an inferior way to display data" some of us need to produce them routinely.  So my questions to the list:
>
> - Is there an alternative command out there? Could not find one, so grateful for suggestions that are more efficient thing than the ado command below.
>
> - If not, would anybody have suggestions on how to improve the code for -mystack.ado- below so that it can be more useful to the community when I circulate the final version?
>
> Thank you in advance
> Gregorio
>
> *************** begin example here
> clear all
> set more off
>
> ** begin dataset
> set obs 20
> gen n = _n
> local k 4  // change this as needed
> local list ""
> forval i = 1/`k' {
>         gen v`i' = invnormal(uniform())
>         local list = "`list'" + "v`i' "
>         }
> egen total = rowtotal(v*)
> ** end dataset
>
> ** begin mystack.ado
> *! mystack v1.0.0 [email protected] 11mar2014 program mystack
>         version 10.0
>         syntax varlist(min=2 numeric), Generate(string)
>
> /* Perform user error checks */
>
>         if "`generate'" == "" {
>                 di as err "must specify generate option"
>                 exit 198
>         }
>
>         if "`generate'" != "" {
>                 local ct1: word count `varlist'
>                 local save "`varlist'"
>                 local 0 "`generate'"
>                 capture syntax newvarlist
>                 if _rc {
>                         di as err "generate(newvarlist) invalid"
>                         exit _rc
>                 }
>                 local generate "`varlist'"
>                 local varlist "`save'"
>                 local ct2: word count `generate'
>                 if `ct1' != `ct2' {
>                         di as err "number of variables in varlist must equal"
>                         di as err "number of variables in generate(newvarlist)"
>                         exit 198
>                 }
>         }
> /* Cycle through varlist creating tempvar for each variable */
> * split the plane in >0 and <=0
> local varlist_p
> local varlist_n
> foreach var of local varlist {
>         tempvar `var'_p `var'_n
>         qui gen ``var'_p' = `var' if `var'>0
>         local varlist_p = "`varlist_p'" + " ``var'_p'"
>         qui gen ``var'_n' = `var' if `var'<=0
>         local varlist_n = "`varlist_n'" + " ``var'_n'"
>         * di "`varlist_p'"
>         * di "`varlist_n'"
>         }
>
> * build separate varlists for >0 and <=0 local k = 
> wordcount("`varlist'") forvalues i = `k'(-1)1 {
>         tempvar v`i'_sp v`i'_sn
>         qui egen `v`i'_sp' = rowtotal(`varlist_p')
>         qui egen `v`i'_sn' = rowtotal(`varlist_n')
>
>         local oldvar = word("`varlist'",`i')
>         local newvar = word("`generate'",`i')
>         * di "`oldvar'"
>         * di "`newvar'"
>         qui gen `newvar' = .
>         qui replace `newvar' = `v`i'_sp' if `oldvar'>0
>         qui replace `newvar' = `v`i'_sn' if `oldvar'<=0
>
>         local w_p = word("`varlist_p'",`i')
>         * di "`w_p'"
>         local varlist_p = subinword("`varlist_p'","`w_p'","",1)
>         * di "`varlist_p'"
>         local w_n = word("`varlist_n'",`i')
>         * di "`w_n'"
>         local varlist_n = subinword("`varlist_n'","`w_n'","",1)
>         * di "`varlist_n'"
>         }
> end
> ** end mystack.ado
>
> mystack v*, generate(c1 c2 c3 c4)
>
> ** begin graph here
> graph twoway                                                                                                            ///
>         (bar c4 n, pstyle(p4bar) barwidth(0.8))                                                                 ///
>         (bar c3 n, pstyle(p3bar) barwidth(0.8))                                                                 ///
>         (bar c2 n, pstyle(p2bar) barwidth(0.8))                                                                 ///
>         (bar c1 n, pstyle(p1bar) barwidth(0.8))                                                                 ///
>         (scatter total n, pstyle(p6dot))                                                                                ///
>         (line total n, pstyle(p6dot)),                                                                          ///
> title("Figure X. [[Title here]]",                                                                                       ///
>         span ring(7) position(11) size(*0.8))                                                                   ///
> subtitle("({it:[[Subtitle Here]]})",                                                                            ///
>         span ring(6) position(11) size(small))                                                                  ///
> ytitle("[[ytitle here]]")                                                                                               ///
> yscale()                                                                                                                        ///
> ylabel(, ang(h) gstyle(major) glcolor(gs12))                                                                    ///
> xtitle("[[xtitle here]]")                                                                                               ///
> xscale() xlabel(1(1)`=_N')                                                                                              ///
> legend(order(4 3 2 1 5) label(1 "v4") label(2 "v3") label(3 "v2")                                       ///
>         label(4 "v1") label(5 "Total")                                                                          ///
>         cols() rows(2) size(vsmall) symxsize(vsmall) symysize(vsmall)                                   ///
>         ring(6) position(1) region(fcolor(ltbluishgray) lstyle(none))                                   ///
>         bmargin(zero))                                                                                                  ///
> note("Sources: [[source here]] and authors' calculations."                                                      ///
>         "Notes: [[notes here]].",                                                                                       ///
>         size(vsmall) span)                                                                                              ///
> graphregion(fcolor(ltbluishgray)) ysize() plotregion(style(none))
> ** end graph here
>
> *************** end example
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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