I will put together the steps required to make a stata graph template. In Stata's terminology, graph-templates are called scheme. Stata schemes are a set of instructions written in plain text that controls how a particular graph will look like in terms of color, line styles, and all other aesthetic aspects of it. Stata scheme is an easy way of making the graphs like the way you want. Stata color scheme can give your graph a better look and feel.
PURPOSE
As Bill Rising has mentioned in his presentation you might want to make your graphs look different than the default ones because
- a particular journal needs the figures in a different color and styles
- your in-house graphic designer wants it in a specific shade of colors for the publication, or
- you simply want it in a different way
There are three ways you can achieve this. Bill has discussed all of them. Here, I will focus on the third one-- using the scheme. It is much easier to modify an existing scheme to fit for your purpose. As such, I will be modifying an existing one to create a new scheme.
EXAMPLE
Let us start with an example.
sysuse auto, clear tabulate rep78, generate(rep) /* set scheme s2color // the default scheme */ graph bar rep1-rep5, /// title(Title of Bar Plot) /// ytitle(Average)
This will create the following bar plot

Let’s suppose that we want to modify for whatever reason, the color of the bars, the color of the font in the title, and the background of the plot area to look like the following:

We can make the graph look like the above by using either the graph editor and making the changes, or by using the Graph Recorder, or by creating a new color scheme. I will illustrate how to create a scheme from an existing scheme in Stata. I’ve done this with Stata version 12.1 SE. I do not know if the procedure will be the same in other versions.
COLOR SCHEME
Let’s talk a few words about color schemes. First, you need to decide your color scheme. If you have a graphic design team in your office, they will provide you with the color scheme that they’ve picked for the publication.
You should have either the names of the colors or the RGB or CMYK color codes for the color you wish you use. I will use RGB codes in this example since I do not want to be restricted by the names of the colors. The procedure is the same for CMYK codes. To show an example, I will use the color scheme as shown in this picture. It has been generated from the www.colorschemer.com website.

There are four columns in the above scheme. Let’s say, I want to assign the colors to the bars as follows:
Color for bar 1: 51 204 128 (Column 4, color 1) Color for bar 2: 51 204 204 (Column 4, color 2) Color for bar 3: 51 128 204 (Column 4, color 3) Color for bar 4: 51 51 204 (Column 4, color 4) Color for bar 5: 51 204 51 (Column 3, color 1)
MAKING THE SCHEME
Now we will actually create the scheme with the colors we’ve picked. The steps are:
Step 1:
Create a plain text file and give it a name. The syntax for the file name is
scheme-name.scheme
I will create a scheme named sanbbox, so, my filename will be
scheme-sandbox.scheme
You may wish to save it in the ado path in your Stata installation. Usually the path is C:\ado. You can put this file there. Or, for now, we will put it in our working folder where Stata is running from.
Step 2:
Now, copy the following code in to the scheme-sandbox.scheme file and save it.
// An example of making a color-scheme in Stata // Author: Enayetur Raheem // Feb 21, 2012 // We will use the default theme s2color as our // base scheme. We will only override the options // where we want to make the changes #include s2color // Colors for the bars-- a modification from s2color color p1bar "51 204 128" color p2bar "51 204 204" color p3bar "51 128 204" color p4bar "51 51 204" color p5bar "51 204 51"
Notice that there is no space after # in the line #include s2color
This line tells Stata to load or use the color scheme called s2color. That means, if you create your own scheme called sandbox and just add the #include s2color in the scheme file, it is as if you’ve renamed s2color scheme to sandbox.
Step 3:
Assuming that you’ve placed the scheme-sandbox.scheme file in c:\mystata, open stata from the same folder and run the following commands to load the data:
sysuse auto, clear tabulate rep78, generate(rep) /* set scheme s2color // the default scheme */ graph bar rep1-rep5, scheme(sandbox) /// title(Title of Bar Plot) /// ytitle(Average)
Or, if you have already opened the data set, just run
graph bar rep1-rep5, scheme(sandbox) /// title(Title of Bar Plot) /// ytitle(Average)
Notice that I’ve added something extra in the first line after the comma.
scheme(sandbox)
This tells the graph command to use the sandbox scheme instead of the default one. After this, you should get a figure like the following.

If your figure does not look like this, you might want to clear the cache and run the code again. To do this, run the following
/* to clear the cache */ discard /* and then run */ graph bar rep1-rep5, scheme(sandbox) /// title(Title of Bar Plot) /// ytitle(Average)
You might have noticed that the newly created graph does not look as you wanted. Its title-text color is not brown, the background is still white. More importantly, the bars have a kind of glowing outlines that looks like they have been shaded.
To fix the problem, add the following lines to your scheme-sandbox.scheme file, and repeat the code again
// outlines for the bars // assign same color as the bars to have no outline color p1 "51 204 128" color p2 "51 204 204" color p3 "51 128 204" color p4 "51 51 204" color p5 "51 204 51"
With these lines of codes, we are giving the outlines of the bars the same color as the bars.
Step 4:
To change the title color and the color of the background, add the following two lines in your scheme-sandbox.scheme file
color heading "122 26 23" // Title color color background "246 226 180" // Background color
Now, you should get the desired graph.

Finally, the scheme file looks like the following:
// An example of making a color-scheme in Stata // Author: Enayetur Raheem // Feb 21, 2012 #include s2color color heading "122 26 23" // Title color color background "246 226 180" // Background color // Colors for the bars-- a modification from s2color color p1bar "51 204 128" color p2bar "51 204 204" color p3bar "51 128 204" color p4bar "51 51 204" color p5bar "51 204 51" // outlines for the bars: // assign same color as the bars to have no outline color p1 "51 204 128" color p2 "51 204 204" color p3 "51 128 204" color p4 "51 51 204" color p5 "51 204 51"
FURTHER READING
For more information about the components of each graph, read from Stata help files. You should at least read the scheme elements page by running the command:
help scheme entries
This will open up the scheme file entries. You will find all the options in each of the components of a graph.
You should also read Bill Rising’s beamer slide on “Schemes and the Graph Editor” at http://www.stata.com/meeting/portugal10/portugal10_rising.pdf


Comments
Great!
Excellent article. Have plans to do some hardcore reading/coding on stata soon: these pages will definitely help me get started. Keep them coming :)
Thanks!
Thanks for your kind words. Its always best to keep things in writing as you do it. I was making a scheme for a project and thought it could be useful for others.
I would love to get some input from you as well as you are quite an expert in programming and computational statistics.
Nice work
I have replicated the graphs, its really very interesting to get control over the color on graphs. It will help me a lot. Thanks for the post.
Add new comment