10 May 2024 14:15:35
Once we have discerned the type of variable that have, there are two followup questions we may ask before deciding upon a chart strategy:
We are going to use the famous “iris” data collected by Edgar Anderson in the early 20th Century.
. use "iris.dta", clear . . summarize Variable │ Obs Mean Std. dev. Min Max ─────────────┼───────────────────────────────────────────────────────── Sepal_Length │ 150 5.843333 .8280661 4.3 7.9 Sepal_Width │ 150 3.057333 .4358663 2 4.4 Petal_Length │ 150 3.758 1.765298 1 6.9 Petal_Width │ 150 1.199333 .7622377 .1 2.5 Species │ 150 2 .8192319 1 3
The
iris
data set has 5 variables.
Iris species images courtesy Wikipedia.
histogram
. histogram Petal_Length (bin=12, start=1, width=.49166667)
graph bar
. graph bar, over(Species)
twoway
. twoway scatter Petal_Length Petal_Width
graph bar
. recode Petal_Length /// > (min/3.758 = 0 "below mean") /// > (3.758/max = 1 "above mean"), /// > generate(Petal_Group) // dichotomize Petal_Length (150 differences between Petal_Length and Petal_Group) . . graph bar, over(Species) over(Petal_Group)
graph bar
. graph bar Petal_Length, over(Species)
, title(...) xtitle(...) ytitle(...)
. twoway scatter Petal_Length Petal_Width, scheme(s1rcolor) /// > title("Petal Length by Petal Width") /// > xtitle("Petal Width") ytitle("Petal Width") /// > caption("Iris Data")
,scheme(...)
The easiest method to make better Stata graphs is through the use of predefined Stata graphing schemes.
Some schemes, e.g. economist
, sj
,
s1color
, and s1rcolor
are pre-installed with
Stata.
. twoway scatter Petal_Length Petal_Width, scheme(economist)
. twoway scatter Petal_Length Petal_Width, scheme(sj)
s1color
Scheme. twoway scatter Petal_Length Petal_Width, scheme(s1color)
s1rcolor
Scheme. twoway scatter Petal_Length Petal_Width, scheme(s1rcolor)
Two of the best user written schemes are plottig
and
lean2
.
Use the findit
command e.g. findit lean2
to
find these schemes.
lean2
Scheme. twoway scatter Petal_Length Petal_Width, scheme(lean2)
I have written a michigan
graph scheme described here.
. twoway (scatter Petal_Length Petal_Width) /// > (lfit Petal_Length Petal_Width), scheme(michigan)
Schemes can be used as a base that can then be further modified.
. twoway (scatter Petal_Length Petal_Width, msymbol(0) mcolor(red)) /// > (lfit Petal_Length Petal_Width), /// > scheme(lean2) (note: named style 0 not found in class symbol, default attributes used)
Based upon an example at https://blog.stata.com/2018/10/02/scheming-your-way-to-your-favorite-graph-style/
. twoway scatter Sepal_Length Sepal_Width Petal_Width Petal_Length, /// > color(%50 %50 %50) /// transparency > title("Multiple Iris Characteristics") /// title > scheme(s1rcolor) // scheme
See also Two Page Stata
Created by agrogan@umich.edu