st: RE: Order Bar Graph Bars by the Over Variable

 From "Nick Cox" To Subject st: RE: Order Bar Graph Bars by the Over Variable Date Fri, 13 Feb 2009 19:05:09 -0000

```Your syntax wouldn't achieve what you want in Stata, even if it were
legal. -over(foreign)- sorts on foreign anyway. Here is one way.

sysuse auto, clear
local y1 : variable label turn
local y2 : variable label  mpg
stack turn foreign mpg foreign , into(y foreign) clear
label def _stack 1 "`y1'" 2 "`y2'"
label val _stack _stack
label def foreign 0 Domestic 1 Foreign
label val foreign foreign
graph bar y , over(foreign) over(_stack) ytitle(means)

And here is another way:

sysuse auto, clear
collapse (mean) turn mpg, by(foreign)
rename mpg ympg
rename turn yturn
reshape long y , i(foreign) string
graph bar y , over(foreign) over(_j)

I am probably missing something easier.

Nick
n.j.cox@durham.ac.uk

L S

I am trying to create a simple bar graph in STATA.  Suppose I am using
the sysuse auto dataset.

Q:  I would like to have it so that my graph compares the variables
turn and mpg across the dummy variable foreign.  That is, on the left
side
of the graph, it should compare turn across foreign and domestic cars,
and
on the right side it should compare mpg across foreign and domestic
cars.
Right now I am typing:

sysuse auto, clear
bar graph (mean) turn mpg, over(foreign)

The problem with this is that this displays the mean of turn and mpg for
domestic cars on the left hand side, and then the mean of turn and mpg
for
foreign cars on the right hand side.  Visually, this setup makes the
compare turn and mpg for domestic cars, and compare turn and mpg for
foreign cars,
instead of the comparison I want.  What I want to essentially do is to
sort the 4 bars by foreign.

graph bar (mean) turn mpg, over(foreign, sort(foreign))