Bar graphs

The most frequent usage of a bar graph is to only depict summary statistics for one variable over the categories of another variable.

There are three basic applications of bar graphs:

  1. Relative frequencies for categories of a variable
  2. Summary statistic (e.g. mean) for one variable over categories of another variable
  3. Summary statistic (e.g. mean) for more than one variable (one bar per variable). This only makes sense if all variables have same the scale.

See the code below for an example as well as commonly used options.

* Basic bar graphs
sysuse auto, clear

// default is to display the mean of the variable
graph bar price
sum price 
graph bar (mean) price
// the statistic to be displayed can be changed
graph bar (median) price

// use option over() to display variable for different groups
graph bar (mean) price, over(foreign)
// add titles
graph bar (mean) price, over(foreign) title("Origin of cars and average prices") ytitle("Price of cars")

// not specifying yvars (here: price) will show percent of observations in each group
graph bar, over(foreign)

// the option asyvars treats group in option over() as yvars
// here, this allows you to change the colour of each bar
graph bar (mean) price, over(foreign) title("Origin of cars and average prices") ytitle("Price of cars") asyvars bar(1, color(blue)) bar(2, color(green))
	//Increase space between bars 
graph bar (mean) price, over(foreign, gap(160)) title("Origin of cars and average prices") ytitle("Price of cars") asyvars bar(1, color(blue)) bar(2, color(green))
	//Change aspect ratio (height/width) 
graph bar (mean) price, over(foreign, gap(60)) title("Origin of cars and average prices") ytitle("Price of cars") asyvars bar(1, color(blue)) bar(2, color(green)) aspectratio(2)
	//Shift y-axis to the right
graph bar (mean) price, over(foreign, gap(60)) title("Origin of cars and average prices") ytitle("Price of cars") asyvars bar(1, color(blue)) bar(2, color(green)) yalt
	//Horizontal bars
graph hbar (mean) price, over(foreign, gap(60)) title("Origin of cars and average prices") ytitle("Price of cars") asyvars bar(1, color(blue)) bar(2, color(green))