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

Re: st: Variable label with -biplot-


From   [email protected]
To   [email protected]
Subject   Re: st: Variable label with -biplot-
Date   Thu, 29 Sep 2005 12:52:33 -0500

Jean-Benoit Hardouin <[email protected]> asks:

> I search to display in a biplot graph (obtained with -biplot- under 
> Stata 9) the labels instead of the names of the variables. Is it 
> possible ? I have not found any help in the manual about that.

Assuming the variable labels were reasonably short and more
descriptive than the variable names, it seems a reasonable thing
to want.

My first guess came from looking at -help biplot- and looking at
the -colopts()- option which allows pcarrow_options as suboptions
(which in turn allow for marker_label_options -- see help
marker_label_options).  From this I see that I can ask for a
change in the default size of the labels with

    . biplot ... , colopts(mlabsize(huge))

(which produces "huge" labels) and I could change the color, the
gap, the position, etc.  The suboption I had hoped would be of
help was

    colopts(mlabel(...))

where I would create a variable before calling -biplot- that
contained the variable labels and pass that in through
-colopts(mlabel())-.  but the -mlabel()- suboption is not allowed
in the -colopts()- option of -biplot-.

The reason is that Stata's -graph- system thinks in terms of
observations, and the information of the variables (columns) in
-biplot- has to be handled in a special way.  -biplot- hijacks
the -mlabel()- option to accomplish the task.

Assuming that seeing the variable names in your graphs is
sufficient for your initial explorations using -biplot- and that
the variable labels are only needed when you are producing your
final biplot (for publication), here is what I would do.

I use the auto dataset as a quick example

    sysuse auto
    biplot turn trunk mpg

Assuming that this graph is what I want except that I want to
replace "turn", "trunk", and "mpg" in the graph with "Turning
Circle", "Trunk space", and "Mileage" (wording similar to what is
in the variable labels for these 3 variables) I would look at the
y and x locations where the labels should land in the graph and
use that information to directly place the text using the
-text()- option (see twoway_options and follow that to
added_text_option) while suppressing the default labels with the
-colopts(nolabel)- option.

    biplot turn trunk mpg, colopts(nolabel) text(0.3 -4.5
        "Turning Circle" -5 -4.5 "Trunk space" -3.4 5 "Mileage")

(treat the 2 lines above as one line.)

If you are needing to do this a lot you might want to write a
wrapper command that will take care of a lot of this for you
automatically.  Here is a beginning draft of such a command:

=================== begin of biplotvlab.ado =========================
*! version 1.0.0  29sep2005
program biplotvlab

        version 9

        syntax varlist(numeric min=2) [if] [in] [, *]

	// run biplot quietly (and nograph) so we can get r(V)
        qui biplot `varlist' `if' `in' , `options' nograph
        tempname V
        mat `V' = r(V)

	// build the -text()- option
        local topt "text("
        local i 0
        foreach v of local varlist {
                local ++i
		// y value
                local topt `"`topt' `= `V'[`i',2]'"'
                // x value
                local topt `"`topt' `= `V'[`i',1]'"'
                // variable label
                local topt `"`topt' `"`: var label `v''"' "'
        }
        local topt `"`topt')"'

	// call with -colopts(nolabel)- and -text()- just built
        biplot `varlist' `if' `in', `options' colopts(nolabel) `topt'
end
===================== end of biplotvlab.ado =========================


Then I would call this as follows

    biplotvlab turn trunk mpg

Notice that -biplotvlab- places the variable labels right on the
ends of the arrows.  You probably want to add logic to the code
for moving them out a bit from the ends of the arrows.  If so,
replace the

    `= `V'[`i',2]'     and     `= `V'[`i',1]'

with more elaborate code.

If anyone wants to take over this code and enhance it, that is
fine with me.  I just wanted to point out the beginning direction
for a solution.

Ken Higbee    [email protected]
StataCorp     1-800-STATAPC

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