Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Stacked Bar Chart


From   [email protected] (Vince Wiggins, StataCorp)
To   [email protected]
Subject   Re: st: Stacked Bar Chart
Date   Tue, 28 Jan 2003 15:22:56 -0600

Eric G. Wruck <[email protected]> asks about creating a stacked bar chart
where one of the bars stacks three variables and another bar stacks two other
variables.  He then wants these two bars graphed for nine years.

> I have five different variables (call them a, b, c, d, & e) for nine
> years (i.e., 9 obs).  I would like to construct a bar chart in the
> following way:
>
> I would like to stack a, b, & c in a single bar, and d & e in
> another & graph them both -over(year)-
>
> I have been able to graph d & e stacked over(year) & I've been able
> to graph (a + b + c) and (d + e) over(year) but I can't figure out
> how to get both in a single graph.


-graph bar- is flexible, but not nearly so flexible as all of Stata.  What is
required here is a little good old-fashioned data manipulation.

What Eric can do is duplicate each of his observations and set his 3 variables
(a, b, and c) to 0 in the original observations and his 2 variables (d and e)
to 0 in the duplicate observations.  Then, all he need do is create a bar
graph stacking all 5 variables -- half the bars will stack variables a, b, and
c, the other two variables being 0, and the other half of the bars will stack
variables d and e.

Let's use the Klein data from the [R] reg3 entry, mainly because it has short
names.  Eric's a, b, and c will be w, p, and t; and his d, and e will be y and
c.  (I am too lazy to rename the variables.)  We keep the first 9 observations
to match Eric's data.

      . webuse kleinabr
      . keep in 1/9

Now, expand the data to duplicate each observation and create a variable to
label the original and duplicate observations,

      . expand 2
      . gen barname = cond(_n <= 9 , "my 2 var" , "my 3 vars")

Replace the set of three variables to stack with 0 in half the observations.

      . replace w = 0 in 1/9
      . replace p = 0 in 1/9
      . replace t = 0 in 1/9

Replace the set of two variables to stack with 0 in the other half of the
observations.

      . replace y = 0 in 10/l
      . replace c = 0 in 10/l

Graph the 5 variables over the bar identifier variable (barname) and year.
(We will use horizontal bars, -hbar-, because they look better with so many
labels).

      . gr hbar w p t y c , stack over(barname) over(year)

The graph described above is prettier if we place all the 2-variable bars
together and all the three variable bars together, though I think Eric wanted
the first graph.

      . gr hbar w p t y c , stack over(year) over(barname)

Finally, adorn graph to taste.


-- Vince
   [email protected]

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index