Data visualization with Stata¶
-- Hua Peng @ StataCorp
- Creating graphs in Stata is easy.
- Stata supports a wide variety of plots.
- Stata graphic commands are highly customizable and extensible.
This presentation uses PyStata: see https://www.stata.com/python/pystata18/ for details.hub:
In [1]:
import stata_setup
stata_setup.config('C:/Program Files/Stata18', 'mp')
___ ____ ____ ____ ____ ® /__ / ____/ / ____/ StataNow 18.5 ___/ / /___/ / /___/ MP—Parallel Edition Statistics and Data Science Copyright 1985-2023 StataCorp LLC StataCorp 4905 Lakeway Drive College Station, Texas 77845 USA 800-782-8272 https://www.stata.com 979-696-4600 [email protected] Stata license: 10-user 4-core network perpetual Serial number: 1 Licensed to: Stata Developer StataCorp LLC Notes: 1. Unicode is supported; see help unicode_advice. 2. More than 2 billion observations are allowed; see help obs_advice. 3. Maximum number of variables is set to 5,000 but can be increased; see help set_maxvar.
graph command history:¶
In [2]:
%%stata -qui
clear
input float version str60 notes float(date z year) byte weight
8 "new graph command {superscript:1}" 15707 0 2003 1
9 "new plot types, png export, etc." 16548 0 2005 0
10 "graph editor" 17342 0 2007 1
11 "smcl tags in text" 18091 0 2009 0
12 "marginsplot, contour plot, export pdf" 18833 0 2011 0
13 "improvements of marginsplot" 19526 0 2013 0
14 "Unicode支持" 20180 0 2015 1
15 "color transparency, new marker symbols, and svg export" 20976 0 2017 1
16 "meta forestplot, size in points, inches, or centimeters" 21726 0 2019 0
17 "more meta and bayes plots" 22390 0 2021 0
18 "new schemes, new color styles, and colorvar()" 23125 0 2023 1
end
format %td date
label var version "Stata version"
label var z "Major features"
label var year "Release year"
twoway (scatter version z if weight==0, mlabel(notes) msymbol(none)) ///
(scatter version z if weight==1, mlabel(notes) mlabsize(*1.5) msymbol(none)) ///
(scatter year z, mcolor(none) yscale(noline axis(2)) ///
ylabel(2003(2)2023, axis(2) notick nogrid) yaxis(2)) ///
, yscale(noline) ylabel(8(1)18, notick nogrid) ///
xlabel(0 2, nogrid noticks nolabels) ///
xscale(noline noextend) ///
legend(off) xtitle("") ///
title("Major graphic features in each release") ///
note("1. old graph command can be invoked with {bf:graph7}") nodraw
graph save gr_history.gph, replace
In [3]:
%stata graph use gr_history.gph
graph command overview:¶
New default scheme¶
- The graphs are 7.5 inches wide and 4.5 inches tall.
- The color palette is updated with brighter colors. ...
- The background color is white.
- The legend contains one column and is placed on the right side of the graph.
- The y-axis labels are horizontal.
- The major grid line is dashed. The marker size is small.
New default scheme(cont.)¶
- Reference lines and lines added with the xline() and yline() options are black.
- The histogram fill color is stc1 with a 90% intensity, while the outline color is stc1 with a 70% intensity.
An example of different schemes¶
In [4]:
%%stata -qui
// new default scheme stcolor
sysuse sp500, clear
twoway scatter high low date || ///
line high low date, || ///
rarea high low date, color(gray%20)
In [5]:
%%stata -qui
// old default scheme s2color
sysuse sp500, clear
twoway scatter high low date || ///
line high low date, || ///
rarea high low date, color(gray%20) ///
scheme(s2color)
In [6]:
%%stata -qui
// new grey style scheme stmono2
sysuse sp500, clear
twoway scatter high low date || ///
line high low date, || ///
rarea high low date, color(gray%20) ///
scheme(stmono2)
In [7]:
%%stata -qui
// requires user-written schemepack
// https://github.com/asjadnaqvi/stata-schemepack
sysuse sp500, clear
twoway scatter high low date || ///
line high low date, || ///
rarea high low date, color(gray%20) ///
scheme(gg_tableau)