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

st: RE: Graph title question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Graph title question
Date   Sat, 17 May 2003 15:59:03 +0100

Friedrich Huebler suggested a problem

> How can I add the contents of a variable to a graph title? I work
> with data like this:
>
> country  year  total  male  female
> Albania  1999     20    15      25
> Algeria  2000     10    15       5
> Angola   2000     30    35      25
>
> The following commands produce bar graphs for individual countries.
>
> levels country, local(countries)
> foreach c of local countries {
>   graph bar total male female if country=="`c'", title("`c'")
> }
>
> The title contains the country name and I would like to add
> the year,
> as in "Albania 1999." This looks like a trivial problem but I cannot
> find a solution.

For those following, let me stress that this is all (up-to-date)
Stata 8.

The following solutions have been suggested:

(1) Lee Sieswerda
=================

I don't know if this is the most efficient way to accomplish your
task, but
it works:

levels country, local(countries)
levels year, local(year)
foreach c of local countries {
	foreach y of local year {
		capture graph bar total male female if country=="`c'" &
year==`y', title("`c' `y'")
	}
}


You need the -capture- before -graph bar-, otherwise your do-file will
end
as soon as the -foreach- loops generate a country/year combination
that
doesn't exist.

(2) Kit Baum
============

Assuming you only have one year per country, this is a bit less work
than checking every possible year/country combination:

levels country, local(countries)
g n = _n
foreach c of local countries {
summ n if country=="`c'",meanonly
local y = year[r(min)]
graph bar total male female if country=="`c'", title("`c' `y'")
}

(3) Michael Blasnik
===================

Even less work is:

gen countryyear=country+" "+string(year)
levels countryyear, local(countries)
foreach c of local countries {
   graph bar total male female if countryyear=="`c'", title("`c'")
 }

All these solutions show key Stata tricks, as follows:

(1) A double loop and -capture- (Lee). This is crude,
but often practical: you make Stata do the work of catching
the pairs which don't apply.

(2) Look-up technique (Kit). I know the value of one
variable in an observations, but not another. How
do I find it? The
technique illustrated by Kit has many applications.

(3) A new composite variable (Michael). Sometimes
a slightly different data representation does
the trick.

Here is yet another solution.

(4)

Friedrich wants here the often-maligned loop over observations,
so we are not obliged to use -levels-:

forval i = 1 / `=_N' {
	graph bar country in `i', ti(`=country[`i']' `=year[`i']')
}

The trick is the construct

`=whatever'

which evaluates whatever and inserts its value in the same
place.

Having said that, for whatever is Friedrich's real problem
there could also be value in a plot of several countries'
values. For that, in addition to -graph dot-, -pairplot-
on SSC offers various possibilities.

Nick
[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