Introduction
Often, when creating a bar graph, you want to be able to include more than one variable in the display. In this blog entry, we’ll show you how to use Stata to generate bar graphs that track several variables.
Create Your Data
Let’s say you’re asking a cohort of 60 students divided into 3 classes to disclose how many times they have watched each of 7 movies. Try the following code:
set obs 7
gen movie = _n
label define movie 1 "The Godfather" 2 "Star Wars" 3 "Gone with the Wind" 4 "Jaws" 5 "The Exorcist" 6 "The Wizard of Oz" 7 "The Wall"
label value movie movie
gen class_1 = runiform(0,10)
gen class_2 = runiform(2,16)
gen class_3 = runiform(5,25)
gen class1 = round(class_1)
gen class2 = round(class_2)
gen class3 = round(class_3)
drop class_*
Horizontal Graph Bar
Try the following code to generate a horizontal bar graph:
graph hbar class1 class2 class3, over(movie) scheme(s1color)
Here’s what you get:
Vertical Graph Bar
Try the following code to generate a vertical bar graph:
graph bar class1 class2 class3, over(movie) scheme(s1color)
Here’s what you get (after working in the graph editor to generate vertical rather than horizontal movie labels):
The logic of placing more than one variable on a bar graph in Stata involves listing these variables after graph bar or graph hbar, and then using alternatives such as by and over to integrate comparisons.
BridgeText can help you with all of your statistical analysis needs.