Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# RE: st: Bar chart with several different variables on X axis

 From "Nick Cox" To Subject RE: st: Bar chart with several different variables on X axis Date Fri, 26 Mar 2010 13:12:51 -0000

```Uli is right, but Florian's problem remains getting there from where he is.

Florian is right, and I think this has arisen several times before. But it's more fun re-creating a solution than ploughing through the results of a Google on bar charts in Stata, or whatever.

With three variables and three categories, Florian's problem is that -graph bar- has the structure the wrong way round has far as he is concerned. Therefore, one way to approach the problem is to change the data structure. (This clearly doesn't rule out a neater solution with -graph bar-, but I don't know of one.)

Let's fake a dataset:

set obs 30
set seed 42
gen group = ceil(_n/10)
label def group 1 A 2 B 3 C
label val group group
forval j = 1/3 {
gen y`j' = runiform()
}

It's easier to work with a reduced dataset. -collapse- defaults to means.

collapse y?, by(group)

Now we need a -reshape-:

reshape long y , i(group) j(variable)

This is what the data now look like

l

+-----------------------------+
| group   variable          y |
|-----------------------------|
1. |     A          1   .6148067 |
2. |     A          2   .4555445 |
3. |     A          3   .6178768 |
4. |     B          1   .4991903 |
5. |     B          2   .5034846 |
|-----------------------------|
6. |     B          3   .4378768 |
7. |     C          1   .4493385 |
8. |     C          2   .4305637 |
9. |     C          3   .4129249 |
+-----------------------------+

Now it is easy to get two kinds of graph:

. graph bar y , over(group) over(variable)

. graph bar y , over(variable) over(group)

You've lost the different colours, but that's arguably gain too. The labelling will need work, but that's standard options.

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

Ulrich Kohler

Besides googling, have you also looked in the help of -graph bar-?
If you type

. help graph_bar##group_options

you'll find an example that is similar to

. sysuse auto
. graph bar weight, over(for) over(rep78)

Florian Köhler:

> I am trying to generate a bar chart where I have several different variables
> on the X-Axis (Var 1 to Var 3 in the example below, identically scaled) and
> the according mean on the Y axis. Each of this variables should be split
> into three groups according to a categorical variable "Cat" (categories A,
> B,C in the example). The graph should look like that.
>
> 	mean
> 	|
> 	|			___		___
> ___
> 	|		___	| |		| |
> | |
> 	|		| |	| |		| |		___
> ___	| |
> 	|	___	| |	| |		| |	___	| |
> | |	| |	___
> 	|	| |	| |	| |		| |	| |	| |
> | |	| |	| |
> 	|	| |	| |	| |		| |	| |	| |
> | |	| |	| |
> 	|	| |	| |	| |		| |	| |	| |
> | |	| |	| |
> 	|	|A|	|B|	|C|		|A|	|B|	|C|
> |A|	|B|	|C|
>
> --------------------------------------------------------------------------
> 			Var	1			Var	2
> Var	3
>
>
> Isn't the graph I need pretty common? I find it quite strange that I cannot
> figure out how to make it work even after excessive googling ;).
>
> All I managed to get was a bar chart split into three groups A, B, and C
> (according to the three categories in variable "cat") and for each of these
> groups the mean of var 1 var2 etc was graphed.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```