Defaults for Stata graphs are recorded in so-called scheme files. Scheme
files have their own syntax. Basic information on the scheme file format
can be found in
help scheme files
and details on scheme entries are documented in
help scheme entries
.
In addition, a look into one of Stata's official scheme files can sometimes
be helpful (e.g., type viewsource scheme-s2color.scheme
to
view the source of the
s2color
scheme file).
Below is a step-by-step example that introduces a number of useful scheme
entries. Note that most of these changes can also be achieved with
less typing using grstyle set
.
Often an efficient procedure is to apply
grstyle set
for the bulk of the
changes and then do some fine tuning with additional scheme entries. Below
we focus on individual scheme entries only; see here
for examples on grstyle set
.
We start with a graph using official Stata's
s2color
scheme, which looks as follows:
. sysuse auto, clear (1978 Automobile Data) . set scheme s2color . two (scatter price weight if foreign==0) /// > (scatter price weight if foreign==1) /// > (lfitci price weight if foreign==0, clstyle(p1line)) /// > (lfitci price weight if foreign==1, clstyle(p2line)) /// > , legend(order(1 "domestic" 2 "foreign"))Code
A number of potentially useful modifications with respect to background and coordinate plane are as follows:
The described changes can be achieved as follows:
. grstyle init . grstyle color background white . grstyle color major_grid gs8 . grstyle linewidth major_grid thin . grstyle linepattern major_grid dot . grstyle yesno draw_major_hgrid yes . grstyle yesno grid_draw_min yes . grstyle yesno grid_draw_max yes . grstyle anglestyle vertical_tick horizontal . grstyle gsize axis_title_gap tiny . two (scatter price weight if foreign==0) /// > (scatter price weight if foreign==1) /// > (lfitci price weight if foreign==0, clstyle(p1line)) /// > (lfitci price weight if foreign==1, clstyle(p2line)) /// > , legend(order(1 "domestic" 2 "foreign"))Code
A problem with the graph is that the confidence intervals cover up a lot of information. A good idea might be to make the confidence intervals transparent (Stata 15 required) (we could also solve the problem by changing the order of plots so that the confidence areas are printed behind the markers; however, overlapping confidence areas would still be a problem). Furthermore, let's make the fitted lines a bit thicker:
. grstyle color ci_area gs12%50 . grstyle color ci_arealine gs12%0 . grstyle linewidth plineplot medthick . two (scatter price weight if foreign==0) /// > (scatter price weight if foreign==1) /// > (lfitci price weight if foreign==0, clstyle(p1line)) /// > (lfitci price weight if foreign==1, clstyle(p2line)) /// > , legend(order(1 "domestic" 2 "foreign"))Code
Color argument gs12%50
uses color gs12
and sets
the opacity to 50% (see
help colorstyle
).
Color argument gs12%0
sets the opacity to 0%, which makes
the outline invisible.
A further issue is that some of the markers are printed on top of each other. Moreover, for better visual distinction we might want to use different symbols for the two groups. The following code makes the markers transparent (Stata 15 required) and uses triangles for the markers of the second group:
. grstyle color p1markline navy%0 . grstyle color p1markfill navy%50 . grstyle color p2markline maroon%0 . grstyle color p2markfill maroon%50 . grstyle symbol p2 triangle . two (scatter price weight if foreign==0) /// > (scatter price weight if foreign==1) /// > (lfitci price weight if foreign==0, clstyle(p1line)) /// > (lfitci price weight if foreign==1, clstyle(p2line)) /// > , legend(order(1 "domestic" 2 "foreign"))Code
As a final change, we move the legend to the lower right side of the plot region and remove the frame around the legend:
. grstyle clockdir legend_position 4 . grstyle numstyle legend_cols 1 . grstyle linestyle legend none . two (scatter price weight if foreign==0) /// > (scatter price weight if foreign==1) /// > (lfitci price weight if foreign==0, clstyle(p1line)) /// > (lfitci price weight if foreign==1, clstyle(p2line)) /// > , legend(order(1 "domestic" 2 "foreign"))Code
We can now go on and create additional graphs using the style above. When done, we can
clear the custom settings and revert back to the
s2color
scheme as follows:
. grstyle clear . two (scatter price weight if foreign==0) /// > (scatter price weight if foreign==1) /// > (lfitci price weight if foreign==0, clstyle(p1line)) /// > (lfitci price weight if foreign==1, clstyle(p2line)) /// > , legend(order(1 "domestic" 2 "foreign"))Code