Adding average line to bar graph

Below is a worked example:

The command I used in a do-file are:

webuse grunfeld, clear  
keep if company == 1
keep if year < 1945

generate year2 = year + 0.3
generate year3 = year - 0.3

generate average = (invest + mvalue + kstock) / 3

twoway bar invest year3, barw(0.3) || ///
bar mvalue year, barw(0.3)         || ///
bar kstock year2 , barw(0.3)       || ///
line average year , name(a2, replace)

You may find more useful information in the book A Visual Guide to Stata Graphics, 3rd Edition.

169 views0 comments