How can letters in Stata graphs be formatted?
Text in Stata graphs can be formatted according to the
SMCL standards, for instance
sysuse auto
regress mpg weight
local r_sq = round(e(r2_a), .01)
scatter mpg weight, title("Mileage {it:decreases} with weight") ///
text(30 4000 "R{sup:2} = `r_sq'")
Other useful SMCL tags for this purpose are bold text
{bf}, superscripts
{sup}, and subscripts
{sub}. The Stata help for
-text- has a full list
here, including a list of greek, math, and other symbols. Furthermore, the page explains that fonts in graphs can also be adjusted without having to change the entire
-scheme- of the graph.
How can odd characters be included in Stata graphs?
Nonstandard characters like the
en or
em dash can also be included in graph text by using the
{char} tag.
{char} is followed by the ASCII (or rather ANSI/
Windows-1252) code; so in order to obtain the ASCII-superscripted 2, one needs to write
{char 178} or just
{c 178} (no colon in between).
#delimit ;
scatter mpg weight, title("Mileage{char 151}
does it decrease with weight?")
text(30 4000 "R{char 178} = `r_sq'") ;
#delimit cr
The readymade superscript 2 actually seems to be a bit clearer than the 2 superscripted by Stata.
More details on displaying characters via ASCII codes can be found
here.
Cox (2004) describes a somewhat more awkward way of using odd characters, namely by assigning them to locals first and then including the locals into the text strings.
How can a line break be included in a Stata graph?
For titles, creating a line break is rather straightforward. Simply put the text in two sets of double quotes " ", the line break will appear between those two line breaks:
#delimit ;
scatter mpg weight, title("Mileage:"
"Does it decrease with weight?");
#delimit cr
However, when doing this in a label command, it will yield an error message:
option labels() incorrectly specified
expects, # "label" # label ... r(198);
Cox (2005) explains how this can be fixed. The intuition to use two sets of double quotes is also correct for labels in a graph. However, in order to have Stata understand this in a
-label()- option, the two sets of double quotes need to be included in compound double quotes `" "'. Cox gives the following example:
graph hbar (mean) mpg, over(foreign, relabel(1 `" "Domestic" "cars" "' ///
2 `" "Foreign" "cars" "'))
Reference
Cox, Nicholas J. 2004. "Stata Tip 6. Inserting Awkward Characters in the Plot."
Stata Journal 4(1):95-96.
Cox, Nicholas J. 2005. "Stata Tip 24. Axis Labels on Two or More Levels."
Stata Journal 5(3):469-469.