Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: second y-axis in graph bar


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: second y-axis in graph bar
Date   Wed, 29 Nov 2006 18:07:27 -0000

You can work your way to cloning -graph bar- 
more or less like this. 

. sysuse auto
(1978 Automobile Data)

. collapse (mean) length (mean) weight , by(rep78)

. l

     +---------------------------+
     | rep78    length    weight |
     |---------------------------|
  1. |     1       189     3,100 |
  2. |     2   199.375   3,353.8 |
  3. |     3       194     3,299 |
  4. |     4   184.833     2,870 |
  5. |     5   170.182   2,322.7 |
     |---------------------------|
  6. |     .       183     2,846 |
     +---------------------------+

If we draw a -twoway bar- for both variables, the bars 
will just be superimposed. We need to offset for clarity. 
In Stata Journal 7(1), James Cui has a Tip on this, but 
you won't see that until March. Here is the gist: 

(a) You nudge to the left...

. gen rep78_1 = rep78 - 0.2
(1 missing value generated)

(b) You nudge to the right... 

. gen rep78_2 = rep78 + 0.2
(1 missing value generated)

(c) And then you do the hokey-cokey, and ... 

. twoway bar length rep78_1, barw(0.4) || bar weight rep78_2, barw(0.4)

. twoway bar length rep78_1, barw(0.4) || bar weight rep78_2, barw(0.4) yaxis(1 2)

If you want different scales on the two axes, there are various 
ways of doing that. In this case, with different units, the length bars 
are much shorter. Suppose we multiply by 10, and then fix the labels. 
(Note that some people despise this. Howard Wainer wrote an entire paper
despising it: Wainer, H. 1991. Double Y-axis graphs. Chance 4(1): 50-51.) 

gen length2 = length * 10

// what follows is all one command 
twoway bar length2 rep78_1, 
barw(0.4) 
yaxis(1 2) 
yti("Length (in)", axis(1)) 
yla(1500 "150" 2000 "200" 2500 "250" 3000 "300", axis(1))  
|| 
bar weight rep78_2, 
barw(0.4) 
yti("Weight (lb)", axis(2)) 
legend(order(1 "Length" 2 "Weight"))

So the principles are: 

1. Suppose your categories are represented by integers. Then 
offset categories +/- w and use bar-widths that are 2w for 
showing two variables side-by-side. Three variables side-by-side
can be centred at integer - w, integer and integer + w, with 
bar-widths that are w. And so on. 

2. If you want separate scales, you can multiply or divide
and then make the labels honest. 

3. You must declare -yaxis(1 2)- and then say what you want 
on each axis. 

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

Harry Telser
 
> I want to draw a bar chart with two y-variables relating to two 
> different axes.
> 
> The graph should look something like that:
> 
> . sysuse auto
> . graph bar weight length, over(rep78)
> 
> but with "length" relating to a second y-axis on the right side.
> 
> Is there any way to define a second y-axis in "graph bar" command? I 
> know it can be done with "twoway bar" but I did not manage to get the 
> look as in "graph bar".
> 

*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index