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]

st: twoway stacked bar charts


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

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/


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