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

RE: st: RE: Getting at value labels


From   Roger Harbord <[email protected]>
To   [email protected]
Subject   RE: st: RE: Getting at value labels
Date   Fri, 13 Feb 2004 21:27:47 -0000

I wrote the short ado-file below a while back, prompted by a colleague's request for help in overlaying twoway plots for several groups on the same axes and using the value labels in the legend. It seems from this thread that other people have encountered the same issues.

Personally i think StataCorp should consider adding such a facility to -twoway- but in the meantime others may find this helpful. (Not thoroughly tested and doubtless will give unexpected errors, ugly graphs or unsuitable legends in some situations.)

After running the code below, the graph Danielle H. Ferry wanted should be produced by the single command:

. twover connected aRate year, over(marEduc)

For further examples using auto.dta see the extended comment at the top of the code.

Feedback welcomed... if people find it useful and StataCorp don't soon render it obsolete i might get around to writing a .hlp file and submitting it to SSC.

(In case anyone wonders, i avoided using -separate- in the code below because of difficulties ensuring the variables it creates are temporary.)

Roger.
[email protected]

------------------------------------------------------------

*! twover.ado
*! Roger Harbord version 0.2 24oct2003

/*
Gives twoway graphs an over(varname) option
to overlay graphs for each level of varname on the same axes
and use value labels of varname in legend.

Syntax: twover plottype yvar xvar [if exp] [in range],
over(varname) [twoway_options]

e.g.: . twover scatter price weight, over(foreign)
. twover connected price weight, over(foreign) sort
. twover scatter price mpg, over(foreign) ///
legend(order(- "Origin:" - " " 1 2) colfirst)

Note that only one yvar can be specified.
*/

program define twover
version 8.0
syntax namelist(min=3 max=3 id="plottype yvar xvar") ///
[if] [in], over(varname) *
marksample touse
tokenize `namelist'
args plottype y x
confirm variable `y' `x'

local fmt : format `y'
qui levels `over' if `touse', local(levels)

foreach i of local levels {
tempvar tmp
local tmplist `tmplist' `tmp'
qui gen `tmp' = `y' if `over' == `i' & `touse'
label var `tmp' `"`: label (`over') `i''"'
format `tmp' `fmt'
}

local ylabel : var label `y'
if `"`ylabel'"' =="" {
local ylabel `y'
}

twoway `plottype' `tmplist' `x' ///
if `touse', ytitle(`ylabel') `options'

end

-----------------------------------------------------


--On 13 February 2004 14:12 +0000 Nick Cox <[email protected]> wrote:


Correct about the missing quotes and other
careless slips. Thanks for the polish.

To put this in context, the results of

. separate mpg, by(foreign) shortlabel

              storage  display     value
variable name   type   format      label      variable label
------------------------------------------------------------------------
-------
mpg0            byte   %8.0g                  foreign == Domestic
mpg1            byte   %8.0g                  foreign == Foreign

are too long-winded for my graphical tastes.

For a graph, I'd personally want to zap further "foreign == "
and the corresponding lines should be more like.

local eq = index(`"`label'"', "=")
local label = substr(`"`label'"',`eq'+3,.)

That's what more or less -labnoeq- does, any way.

Nick
[email protected]

-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of
Arnold Kester
Sent: 13 February 2004 13:59
To: [email protected]
Subject: Re: st: RE: Getting at value labels


Two missing quotes and a missing part in the label string, imho.

Nick Cox wrote:
> I would do this. I assume value labels attached
> to -marEduc-.
>
> separate aRate, by(marEduc) shortlabel
> local aRatevars "`r(varlist)'"
> twoway connected `aRatevars' year
>
> Sometimes even the -shortlabel- is too long.
> (There should be an option -veryshortlabel-
> to go beyond the -shortlabel- I suggested
> a while back.)
>
> ssc inst labutil
> labnoeq `aRatevars'
>
> will cut them down further. The first principles
> way to do that is something like
>
> foreach v of local aRatevars {
> 	local label : variable label `v'

Here a ' missing:

> 	local eq = index(`"`label'"', "=") - 1

and here a ' and the second part of the string

> 	local label =
substr(`"`label'"',1,`eq')+substr(`"`label'"',`eq'+4,.)
> 	label var `v' `"`label'"'
> }
>
I'd been looking for this for some time, so I tried it right
away. Thanks.
*
*   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/

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