0% found this document useful (0 votes)
69 views2 pages

StataCheatSheet Visualization 2016 June-REV

This document provides a cheat sheet for basic data visualization with Stata. It outlines the syntax for creating various plot types including histograms, scatter plots, bar charts, dot plots, and more. It also describes options for customizing the appearance, axes, titles, and saving plots. Key plot types covered are histograms, scatter plots, bar charts for both discrete and continuous variables, as well as combined plots like contour plots and regression plots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views2 pages

StataCheatSheet Visualization 2016 June-REV

This document provides a cheat sheet for basic data visualization with Stata. It outlines the syntax for creating various plot types including histograms, scatter plots, bar charts, dot plots, and more. It also describes options for customizing the appearance, axes, titles, and saving plots. Key plot types covered are histograms, scatter plots, bar charts for both discrete and continuous variables, as well as combined plots like contour plots and regression plots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Visualization BASIC PLOT SYNTAX: graph <plot type>

variables: y first
y1 y2 … yn x [in]
plot-specific options
[if ], <plot options>
facet annotations
by(var) xline(xint) yline(yint) text(y x "annotation")
with Stata 15 Cheat Sheet titles axes
For more info see Stata’s reference manual (stata.com) title("title") subtitle("subtitle") xtitle("x-axis title") ytitle("y axis title") xscale(range(low high) log reverse off noline) yscale(<options>)
ONE VARIABLE sysuse auto, clear custom appearance plot size save
<marker, line, text, axis, legend, background options> scheme(s1mono) play(customTheme) xsize(5) ysize(4) saving("myPlot.gph", replace)
CONTINUOUS
histogram mpg, width(5) freq kdensity kdenopts(bwidth(5)) TWO+ CONTINUOUS VARIABLES
histogram
bin(#) • width(#) • density • fraction • frequency • percent • addlabels y1 graph matrix mpg price weight, half twoway pcspike wage68 ttl_exp68 wage88 ttl_exp88
addlabopts(<options>) • normal • normopts(<options>) • kdensity scatter plot of each combination of variables Parallel coordinates plot (sysuse nlswide1)
kdenopts(<options>) y2
half • jitter(#) • jitterseed(#) vertical, • horizontal
kdensity mpg, bwidth(3) y3 diagonal • [aweights(<variable>)]
smoothed histogram
bwidth • kernel(<options> main plot-specific options; twoway pccapsym wage68 ttl_exp68 wage88 ttl_exp88
twoway scatter mpg weight, jitter(7) Slope/bump plot (sysuse nlswide1)
normal • normopts(<line options>) see help for complete set scatter plot vertical • horizontal • headlabel
jitter(#) • jitterseed(#) • sort • cmissing(yes | no)
DISCRETE connect(<options>) • [aweight(<variable>)]
graph bar (count), over(foreign, gap(*0.5)) intensity(*0.5)
bar plot graph hbar draws horizontal bar charts
THREE VARIABLES
(asis) • (percent) • (count) • over(<variable>, <options: gap(*#) • 23 twoway scatter mpg weight, mlabel(mpg) twoway contour mpg price weight, level(20) crule(intensity)
relabel • descending • reverse>) • cw •missing • nofill • allcategories • 20 scatter plot with labelled values 3D contour plot
percentages • stack • bargap(#) • intensity(*#) • yalternate • xalternate 17 jitter(#) • jitterseed(#) • sort • cmissing(yes | no) ccuts(#s) • levels(#) • minmax • crule(hue | chue | intensity | linear) •
graph bar (percent), over(rep78) over(foreign) 2 10 connect(<options>) • [aweight(<variable>)] scolor(<color>) • ecolor (<color>) • ccolors(<colorlist>) • heatmap
interp(thinplatespline | shepard | none)
grouped bar plot graph hbar ...
(asis) • (percent) • (count) • over(<variable>, <options: gap(*#) • twoway connected mpg price, sort(price)
relabel • descending • reverse>) • cw •missing • nofill • allcategories • regress price mpg trunk weight length turn, nocons
a b c percentages • stack • bargap(#) • intensity(*#) • yalternate • xalternate scatter plot with connected lines and symbols matrix regmat = e(V) ssc install plotmatrix
jitter(#) • jitterseed(#) • sort see also line plotmatrix, mat(regmat) color(green)
DISCRETE X, CONTINUOUS Y connect(<options>) • cmissing(yes | no) heatmap mat(<variable) • split(<options>) • color(<color>) • freq
graph bar (median) price, over(foreign) graph hbar ...
bar plot (asis) • (percent) • (count) • (stat: mean median sum min max ...) twoway area mpg price, sort(price) SUMMARY PLOTS
over(<variable>, <options: gap(*#) • relabel • descending • reverse line plot with area shading
sort(<variable>)>) • cw • missing • nofill • allcategories • percentages twoway mband mpg weight || scatter mpg weight
sort • cmissing(yes | no) • vertical, • horizontal plot median of the y values
stack • bargap(#) • intensity(*#) • yalternate • xalternate base(#)
bands(#)
graph dot (mean) length headroom, over(foreign) m(1, ms(S))
dot plot (asis) • (percent) • (count) • (stat: mean median sum min max ...) twoway bar price rep78
over(<variable>, <options: gap(*#) • relabel • descending • reverse binscatter weight mpg, line(none) ssc install binscatter
sort(<variable>)>) • cw • missing • nofill • allcategories • percentages bar plot plot a single value (mean or median) for each x value
linegap(#) • marker(#, <options>) • linetype(dot | line | rectangle) vertical, • horizontal • base(#) • barwidth(#)
dots(<options>) • lines(<options>) • rectangles(<options>) • rwidth medians • nquantiles(#) • discrete • controls(<variables>) •
linetype(lfit | qfit | connect | none) • aweight[<variable>]
graph hbox mpg, over(rep78, descending) by(foreign) missing
box plot graph box draws vertical boxplots twoway dot mpg rep78 FITTING RESULTS
over(<variable>, <options: total • gap(*#) • relabel • descending • reverse dot plot vertical, • horizontal • base(#) • ndots(#) twoway lfitci mpg weight || scatter mpg weight
sort(<variable>)>) • missing • allcategories • intensity(*#) • boxgap(#) dcolor(<color>) • dfcolor(<color>) • dlcolor(<color>)
medtype(line | line | marker) • medline(<options>) • medmarker(<options>) calculate and plot linear fit to data with confidence intervals
dsize(<markersize>) • dsymbol(<marker type>) level(#) • stdp • stdf • nofit • fitplot(<plottype>) • ciplot(<plottype>) •
vioplot price, over(foreign) ssc install vioplot dlwidth(<strokesize>) • dotextend(yes | no) range(# #) • n(#) • atobs • estopts(<options>) • predopts(<options>)
violin plot over(<variable>, <options: total • missing>) • nofill •
vertical • horizontal • obs • kernel(<options>) • bwidth(#) •
twoway dropline mpg price in 1/5 twoway lowess mpg weight || scatter mpg weight
barwidth(#) • dscale(#) • ygap(#) • ogap(#) • density(<options>) calculate and plot lowess smoothing
bar(<options>) • median(<options>) • obsopts(<options>) dropped line plot
vertical, • horizontal • base(#) bwidth(#) • mean • noweight • logit • adjust

Plot Placement
twoway qfitci mpg weight, alwidth(none) || scatter mpg weight
JUXTAPOSE (FACET) twoway rcapsym length headroom price calculate and plot quadriatic fit to data with confidence intervals
twoway scatter mpg price, by(foreign, norescale) range plot (y1 ÷ y2) with capped lines level(#) • stdp • stdf • nofit • fitplot(<plottype>) • ciplot(<plottype>) •
range(# #) • n(#) • atobs • estopts(<options>) • predopts(<options>)
total • missing • colfirst • rows(#) • cols(#) • holes(<numlist>) vertical • horizontal see also rcap
compact • [no]edgelabel • [no]rescale • [no]yrescal • [no]xrescale REGRESSION RESULTS
[no]iyaxes • [no]ixaxes • [no]iytick • [no]ixtick • [no]iylabel
[no]ixlabel • [no]iytitle • [no]ixtitle • imargin(<options>) regress price mpg headroom trunk length turn
SUPERIMPOSE twoway rarea length headroom price, sort coefplot, drop(_cons) xline(0) ssc install coefplot
range plot (y1 ÷ y2) with area shading Plot regression coefficients
graph combine plot1.gph plot2.gph... vertical • horizontal • sort baselevels • b(<options>) • at(<options>) • noci • levels(#)
combine 2+ saved graphs into a single plot cmissing(yes | no) keep(<variables>) • drop(<variables>) • rename(<list>)
horizontal • vertical • generate(<variable>)
scatter y3 y2 y1 x, msymbol(i o i) mlabel(var3 var2 var1) regress mpg weight length turn
plot several y values for a single x value margins, eyex(weight) at(weight = (1800(200)4800))
twoway rbar length headroom price
graph twoway scatter mpg price in 27/74 || scatter mpg price /* range plot (y1 ÷ y2) with bars marginsplot, noci
*/ if mpg < 15 & price > 12000 in 27/74, mlabel(make) m(i) vertical • horizontal • barwidth(#) • mwidth Plot marginal effects of regression
combine twoway plots using || msize(<marker size>) horizontal • noci

Laura Hughes ([email protected]) • Tim Essam ([email protected]) inspired by RStudio’s awesome Cheat Sheets (rstudio.com/resources/cheatsheets) geocenter.github.io/StataTraining updated February 2016
follow us @flaneuseks and @StataRGIS Disclaimer: we are not affiliated with Stata. But we like it. CC BY 4.0
Plotting in Stata 15 ANATOMY OF A PLOT
title
Apply Themes
Customizing Appearance
annotation titles
subtitle Schemes are sets of graphical parameters, so you don’t
have to specify the look of the graphs every time.

200
For more info see Stata’s reference manual (stata.com) plots contain many features
y-axis 10 1
marker label USING A SAVED THEME
graph region
8 line

150
twoway scatter mpg price, scheme(customTheme)

y-axis title
inner graph region 9 5
inner plot region y-axis title 4 marker

100
Create custom themes by
6 help scheme entries saving options in a .scheme file
2 7
plot region y-axis labels grid lines see all options for setting scheme properties

50
y-line 3 adopath ++ "~/<location>/StataThemes"
tick marks set path of the folder (StataThemes) where custom

0
outer region inner region 0 20 40 60 80 100
scatter price mpg, graphregion(fcolor("192 192 192") ifcolor("208 208 208")) x-axis title .scheme files are saved
specify the fill of the background in RGB or with a Stata color x-axis set as default scheme
y2
scatter price mpg, plotregion(fcolor("224 224 224") ifcolor("240 240 240")) legend Fitted values set scheme customTheme, permanently
specify the fill of the plot background in RGB or with a Stata color change the theme
SYMBOLS LINES / BORDERS TEXT net inst brewscheme, from("https://wbuchanan.github.io/brewscheme/") replace
marker arguments for the plot line marker axes tick marks marker label titles axis labels
install William Buchanan’s package to generate custom
<marker objects (in green) go in the <line options> <marker xscale(...) grid lines <marker title(...) xlabel(...)
schemes and color palettes (including ColorBrewer)
SYNTAX

options> options portion of these xline(...) options> yscale(...) options> subtitle(...) ylabel(...) USING THE GRAPH EDITOR
commands (in orange) yline(...) xlabel(...) xtitle(...)
for example: legend ylabel(...) annotation legend
ytitle(...)
scatter price mpg, xline(20, lwidth(vthick)) legend(region(...)) text(...) legend(...) twoway scatter mpg price, play(graphEditorTheme)
mcolor("145 168 208") mcolor(none) lcolor("145 168 208") lcolor(none) color("145 168 208") color(none)
specify the fill and stroke of the marker specify the stroke color of the line or border specify the color of the text
in RGB or with a Stata color Select the
marker mlcolor("145 168 208") marker label mlabcolor("145 168 208")
COLOR

mfcolor("145 168 208") mfcolor(none) Graph Editor


tick marks tlcolor("145 168 208") axis labels labcolor("145 168 208")
specify the fill of the marker
adjust transparency by adding %#
grid lines glcolor("145 168 208") mcolor("145 168 208 %20")

msize(medium) specify the marker size: lwidth(medthick) marker mlwidth(thin) size(medsmall) specify the size of the text:
Click
specify the thickness tick marks tlwidth(thin) marker label mlabsize(medsmall)
(stroke) of a line: Record
ehuge medlarge grid lines glwidth(thin) axis labels labsize(medsmall)
SIZE / THICKNESSS

vhuge
medium vvvthick
vvthick
medthin
thin
Text vhuge Text medsmall
Text small Double click on
medsmall
small vthick vthin
Text huge Text vsmall symbols and areas
huge Text vlarge Text tiny on plot, or regions
vsmall thick vvthin Text half_tiny on sidebar to
vlarge Text large third_tiny customize
tiny medthick vvvthin
Text

Text medlarge Text quarter_tiny


large vtiny medium none Text medium Text minuscule Unclick
Record
msymbol(Dh) specify the marker symbol: line axes lpattern(dash) specify the marker label mlabel(foreign)
line pattern label the points with the values
Save theme
grid lines glpattern(dash) of the foreign variable as a .grec file
O D T S
solid longdash longdash_dot axis labels
APPEARANCE

nolabels
o d t s dash shortdash shortdash_dot no axis labels Save Plots
axis labels format(%12.2f ) graph twoway scatter y x, saving("myPlot.gph") replace
Oh Dh Th Sh dot dash_dot blank change the format of the axis labels save the graph when drawing
oh dh th sh axes noline axes off no axis/labels legend off graph save "myPlot.gph", replace
turn off legend
+ X p none i tick marks noticks tick marks tlength(2) save current graph to disk
legend label(# "label")
grid lines nogrid nogmin nogmax change legend label text graph combine plot1.gph plot2.gph...
combine 2+ saved graphs into a single plot
POSITION

jitter(#) jitterseed(#) tick marks xlabel(#10, tposition(crossing)) marker label mlabposition(5)


graph export "myPlot.pdf", as(.pdf ) see options to set
randomly displace the markers set seed number of tick marks, position (outside | crossing | inside) label location relative to marker (clock position: 0 – 12) export the current graph as an image file size and resolution

Laura Hughes ([email protected]) • Tim Essam ([email protected]) inspired by RStudio’s awesome Cheat Sheets (rstudio.com/resources/cheatsheets) geocenter.github.io/StataTraining updated June 2016
follow us @flaneuseks and @StataRGIS Disclaimer: we are not affiliated with Stata. But we like it. CC BY 4.0

You might also like