> -----Original Message-----
> From: Friedrich Huebler [mailto:[email protected]]
> Sent: Wednesday, April 09, 2003 11:19 PM
> To: [email protected]
> Subject: st: Re: How to suppress legend keys
>
>
> --- Scott Merryman <[email protected]> wrote:
> > > Is it possible to instruct Stata to only show keys for data
> > > that appears in the graph?
> >
> > This will only show the legend for only the second variable:
> >
> > twoway (scatter h r mp), legend(order(2 "Repair 1978"))
>
> Scott,
>
> Thank you for the suggestion but this does not quite help me. I am
> creating time series graphs for several dozen countries with a
> -foreach- loop and need a catchall command that does not require me
> to know in advance which legend keys should appear in which graph.
>
> Here is an abbreviated excerpt from my command (I use ; as a
> delimiter). The variable "country" contains the country name, "year"
> is the year, and "var_a" and "var_b" are time series variables. The
> macro "countries" contains the names of the countries for which I
> need graphs.
>
> foreach x of local countries {;
> scatter var_a year if country=="`x'", connect(l) ||
> scatter var_b year if country=="`x'", connect(l);
> graph export "`x'.wmf";
> };
>
> If var_a or var_b have no values, no lines will be plotted. However,
> there is still a legend with entries for var_a and var_b. I want to
> suppress the respective legend key if a time series does not appear
> in the graph. How can this be done? Is there a kind of "legend if
> var_a<." command? At least one of my time series always has data, so
> there are no completely empty graphs.
>
> Friedrich Huebler
Seems to me that you will need to build your own check of each variable,
and then modify the graphing command accordingly. Something like:
foreach x of local countries {
quietly count if var_a<. & country=="`x'"
if r(N)>0 {
local leg1 1
}
else {
local leg1
}
quietly count if var_b<. & country=="`x'"
if r(N)>0 {
local leg2 2
}
else {
local leg2
}
scatter var_a year if country=="`x'", connect(l) || ///
scatter var_b year if country=="`x'", connect(l) ///
legend(order(`leg1' `leg2'))
graph export "`x'.wmf"
}
Once you are checking this, you could also do it as follows, which
generalizes to more variables:
foreach x of local countries {
local gcmd
foreach v of varlist var_a var_b {
quietly count if `v'<. & country=="`x'"
if r(N)>0 {
local gcmd `"`gcmd' scatter `v' year if country=="`x'",
connect(l) ||"'
}
}
`gcmd'
graph export "`x'.wmf"
}
This just builds up the list of scatter commands in the local macro
`gcmd', then executes the accumulated set.
Nick Winter
*
* 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/