Difference between revisions of "Stata Coding Practices: Visualization"

Jump to: navigation, search
Line 14: Line 14:
 
==General Graphics Tools==
 
==General Graphics Tools==
  
===Graphics Options===
+
===Graphics options===
  
 
<syntaxhighlight lang="stata">
 
<syntaxhighlight lang="stata">
Line 21: Line 21:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Graphical Schemes===
+
===Graphical schemes===
  
 
Graphical schemes apply a large number of these options simultaneously, and in doing so they provide one of the highest degrees of cross-system consistency that is possible in creating graphs. Stata includes several built-in graphical schemes; the familiar "Stata blue" graphs are created using the <code>s2color</code> scheme.
 
Graphical schemes apply a large number of these options simultaneously, and in doing so they provide one of the highest degrees of cross-system consistency that is possible in creating graphs. Stata includes several built-in graphical schemes; the familiar "Stata blue" graphs are created using the <code>s2color</code> scheme.
Line 36: Line 36:
 
One thing that schemes cannot do, apparently, is control the default graphics font. This can be done using <syntaxhighlight lang="stata" inline>graph set</syntaxhighlight>, as in <syntaxhighlight lang="stata" inline>graph set window fontface "Helvetica"</syntaxhighlight>.
 
One thing that schemes cannot do, apparently, is control the default graphics font. This can be done using <syntaxhighlight lang="stata" inline>graph set</syntaxhighlight>, as in <syntaxhighlight lang="stata" inline>graph set window fontface "Helvetica"</syntaxhighlight>.
  
===Combining Graphics===
+
===Combining Stata graphics===
  
 
==Specific Visualization Approaches==
 
==Specific Visualization Approaches==
  
===The <syntaxhighlight lang="stata" inline>graph</syntaxhighlight> Command===
+
===The <syntaxhighlight lang="stata" inline>graph</syntaxhighlight> command===
  
===The <syntaxhighlight lang="stata" inline>twoway</syntaxhighlight> Command===
+
===The <syntaxhighlight lang="stata" inline>twoway</syntaxhighlight> command===
  
===Built-in Visualization Commands===
+
===Built-in visualization commands===
  
===User-written Visualization Commands===
+
===User-written visualization commands===

Revision as of 21:14, 9 November 2020

(This page is under construction.)

Modern Stata versions have extremely powerful graphics capabilities which allow the rapid creation of publication-quality graphics from almost any kind of tabular data. Although the default graphical commands and settings leave much to be desired, the customizability and interoperability of Stata's visualization tools mean that almost any imaginable output can be rendered using Stata's built-in graphics engine.

Read First

Stata graphics are typically created using one of four command types. Each has specific use cases, strengths, and weaknesses, and it is important to be familiar with the abilities and limitations of each when considering which to use to create a particular visualization. All four methods (except some user-written commands) use the same basic styling syntax discussed in this article.

  • The graph command suite creates pre-packaged visualizations, typically based on Stata's native collapse syntax and statistics.
  • The twoway suite, which is the most commonly used tool, allows a flexible and open-ended approach to visualizing any amount of information in an abstract set of axes.
  • Built-in graphical commands (such as lowess) offer pre-packaged visualizations that do not follow the graph style. These commands are typically better used within a twoway environment and may behave differently when used independently.
  • User-written commands (such as iegraph or spmap) create custom visualizations, but typically have unique purpose-built syntaxes and cannot be integrated in a twoway environment.

General Graphics Tools

Graphics options

local bad BAD
sysuse auto`bad'.dta

Graphical schemes

Graphical schemes apply a large number of these options simultaneously, and in doing so they provide one of the highest degrees of cross-system consistency that is possible in creating graphs. Stata includes several built-in graphical schemes; the familiar "Stata blue" graphs are created using the s2color scheme.

The graph scheme can be changed using the set scheme command. Stata will use the sysdir path to search for matching graph schemes, so for example a third-party scheme file (like Uncluttered) might be included in the top-level directory of a repository and applied in the run file by writing:

sysdir set PERSONAL "${directory}/"
set scheme uncluttered

This directs Stata to search for scheme-uncluttered.scheme and apply it to all graphics created while Stata remains open. This is a simple scheme which incorporates many of the universally-applicable options above for all graphs, particularly region coloring and axis marking. As with any third-party scheme, you should read the documentation; notably, this scheme provides a specific color palette and turns off the legend by default.

One thing that schemes cannot do, apparently, is control the default graphics font. This can be done using graph set, as in graph set window fontface "Helvetica".

Combining Stata graphics

Specific Visualization Approaches

The graph command

The twoway command

Built-in visualization commands

User-written visualization commands