In this dataset I want to separate men and women, but I also want the colours of the bars for the 5 men and the 5 women to match. This means the first bar for men will be the same colour as the first bar for women. I can use the bysort and generate commands with Stata’s _n system variable to randomly “pair up” one man with one woman. As long as I set this new variable as the first over() variable in the graph bar command, the bar colours should line up the way I want them to.
To create this graph use the following commands:
clear
input w pos mark
1 1 69.55
1 2 65.16
1 3 64.91
1 4 64.53
1 5 63.70
0 6 84.58
0 7 84.51
0 8 84.12
0 9 83.34
0 10 82.8
end
sort w pos
by w: generate id = _n
label define kk 1 "Barbora - CZE " 2 "Christina - GER " 3 "Linda - GER " 4 "Sunette - RSA" 5 "Huihui - CHN" 6 "Keshorn - TRI " 7 "Oleksandr - UKR " 8 "Antti - FIN " 9 "Vitezslav - CZE" 10 "Tero - FIN"
label value pos kk
label define w 1 "Women" 0 "Men"
label value w w
graph bar (asis) mark, over(id) over(pos,label(angle(45))) over(w) ylabel(0(10)90, angle(45)) blabel(bar, position(inside) format(%9.1f) color(black)) legend(off) bargap(5) title("London Olympics 2012" "Javelin") nofill ytitle("Metres")
To know more about Stata graphs, please have a look at the book Speaking Stata Graphics.