Stacked bar graph - Reshaping the data

Below is a worked example of a stacked bar graph. In this example, I have created my own dataset:

The commands I used in a do-file are:

//creating the data
clear
set obs 1000
egen id = seq(), block(10)
egen time = seq(), to(10)
format time %tq
gen time2=string(time, "%tq")
gen sector = ceil(3 * runiform())
bysort id: replace sector=sector[1]
label define sector 1 "SECTOR 1" 2 "SECTOR 2" 3 "SECTOR 3"
label values sector sector
gen sales=exp(rnormal())*1000
gen sales2=2*sales

//reshaping the data
gen obs = _n
rename sales sales1
reshape long sales, i(obs) j(which)

graph bar (sum) sales ,  over(sector) over(which) ///
over(time2, label(labsize(vsmall))) asyvars stack legend(size(vsmall))

You may find more useful information in the book A Visual Guide to Stata Graphics

10 views

© 2020 by Survey Design and Analysis Services. 

  • LinkedIn
  • Facebook
  • Twitter
  • YouTube