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

st: Re: Bar labels for stacked bar chart (Oct. 2003)

From   Friedrich Huebler <>
Subject   st: Re: Bar labels for stacked bar chart (Oct. 2003)
Date   Thu, 22 Apr 2004 10:46:44 -0700 (PDT)

I received a couple of messages off-list regarding a query that I
posted in October 2003. I had tried to create a stacked bar chart
with one label per bar, indicating total height. I received no
responses and never posted my solution, which you will find below. My
original message is in the archive:

As an example, the data below shows the share of the population in
countries A and B that speaks at least one or two foreign languages.

country   onelang   twolang
      A        30        10
      B        20         5

A stacked bar graph shows that in country A 40 percent of the
population speak at least one foreign language. In country B 25
percent of the population speak at least one foreign language.

. graph bar onelang twolang, over(country) stack blabel(total)

If we want only one label indicating the total height we have to
transform the data so that a twoway bar plot can be drawn. The labels
are added with a twoway scatter plot.

. gen total = onelang + twolang
. gen barnumber = 1
. replace barnumber = 2 if country=="B"
. graph twoway scatter total barnumber, msymbol(none) mlabel(total)
mlabpos(12) || bar total barnumber || bar onelang barnumber,
yscale(r(0 45)) ylabel(0(10)40) xtitle("") xlabel(1 "Country A" 2
"Country B") legend(col(2) label(2 "Two languages") label(3 "One
language") order(3 2))

The commands can be expanded to accommodate more than two categories
(e.g., onelang, twolang, and threelang; or countries A, B, and C).
Notice that the graph has to be built up from the back, with the
tallest bars being drawn first in the -bar ... || bar ...- sequence.

For those who read my original message, the -blabel(total)- option
used to have a bug that yielded wrong values in the labels. This bug
has been fixed.

Friedrich Huebler

Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25˘
*   For searches and help try:

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