# st: Ordering bar graphs by yvar value (was: Suggestions for SecondEdition of A Visual Guide to)

 From David Radwin To statalist@hsphsun2.harvard.edu Subject st: Ordering bar graphs by yvar value (was: Suggestions for SecondEdition of A Visual Guide to) Date Thu, 23 Aug 2007 12:42:40 -0700

Dear Nick,

Please accept my apologies for unintentionally slighting your program -catplot- as well as for taking so long to apologize. I do greatly appreciate your many, many helpful programs and explanations on this list and elsewhere.

It turns out the primary limitation I am running up against is not related to -catplot- per se but to Stata's graphing capabilities in general--or so I think. What I really would like to do is to create bar charts where the bars are ordered not by values of the xvar but by values of the yvar.

I think this example will make it more clear. Suppose for sake of illustration I want to compare the repair records of foreign and domestic cars. The commands

sysuse auto, clear
catplot hbar rep78, over(foreign) percent(foreign)

shows the bars in this order:

1. percentage of cars with foreign=0 and rep78=1
2. percentage of cars with foreign=0 and rep78=2
. . .
6. percentage of cars with foreign=1 and rep78=1 [this bar happens to have zero length]
. . .
10. percentage of cars with foreign=1 and rep78=5

That is all well and good, but I want to emphasize the difference between foreign and domestic on each value of rep78, with an ordering of

1. percentage of cars with foreign=0 and rep78=1
2. percentage of cars with foreign=1 and rep78=1
3. percentage of cars with foreign=0 and rep78=2
4. percentage of cars with foreign=1 and rep78=2
. . .
9. percentage of cars with foreign=0 and rep78=5
10. percentage of cars with foreign=1 and rep78=5

If there is a -graph- option that does this, I would be delighted to learn about it. The best I could muster was this workaround:

gen domestic=foreign==0
collapse (sum) foreign domestic, by(rep78)
foreach var of varlist foreign domestic {
egen `var'_tot=total(`var')
gen `var'_pct=round(100*`var'/`var'_tot , 1)
}
graph hbar (asis) domestic_pct foreign_pct , over(rep78)

However, this solution is significantly more cumbersome and less flexible than using -catplot-. Can you or anyone else suggest a better alternative?

Thanks,

David

At 7:41 PM +0100 7/26/07, n j cox wrote:

```First, a reminder (to others in this and other threads as well)
to say where user-written stuff you cite comes from. -catplot-,
in this case, is a program on SSC.

Second, what's the implication here? I looked at -catplot-
and it allows the user a wildcard to relevant graph options,
so you don't lose all control over detail.

If you think the program is limited, please make a specific
suggestion or criticism, so the author can think about it.

The author happens to be me, so I endorse that general
principle.

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

...
I would like more examples analogous to -catplot- but
with greater control over formatting.
....
```
```--
Office of Student Research, University of California, Berkeley
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```