Background/coordinate system

grstyle set plain

grstyle set plain applies a plain overall look (white background, no shading of by-labels and matrix labels, black title, and grid lines in gray). Example:

. grstyle init

. grstyle set plain, horizontal grid

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg, by(foreign)
Code
grstyle-set/plain.svg

Option grid request that all grid lines be printed. Option horizontal sets the orientation of tick labels on the y axis to horizontal. Moreover, you can use option dotted to request dotted grid lines:

. grstyle init

. grstyle set plain, horizontal grid dotted

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/plain2.svg

Furthermore, option nogrid turns grid lines off and option noextend requests that axis lines only be drawn to the smallest and largest tick:

. grstyle init

. grstyle set plain, nogrid noextend

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/plain3.svg
[top]

grstyle set mesh

grstyle set mesh uses a mesh of grid lines as coordinate plane, omits axis lines and ticks, and prints grid lines and tick labels in gray. Example:

. grstyle init

. grstyle set mesh, horizontal

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/mesh.svg

Option horizontal sets the orientation of tick labels on the y axis to horizontal. Furthermore, you can use option compact to reduce the symbols size, some font sizes, and the graph margin, and option minor to add a minor grid:

. grstyle init

. grstyle set mesh, horizontal compact minor

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/mesh2.svg
[top]

grstyle set imesh

grstyle set imesh uses an inverted mesh of grid lines as coordinate plane (gray background with white grid lines; no axis lines). Example:

. grstyle init

. grstyle set imesh, horizontal 

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/imesh.svg

Option horizontal sets the orientation of tick labels on the y axis to horizontal. Furthermore, you can use option compact to reduce the symbols size, some font sizes, and the graph margin, and option minor to add a minor grid:

. grstyle init

. grstyle set imesh, horizontal compact minor

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/imesh2.svg
[top]

grstyle set horizontal

grstyle set horizontal sets the orientation of tick labels on the y axis to horizontal:

. grstyle init

. grstyle set horizontal

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/hor.svg
[top]

grstyle set compact

grstyle set compact reduces the symbols size, some font sizes, and the graph margin:

. grstyle init

. grstyle set compact

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/compact.svg
[top]

grstyle set grid

grstyle set grid turns all grid lines on (horizontal, vertical, minimum, maximum):

. grstyle init

. grstyle set grid

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/grid.svg
[top]

grstyle set nogrid

grstyle set nogrid turns grid lines off:

. grstyle init

. grstyle set nogrid

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/nogrid.svg

This does not affect all plot types. For example, in graphs using the by() option the default horizontal grid lines are still drawn. Apply option ylabel(, nogrid) to the graph command to omit these grid lines.

[top]

grstyle set noextend

grstyle set noextend causes axis lines to be drawn only to the smallest and largest tick marks without extending them to the bounding box of the plot region:

. grstyle init

. grstyle set noextend

. sysuse auto, clear
(1978 Automobile Data)

. scatter price mpg
Code
grstyle-set/noextend.svg
[top]

The legend

grstyle set legend determines the position and some style aspects of the legend. Example:

. grstyle init

. grstyle set legend 2, inside

. sysuse auto, clear
(1978 Automobile Data)

. two (scatter price mpg if foreign) (scatter price mpg if !foreign)
Code
grstyle-set/legend.svg

Option inside requests placing the legend inside the plot region. Argument 2 moves the legend to the top right; see help clockposstyle. In the following example, the legend is placed at the lower right side of the plot region and the frame around the legend is removed:

. grstyle init

. grstyle set imesh, horizontal compact minor

. grstyle set legend 4, nobox

. sysuse auto, clear
(1978 Automobile Data)

. two (scatter price mpg if foreign) (scatter price mpg if !foreign)
Code
grstyle-set/legend2.svg
[top]

Transparent CIs

grstyle set ci makes shading of confidence intervals transparent (Stata 15 required):

. grstyle init

. grstyle set ci

. sysuse auto, clear
(1978 Automobile Data)

. two (scatter price length) ///
>     (lpolyci price length if foreign==0) ///
>     (lpolyci price length if foreign==1, astyle(ci2)) ///
>     , legend(off)
Code
grstyle-set/ci.svg

By default, gs12 and ltkhaki are used as colors for ci and ci2 (as in the s2color scheme), and opacity is set to 50%. The following example uses colors 11 and 13 from the burd palette and sets opacity to 60%:

. grstyle init

. grstyle set ci burd, select(11 13) opacity(60)

. sysuse auto, clear
(1978 Automobile Data)

. two (scatter price length) ///
>     (lpolyci price length if foreign==0) ///
>     (lpolyci price length if foreign==1, astyle(ci2)) ///
>     , legend(off)
Code
grstyle-set/ci2.svg
[top]

Colors

Assigning a palette

grstyle set color assigns colors using the color scheme entry attribute. See help colorpalette for available color palettes. Here is an example using the economist palette:

. grstyle init

. grstyle set legend 2, nobox

. grstyle set color economist

. sysuse nlsw88, clear
(NLSW, 1988 extract)

. graph bar wage if occ<9, over(occ) asyvars over(union)
Code
grstyle-set/color.svg

Same graph using the rainbow scheme from the ptol palette:

. grstyle set color ptol, rainbow n(8) reverse

. graph bar wage if occ<9, over(occ) asyvars over(union)
Code
grstyle-set/color2.svg

Same graph using the Greens palette from ColorBrewer:

. grstyle set color Greens, n(8)

. graph bar wage if occ<9, over(occ) asyvars over(union)
Code
grstyle-set/color3.svg

Option n() has been specified in the last two examples because these palettes provide different colors depending on the number of colors one requires. The same is true in the following example that uses HCL colors with evenly spaced hues (hue palette):

. grstyle init

. grstyle set imesh, horizontal compact minor

. grstyle set legend 4, nobox stack

. grstyle linewidth plineplot 0.7

. grstyle set color hue, n(4)

. sysuse uslifeexp, clear
(U.S. life expectancy, 1900-1999)

. foreach v in le_wfemale le_wmale le_bfemale le_bmale {
  2.     label variable `v' `"`=substr("`:var lab `v''",18,.)'"'
  3. }

. line le_wfemale le_wmale le_bfemale le_bmale year, ytitle(Life expectancy)
Code
grstyle-set/color4.svg

Specifying custom colors

Instead of using a named palette you can provide custom colors by specifying a list of colorstyles (named colors, RBG colors specified as "# # #", CMYK colors specified as "# # # #", or HSV colors specified as "hsv # # #"). In addition, grstyle set also supports colors specified as hex triplets or in HCL format ("hcl # # #"). See Specifying colors in help grstyle set for details. Here is an example using hex codes of semantic colors used for buttons in Bootstrap v3.3:

. grstyle init

. grstyle set color #286090 #449d44 #31b0d5 #ec971f #c9302c

. drop _all

. input primary success info warning danger

        primary     success        info     warning      danger
  1. 12 14 2 8 5
  2. end

. graph pie primary success info warning danger ///
>     , plabel(_all name, size(*1.5) color(white)) legend(off)
Code
grstyle-set/hex.svg

Making markers transparent

Making marker symbols transparent is a bit tricky. If you just make their color transparent, the marker outline will look different than the marker fill because the outline is printed on top of the fill. A solution is to make the outline fully transparent, as in the following example:

. grstyle init

. grstyle set imesh

. grstyle set color Set1, opacity(50): p#markfill

. grstyle set color Set1, opacity(0):  p#markline

. grstyle symbolsize p huge

. sysuse auto, clear
(1978 Automobile Data)

. two (scatter price length if foreign) (scatter price length if !foreign)
Code
grstyle-set/tmark.svg

Color intensity

Color intensity can be set using the intensity() option when calling grstyle set color. For some elements, however, color intensity can also be set as a separate scheme entry using grstyle set intensity. The following example sets the fill intensity of bars in graph bar to 30 percent:

. grstyle init

. grstyle set plain

. grstyle set color Accent: p#bar p#barline 

. grstyle set inten 30: bar

. sysuse citytemp, clear
(City Temperature Data)

. graph bar (mean) tempjuly tempjan, over(region) bargap(-30)
Code
grstyle-set/inten.svg
[top]

Symbols and line patterns

Command grstyle set symbol assigns marker symbols, command grstyle set lpattern assigns line patterns. Stata's default color schemes use filled circles as marker symbols and solid lines for all plots, which makes the graphs hard to read on black and white printouts. To improve readability, you could change the symbols and line patterns as in the following example:

. grstyle init

. grstyle set plain

. grstyle set color Set1

. grstyle set symbol

. grstyle set lpattern

. sysuse auto, clear
(1978 Automobile Data)

. quietly separate price, by(foreign) shortlabel

. two connect price? weight, ytitle(Price) sort
Code
grstyle-set/symbol.svg

Since Stata 15 it is possible to use rotated symbols. Corresponding scheme entries can be constructed using the anglestyle scheme entry attribute. Example:

. grstyle init

. grstyle set plain

. grstyle set color Set1, opacity(50)

. grstyle set symbolsize large

. grstyle set symbol T T

. grstyle anglestyle p2symbol 180

. sysuse auto, clear
(1978 Automobile Data)

. qui separate price, by(foreign)

. scatter price? weight, ytitle(Price)
Code
grstyle-set/angle.svg
[top]

Absolute sizes

grstyle set provides a number of subcommands to set the size of objects, thickness of lines, and width of margins. See grstyle set size, grstyle set symbolsize, grstyle set linewidth, and grstyle set margin. Their usage with standard size specifications as documented in textsizestyle, markersizestyle, linewidthstyle, and marginstyle is straightforward. In addition to these relative sizes, however, grstyle set also supports the specification of sizes in absolute units (inches, points, centimeters, or millimeters).

The procedure is to first define the desired graph size using grstyle set graphsize. After that, apply grstyle set size etc. to set sizes in absolute terms. An example is as follows:

. grstyle init

. grstyle set legend 2

. grstyle set graphsize 10cm 13cm

. grstyle set size 12pt: heading

. grstyle set size 10pt: subheading axis_title

. grstyle set size 8pt: tick_label key_label

. grstyle set symbolsize 1 2 3 4 5, pt

. grstyle set linewidth 0: pmark

. grstyle set linewidth 1pt: axisline tick major_grid legend xyline

. grstyle set linewidth 2.5pt: plineplot

. grstyle set margin .5cm: twoway               // margin of plot region

. grstyle set margin ".5 1 .75 .25", cm: graph  // margin of graph region

.                                               // (left right bottom top)
. sysuse auto, clear
(1978 Automobile Data)

. quietly separate price, by(rep) shortlabel

. twoway (scatter price? weight) (lfit price weight), ///
>     title("Title (12pt)") subtitle("Subtitle (10pt)") ///
>     xtitle("X axis title (10pt)") ytitle("Y axis title (10pt)") ///
>     legend(order(1 "1pt" 2 "2pt" 3 "3pt" 4 "4pt" 5 "5pt")) ///
>     plotregion(color(gs14) icolor(white)) ///
>     graphregion(color(gs14) icolor(ltbluishgray))
Code
grstyle-set/sizes.svg

The requested absolute sizes will only be preserved as long as the graph size (i.e., the minimum of height and width of the graph) does not change. Also see Setting absolute sizes in help grstyle set.

[top]

Custom subcommand

A personal collection of graph settings can be provided by defining a program called grstyle_set_myname. myname will then be available to grstyle set like any other subcommand.

When calling your program, grstyle set provides global macro GRSTYLE_FH containing the file handle of the scheme file to be written to. Use file write with this handle to add your scheme entries. For example, the following program would set the default approximate number of labeled ticks to 10 (and print y-labels horizontally):

capture program drop grstyle_set_10ticks
program grstyle_set_10ticks
    file write $GRSTYLE_FH "numticks_g horizontal_major 10" _n
    file write $GRSTYLE_FH "numticks_g vertical_major   10" _n
    file write $GRSTYLE_FH "anglestyle vertical_tick horizontal" _n
end
Code

After defining the program (or storing the program in file grstyle_set_10ticks.ado), it can be used as follows:

. grstyle init

. grstyle set 10ticks

. sysuse auto, clear
(1978 Automobile Data)

. scatter price weight
Code
grstyle-set/10ticks-ex.svg

Your program can also have options. Here is an extended example with an option for the approximate number of labeled ticks and an additional option to set the number of unlabeled minor ticks between the labeled ticks:

capture program drop grstyle_set_nticks
program grstyle_set_nticks
    syntax [, n(int 5) mticks(int 0) ]
    file write $GRSTYLE_FH "numticks_g horizontal_major `n'" _n
    file write $GRSTYLE_FH "numticks_g vertical_major   `n'" _n
    file write $GRSTYLE_FH "anglestyle vertical_tick horizontal" _n
    file write $GRSTYLE_FH "numticks_g horizontal_tminor `mticks'" _n
    file write $GRSTYLE_FH "numticks_g vertical_tminor   `mticks'" _n
end
Code

You could then, for example, type:

. grstyle init

. grstyle set nticks, n(10) mticks(5)

. sysuse auto, clear
(1978 Automobile Data)

. scatter price weight
Code
grstyle-set/nticks-ex.svg