Contour plots are a way to show a three-dimensional surface on a two-dimensional plane. It graphs two predictor variables on the x and y axes, and a response variable z as contours. in contour plots, the z dimension with contours are represented using colours or lines. This type of graph is widely used in many disciplines such as cartography, astrology and meteorology.
In this article, I will show you how to create contour plots in Stata. The most commonly used command is twoway contour, in addition to that, you can add other options like levels(), ccuts() and ccolors() to allow you to customise your own contour plots. To demonstrate, I used the dataset sandstone.dta. You can import this dataset by typing:
This dataset contains geographic data regarding the depth of sandstone formations for a certain geographic region. Two dimensions are used to represent the location in the north or south direction and the east or west direction, and the third dimension indicates the depth of the sandstone layer.
Creating contour plots using twoway contour command
In the command pane, type:
twoway contour depth northing easting
It allows you to draw a graph showing the depth of the sandstone layers as a function of the location (north and east). The graph I got is shown below:
This is a basic contour plot you can get using the twoway contour command. You may notice that variable depth is graphed as the the z axis at the right side of the graph, with variables northing and easting as the y axis and x axis respectively. In this graph, variable depth has been divided into 5 levels, with the first contour colour as blue, which represents the smallest values for depth, and the last contour colour as red representing the highest values of depth.
2. Using levels() and ccuts() options to specify the levels of the contours
In the first example, Stata has divided depth into 5 levels by default, by using levels() and ccuts() options, you can specify how many levels you would like. For example, in the command pane, type:
twoway contour depth northing easting, levels(10)
You will get the below graph:
Compared with the original graph, the depths are now represented using 10 contours.
Here is another example using the ccuts() option, in the command pane, type:
twoway contour depth northing easting, ccuts(7500(100)8100)
You will get the below graph:
ccuts(7500(100)8100) specifies that the contours range from 7500 to 8100 in 100-foot increments. There are 6 contours in total, where blue represents the smallest values and orange is the highest depth. However, you may have noticed how similar the colours are for depths of 7700-7800 and 7800-7900. Can we specify colours for each of the contours in Stata? Yes, I will show you how to do this in the next example.
3. Using ccolor(), scolor() and ecolor() options to specify the colours for each of the contours
In Stata, you can use ccolor() option to specify the colours for each of the contours. For example, in the command pane, type:
twoway contour depth northing easting, ccuts(7500(100)8100) ccolors(red green pink purple)
You will get:
In this example, there are 6 levels for the depth, however, I only specified 4 colours. Actually, you do not need to specify the colours for all the contours. From the graph, we can see that Stata has applied green, pink and purple for depths 7500-7800, the contour depth below 7500 feet is red, for depths above 7800, Stata used green, yellow and orange which are the colours when we omitted the ccolors() option.
We can also use Stata to specify the starting and the ending colour. For example, in the command pane, type:
twoway contour depth northing easting, levels(10) scolor(red) ecolor(purple)
In the above example, the scolor() option specifies the starting colour, and the ecolor() option specifies the ending colour. The result is that the colours start with pure red at the lowest depth and end at pure yellow at the highest depths.
4. The twoway contourline command
The twoway contourline command creates contour plots as well except that the contours are shown (by default) as contour lines. Please see the example below. In the command pane, type:
twoway contourline depth northing easting
5. The colorlines option
Similarly to ccolor(), scolor() and ecolor() options being used with twoway contour command, the colorlines option can be used with twoway contourline command to display the contour lines in colour. For example, in the command pane, type:
twoway contourline depth northing easting, colorlines
To learn more about contour plots in Stata and other Stata graphics, we recommend the book A Visual Guide to Stata Graphics, 3rd Edition.