O'Reilly logo

Data Analysis with Stata by Swizec Teller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Pyramidal graphs

What if we need a pyramidal graph divided into all possible subcategories of the data? Here is your answer in terms of the Stata code:

egen scoregroup= cut(score), group(9) label
tab scoregroup

sort sex scoregroup session
contract sex scoregroup session
rename _freq cnt

reshape wide cnt, i(scoregroup sex) j(session)

gen d1 = cnt1 /*session = 1 */
gen d12 = cnt1+cnt2
gen d123 = cnt3 + d12
gen d1_n = - d1
gen d12_n = - d12
gen d123_n = - d123

gen zero = 0

label values scoregroup scoregroup
label values sex f1

twoway bar c123 scoregroup if sex==0 , horizontal || ///
  bar c12 scoregroup if sex==0, horizontal || ///
  bar c1 scoregroup if sex ==0, horizontal  || ///
  bar c123_n scoregroup if sex ==1 , horizontal || ///
 bar c12_n scoregroup ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required