mylabels and myticks(SJ22-?: gr00??)

Title

mylabels and myticks -- Axis labels or ticks on specified scales

Syntax

mylabels
lbllist
,
local(macname)
[
myscale(transformation_syntax)
format(format)
clean
prefix(text)
suffix(text)
firstonly
lastonly
]

myticks
ticklist
,
local(macname)
[
myscale(transformation_syntax)
]

Description

mylabels and myticks generate axis labels and ticks on
specified scales. Their use is best explained by examples, as below.
Note that the label list lbllist and tick list ticklist will
be expanded if presented as legal numlists and left as specified
otherwise.

Remarks

You draw a graph and one axis is on a transformed square root scale.
You wish the axis labels to show untransformed values. For some values
this is easy; for example, ylabel(0 1 2 "4" 3 "9" 4 "16" 5 "25")
says use "4" as label for 2, and so forth. For other values
and/or other transformations the conversion may be more difficult to do
in your head, so that a dedicated utility is helpful.

The idea behind mylabels is that you feed it the labels (usually,
but not necessarily, numeric labels) that you want shown and the
transformation being used. It will then place the appropriate
specification in a local macro which you name. You may then use that
local macro as part of a later graph command.

So suppose that you want labels 0 1 4 9 16 25 36 49, and your data are
square roots of these. Your call is
mylabels 0 1 4 9 16 25 36 49, myscale(sqrt(@)) local(labels). Or
suppose you want percents shown, and your data are proportions 0-1. Your
call is mylabels 0(25)100, myscale(@/100) local(labels). Think of
this in the following way: my graph labels that I want are 0(25)100, but
my data scale is that of the labels divided by 100.

For a more challenging example, see the graph on Gabriel Rossman's blog at
http://codeandculture.wordpress.com/2010/02/08/memetracker-into-stata/

Here the x axis variable is a date-time and the x axis labels
shown are nice numbers in terms of clock time in milliseconds but by any
other standard are arbitrary and awkward. The dates span a period in
late 2008 and early 2009, so monthly labels would seem more natural
(even though not regularly spaced in terms of clock time). An example is
then
mylabels "1 Aug 2008" "1 Sep 2008" "1 Oct 2008" "1 Nov 2008" "1 Dec 2008" "1 Jan 2009" "1 Feb 2009", myscale(clock("@", "DMY")) local(labels)

A similar idea may be used for axis ticks.

Options

local(macname) inserts the option specification in
local macro macname within the calling program's space. Hence that
macro will be accessible after mylabels or myticks has
finished. This is helpful for later use with graph or other
graphics commands. This is a required option.

myscale() specifies the transformation used to produce the data
you have. Stata syntax should be used with @ as placeholder for
original value. To show proportions as percents, specify
myscale(@/100). If no transformation is being used, you may
specify myscale(@), but that is also the default.

format() specifies a format controlling the labels. This is an
occasionally specified option. Its main use is to enforce the
presentation of leading zeros.

clean specifies a minimal format eliding trailing zeros and
decimal points (whether periods (stops) or commas). This option is most
often used together with format(). Thus by itself
format(%03.2f) would render 0(0.25)1 as 0.00 0.25 0.50 0.75 1.00
but clean reduces it to 0 0.25 0.5 0.75 1. Do not use this option with integer labels such as 10(10)40.

prefix() specifies text to be prepended to all axis labels.
Specify any blank spaces within " ".

suffix() specifies text to be appended to all axis labels. Specify
any blank spaces within " ".

firstonly and lastonly specify that any prefix or suffix should be attached to the first or last label, only.

Examples

. sysuse auto, clear

. scatter mpg weight

. generate gpm = 1000/mpg

. mylabels 15(5)40, myscale(1000/@) local(myyla)

. scatter gpm weight, yla(`myyla', ang(h)) ytitle(Miles per gallon (reciprocal scale)) ms(Oh)

. myticks 12/41, myscale(1000/@) local(myyti)

. scatter gpm weight, yla(`myyla', ang(h)) ytitle(Miles per gallon (reciprocal scale)) ymtic(`myyti') ms(Oh)

. webuse citytemp, clear

. summarize

. scatter tempjuly tempjan

. mylabels 10(5)35, myscale(32 + (9/5)* @) local(myyla)

. mylabels -15(5)20, myscale(32 + (9/5)* @) local(myxla)

. scatter tempjuly tempjan, yla(`myyla', ang(h)) xla(`myxla') ms(Oh) ///

ytitle(Average July temperature ({&degree}C)) xtitle(Average January temperature ({&degree}C))

. sysuse census, clear

. generate pc_older = 100 * pop65p / pop

. nicelabels pc_older, local(yla)

. mylabels `yla', suffix(%) local(yla)

. scatter pc_older medage, ylabel(`yla', ang(h)) xlabel(, format(%2.0f)) ytitle(% 65 and older) ms(none) mlabel(state2) mlabpos(0)

. mylabels 0(5)20, local(labels) myscale(@/100) suffix(" %")

. mylabels 0(5)20, local(labels) prefix($)

. mylabels 0(0.2)1, clean local(labels)

. mylabels 0(0.25)1, clean local(labels)

. mylabels -0.025(0.005)0.025, clean local(labels)

. mylabels 0.005(0.005)0.025, format(%04.3f) clean local(labels)

. su mpg

. mylabels -1/3, myscale(`r(mean)' + (@) * `r(sd)') local(labels)

. histogram mpg, xaxis(1 2) xla(`labels', axis(2) grid) xtitle(z scale, axis(2)) normal

Author

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

Acknowledgments

The idea behind these programs may be traced to a program by Patrick
Royston (1996). A question from Richard Campbell led to the last example
above. A question from Clive Nicholas and a suggestion by Philippe van
Kerm led to a correction and further comments above. A question from
Richard Goldstein led to the prefix() and suffix() options.
An example on Gabriel Rossman's blog, as noted above, provided a
stimulus to generalise the program. A question from John Kim reminded me
of my frequent need to elide trailing zeros and decimal points when
enforcing leading zeros.

References

Royston, P. 1996.
Flexible axis scaling.
Stata Technical Bulletin 34, 9-10 (and in STB Reprints 6, 34-36).
Freely accessible at
http://www.stata.com/products/stb/journals/stb34.pdf

Also see

help: [G-3] axis_label_options

help: nicelabels (if installed)

help: niceloglabels (if installed)