Below are some Stata graphs. These are presented as they have used Stata in some interesting ways.
From time to time more graphs will be added to this page. The graph below have generally be taken from the Statalist and in some cases the code has been slightly modified.
To see the original Statalist entry do a Google search on a line of code.
sysuse nlsw88, clear
generate g=max(grade,8)
label var g "Years of education"
rename married m
collapse wage, by(g m)
generate w=round(wage,.01)
summarize wage, meanonly
generate c=round((wage-r(min))/(r(max)-r(min))*255)
quiet levelsof c, loc(cs)
local g
foreach c of loc cs {
local c1=120+round(`c'/2)
local c2=255-round(`c'/2)
local m mc("`c2' `c2' `c1'")
local g `g'||sc m g if c==`c', ms(S) msize(ehuge) `m'
}
local g `g'||sc m g, ms(i) mlab(w) mlabp(0)
local g `g' leg(off) yla(-.75 " " 0 "N" 1 "Y" 1.75 " ", notick)
local g `g' xla(7 " " 8/18 19 " ", notick)
twoway `g' scheme(s1mono) ti(Wage heat map) xsize(8) ysize(5)
clear
input input low high mean
1 1.6 2.2 1.9
2 1.5 2.3 1.9
5 2.9 0.01 1.9
3 1.0 2.9 1.9
4 0.5 3.2 1.9
end
label var input "Input"
label var low "Low value"
label var high "High value"
label var mean "Mean"
label define inputlbl ///
1 "Annual Donations in U.S." ///
2 "Seroprevalence" ///
3 "Exposures per Donation" ///
4 "Probability of Transmission" ///
5 "Sensitivity of Test"
label val input inputlbl
gen sens = high-low
sort sens
local ml1 = (2.2+1.9)/2
local mh1 = (1.6+1.9)/2
local ml2 = (2.3+1.9)/2
local mh2 = (1.5+1.9)/2
local ml3 = (2.9+1.9)/2
local mh3 = (1.0+1.9)/2
local ml4 = (3.2+1.9)/2
local mh4 = (0.5+1.9)/2
local ml5 = (2.9+1.9)/2
local mh5 = (0 +1.9)/2
local format fcolor(white) lcolor(white)
twoway ///
(rbar low mean input, horizontal) ///
(rbar mean high input, horizontal), ///
ylabel(, val angle(horizontal)) xsize(7.1) ysize(5.2) scheme(sj) ///
xtitle(" " "Predicted Cases of Transfusion-Transmitted T. Cruzi") ///
title("Sensitivity of Results to Inputs") subtitle("Test Once Scenario") ///
legend(label(1 "Low Input Values") label(2 "High Input Values") rows(2)) ///
text(5 `ml5' "93.5%", box j(center) margin(l+1 r+1 t+1 b+1) `format') ///
text(5 `mh5' "99.99%", box j(center) margin(l+1 r+1 t+1 b+1) `format') ///
text(4 `ml4' "5%", box j(center) margin(l+1 r+1 t+1 b+1) `format') ///
text(4 `mh4' "0.8%", box j(center) margin(l+1 r+1 t+1 b+1) `format') ///
text(3 `ml3' "3", box j(center) margin(l+1 r+1 t+1 b+1) `format') ///
text(3 `mh3' "1", box j(center) margin(l+1 r+1 t+1 b+1) `format') ///
text(2 1.4 "1:28,600", box margin(l+1 r+1 t+1 b+1) `format' ///
placement(9) j(right)) ///
text(2 2.4 "1:18,200", box margin(l+1 r+1 t+1 b+1) `format' ///
placement(3) j(left)) ///
text(1 1.5 "13.4 million", box margin(l+1 r+1 t+1 b+1) `format' ///
placement(9) j(right)) ///
text(1 2.4 "19 million", box margin(l+1 r+1 t+1 b+1) `format' ///
placement(3) j(left))
// Creating axis
sysuse auto, clear
gen y=(price-5000)/1000
gen x=mpg-20
local x `"||scatteri 0 -10 "-10" 0 10 "10" 0 20 "20","'
local x `"`x' mlabpos(6) mlabsize(*1.25)"'
local x `"`x' msymbol(none) mlabcolor(black)"'
local x `"`x'||scatteri 0 -10 "|" 0 10 "|" 0 20 "|" ,"'
local x `"`x' mlabpos(0) msymbol(none) mlabcolor(black)"'
local y `"||scatteri -3 0 "-3" 3 0 "3" 6 0 "6" 9 0 "9","'
local y `"`y' mlabpos(9) mlabsize(*1.25)"'
local y `"`y' msymbol(none) mlabcolor(black)"'
local y `"`y'||scatteri -3 0 "_" 3 0 "_" 6 0 "_" 9 0 "_","'
local y `"`y' mlabpos(0) msymbol(none) mlabcolor(black)"'
local ax "yli(0,lc(black) lw(thin)) xli(0,lc(black) lw(thin))"
local ax "`ax' ysc(off) xsc(off) leg(off)"
scatter y x,`ax'`x'`y' scheme(s2mono) graphr(fc(white)) yla(,nogrid)
// Creating axis in the middle of a graph
sysuse auto, clear
scatter price weight if weight<3000, graphregion(margin(right(-10))) ///
xscale(range(2000 3010)) plotregion(margin(0)) ///
name(first, replace) yscale(off) nodraw
scatter price weight if weight>3000 , graphregion(margin(left(-10))) ///
xscale( range(3010 5000)) plotregion(margin(0)) ///
name(second, replace) xtitle(,color(bg) ) nodraw
graph combine first second
// Controlling the side of the graph that the axis is on
sysuse auto, clear
twoway ///
(histogram mpg, width(5) yscale(alt axis(1)) ) ///
(line weight mpg, yaxis(2) yscale(alt axis(2)) sort)
// Shade Part of a Graph
sysuse auto, clear
summarize price, mean
local mean = r(mean)
kdensity price, gen(x h)
line h x, || area h x if x < `mean'
// Grid on top of graph
ssc install freduse, replace //gets program online
freduse MPRIME, clear
generate ym = mofd(daten)
tsset ym, monthly //sets for time series
twoway ///
function y=20.705,range(119 130) recast(area) color(gs12) base(4.7025) || ///
function y=20.705,range(166 182) recast(area) color(gs12) base(4.7025) || ///
function y=20.705,range(240 274) recast(area) color(gs12) base(4.7025) || ///
function y= 5 ,range(119 274) lstyle(grid) || ///
function y=10 ,range(119 274) lstyle(grid) || ///
function y=15 ,range(119 274) lstyle(grid) || ///
function y=20 ,range(119 274) lstyle(grid) || ///
function y=ym(1980,11), range(4.7025 20.705) horizontal lstyle(grid) || ///
tsline MPRIME if tin(1970m1,1990m1), xlabel(,format(%tm)) lstyle(p1) ///
legend(order(5 1 "Recession")) tlabel(,grid) scheme(s2color)
*(Maarten Buis - Stata list, 30 October 2011)
// Shade Part of a Graph
sysuse auto, clear
sort mpg
generate x=_n
gen max=ceil(_N/10)*10
summarize mpg, detail
twoway ///
(area max x if inrange(x, `=r(p1)', `=r(p25)'), ///
lcolor(bg) fcolor(green) fintensity(inten20)) ///
(area max x if inrange(x, `=r(p75)', `=r(p99)'), ///
lcolor(bg) fcolor(green) fintensity(inten20)) ///
(line x mpg),legend(off) plotregion(margin(zero)) ///
xtitle("MPG") ///
xlabel(10 `=r(p1)' `=r(p25)' `=r(p75)' `=r(p99)' ) ///
yscale(range(0 `=_N'))
// Graph - Slide
twoway ///
(scatteri 6 0 "Kinds of graphs Stata can make", mlabsize(huge) pstyle(p1)) ///
(scatteri 5 0 "Schemes for controlling look of graphs", mlabsize(huge) ///
pstyle(p1)) ///
(scatteri 4 0 "Structure of graph commands", mlabsize(huge) pstyle(p1)) ///
(scatteri 3 0 "graph options", mlabsize(huge) pstyle(p1)) ///
(scatteri 2 0 "Building complex graphs", mlabsize(huge) pstyle(p1)) ///
(scatteri 1 0 "Additional examples", mlabsize(huge) pstyle(p1)), ///
title("Overview") ///
xlabel(-.05 1) ylabel(-1 7) ///
xscale(off) yscale(off) ///
legend(off) ///
note("Stata graphics are fun and easy" "Stata graphics are powerful", ///
ring(0))
// stacked bar graph
sysuse auto, clear
twoway__histogram_gen mpg if foreign, gen(freq_foreign x) ///
freq start(0) width(2)
drop if missing(x)
keep freq_foreign x
save a1, replace
sysuse auto, clear
twoway__histogram_gen mpg if !foreign, gen(freq_domestic x) ///
freq start(0) width(2)
drop if missing(x)
keep freq_domestic x mpg
merge 1:1 x using a1
mvencode freq_domestic freq_foreign , mv(.=0)
egen freq = rowtotal(freq_domestic freq_foreign )
twoway ///
(bar freq_domestic x if freq < ., barw(2) bcolor(blue) xsc(r(10 .)) ) ///
rbar freq_domestic freq x if freq < ., barw(2) bcolor(red) ///
legend(order(1 "Domestic" 2 "Foreign") pos(11) col(1) ring(0)) ///
xtitle("`: var label mpg'") ytitle(Frequency) xla(10(5)45, alt) yla(, ang(h)) ///
xsc(r(10 .))
// box plot
sysuse auto, clear
statsby "su mpg" mean=r(mean) max=r(max) min=r(min) sd=r(sd), by(rep78)
// here k = 1; tune to choice
gen meanpsd = mean + sd
gen meanmsd = mean - sd
// use -line- or -connected- not -scatter-
twoway scatter mean rep78 || ///
rbar mean max rep78, bcolor(none) barw(0.2) || ///
rbar mean min rep78, bcolor(none) barw(0.2) || ///
rbar meanmsd meanpsd rep78 , barw(0.1) bcolor(red) || , ///
legend(off) ytitle(Miles per gallon) yla(, ang(h)) ///
note(red bars: mean +/- sd; empty bars show range)
// Candle Stick plot
clear
input ///
str3 share str20 time price
abc "1/5/2011:10:34" 10.34
abc "1/5/2011:10:35" 10.25
abc "1/5/2011:10:36" 10.15
abc "1/5/2011:10:37" 10.73
abc "1/5/2011:10:39" 10.95
abc "1/5/2011:10:41" 10.53
abc "1/5/2011:10:45" 10.99
abc "1/5/2011:10:49" 11.05
abc "2/5/2011:10:34" 9.34
abc "2/5/2011:10:35" 9.25
abc "2/5/2011:10:36" 9.15
abc "2/5/2011:10:37" 9.73
abc "2/5/2011:10:39" 9.95
abc "2/5/2011:10:41" 9.53
abc "2/5/2011:10:45" 9.99
abc "2/5/2011:10:49" 9.05
abc "3/5/2011:10:34" 8.34
abc "3/5/2011:10:35" 8.25
abc "3/5/2011:10:36" 8.15
abc "3/5/2011:10:37" 8.73
abc "3/5/2011:10:39" 8.95
abc "3/5/2011:10:41" 8.53
abc "3/5/2011:10:45" 8.99
abc "3/5/2011:10:49" 12.05
abc "4/5/2011:10:34" 12.34
abc "4/5/2011:10:35" 12.25
abc "4/5/2011:10:36" 12.15
abc "4/5/2011:10:37" 12.73
abc "4/5/2011:10:39" 12.95
abc "4/5/2011:10:41" 12.53
abc "4/5/2011:10:45" 12.99
abc "4/5/2011:10:49" 17.05
end
l
gen double date=dofc(clock(time,"DMY hm"))
format date %td
collapse (p1) p1=price (p25) p25=price ///
(p50) p50=price (p75) p75=price ///
(p99) p99=price, by(date)
gen uav=p75- 1.5*(p25-p75)
gen lav=p75+ 1.5*(p25-p75)
gen col=2
replace col=(p50-p50[_n-1])>0 if _n!=1
twoway (rbar p75 p25 date if col==0, barwidth(.6) color(red)) ///
(rbar p75 p25 date if col==1, barwidth(.6) color(green)) ///
(rbar p75 p25 date if col==2, barwidth(.6) color(black)) ///
(rspike uav lav date if col==2, lwidth(thick) color( black)) ///
(rspike uav lav date if col==1 , lwidth(thick) color( green)) ///
(rspike uav lav date if col==0 , lwidth(thick) color(red) ) , ///
legend(off)
// Extrapolate
sysuse auto, clear
regress mpg weight
gen x= weight
sort x
twoway (lfit mpg weight, lwidth(vthick) color(black)) ///
(function y = _b[weight]*x+_b[_cons], range(0 8000)) ///
(scatter mpg weight)
// Area Plot
// prepare the example data
sysuse nlsw88, clear
gen ind_gr = industry
recode ind_gr 1/5=1 6=2 7=3 8/10=4 11=5 12=6
label define ind_gr 1 "manual" ///
2 "trade" ///
3 "finance" ///
4 "other services" ///
5 "professional services" ///
6 "public administration"
label value ind_gr ind_gr
// compute percentile ranks
egen n = count(wage)
egen i = rank(wage)
gen hazen = (i - 0.5) / n * 100
label variable hazen "percentile rank of income"
// smooth the proportions
mkspline s_w=hazen, cubic nknots(5)
mlogit ind_gr s_w*
predict pr*
// create the graph
gen zero = 0
gen one = 100
gen l1 = (pr1)*100
gen l2 = (pr1 + pr2)*100
gen l3 = (pr1 + pr2 + pr3)*100
gen l4 = (pr1 + pr2 + pr3 + pr4)*100
gen l5 = (pr1 + pr2 + pr3 + pr4 + pr5)*100
sort hazen
// collect the labels for the second y-axis
local mid = l1[_N]/2
local yaxis `"`mid' "manual""'
local mid = (l2[_N]-l1[_N])/2 + l1[_N]
local yaxis `"`yaxis' `mid' "trade""'
local mid = (l3[_N]-l2[_N])/2 + l2[_N]
local yaxis `"`yaxis' `mid' "finance""'
local mid = (l4[_N]-l3[_N])/2 + l3[_N]
local yaxis `"`yaxis' `mid' "other services""'
local mid = (l5[_N]-l4[_N])/2 + l4[_N]
local yaxis `"`yaxis' `mid' "professional services""'
local mid = (100-l5[_N])/2 + l5[_N]
local yaxis `"`yaxis' `mid' "public administration""'
twoway rarea zero l1 hazen, yaxis(1) || ///
rarea l1 l2 hazen, yaxis(2) || ///
rarea l2 l3 hazen || ///
rarea l3 l4 hazen || ///
rarea l4 l5 hazen || ///
rarea l5 one hazen, ///
ytitle("percentage") ///
ylab(`yaxis', axis(2) angle(-45)) ///
yscale(range(0 100) axis(1)) ///
yscale(range(0 100) axis(2)) ///
ytitle("", axis(2)) ///
plotregion(margin(zero)) ///
aspect(1) ///
legend(off)
// 45 degree line
sysuse auto, clear
sum weight, meanonly
local max = r(max)
summarize price, meanonly
if r(max) > `max' {
local max = r(max)
}
scatter weight price || scatteri 0 0 `max' `max', recast(line) name(a)
// Fourier plot
clear
set obs 8
gen x=(_n-1)*(2*_pi)/8
gen y=sin(x)
mata: h=st_data(.,"y")
mata: fft(h)
# delimit ;
twoway scatter y x ||
function y=1/8*(4*sin(1*x)-4*sin( 7*x)), range(x) ||
function y=1/8*(4*sin(1*x)-4*sin(-1*x)), range(x) ||,
legend(cols(1) label(1 "sampled values")
label(2 "sum of high frequency components")
label(3 "sum of low frequency components"))
title("Reconstructed signal for y=sin(x) sampled at 8 points");
# delimit cr
// histogram - add x bar labels for the centre of each bar
sysuse auto, clear
twoway__histogram_gen mpg , bin(6) generate(h x , replace )
count if !missing(x)
forvalues i=1/`=r(N)' {
local label `label' `=string(x[`i'],"%8.2f")'
}
histogram mpg , bin(6) xlabel(`label')
// Including a distribution on top of a histogram.
//This is easily done with Stata's: graph histogram
//and the normal options. However, this example
//show this with the twoway histogram.
sysuse auto, clear
summarize mpg
local width=4 //<-----change width as required
twoway (histogram mpg, width(`width') freq) ///
(function y =`width'*`=_N'* normden(x, `r(mean)',`r(sd)'), ///
range(10 45)), name(a1, replace) ytitle("frequency") ///
xtitle("mpg") legend(off)
// Including a distribution on top of a histogram.
//The normal option for graph histogram uses the dataset's mean
//and standard deviation.
//This method allows you to specify these.
sysuse auto, clear
histogram mpg , addplot(function normalden(x,26,10), ra(10 42))
*(Nick Cox - Stata list, Fri, 03 June 2011)
// Graph Title - Includes years
clear
inp str10 date ricepr
"01/01/2006" 700
"01/02/2006" 700
"01/03/2006" 900
"01/04/2006" 900
"01/05/2006" 900
"01/06/2006" 900
"01/07/2006" 900
"01/08/2006" 933.33
"01/09/2006" 1000
"01/10/2006" 1000
"01/11/2006" 1000
"01/12/2006" 1000
"01/01/2007" 1050
"01/02/2007" 1087.5
"01/03/2007" 1100
"01/04/2007" 1100
"01/05/2007" 1100
"01/06/2007" 1100
"01/07/2007" 1175
"01/08/2007" 1200
"01/09/2007" 1200
"01/10/2007" 1300
"01/11/2007" 1400
"01/12/2007" 1400
end
gen int year =yofd(date(date, "DMY"))
gen byte aux=_n
//from http://www.stata-journal.com/sjpdf.html?articlenum=pr0013
//follow special sequences
//generate label for months
token `c(Mons)'
forv i = 1/24{
if `i'==1 la def month `i' "`=year[`i']' ``i''" , modify
if inrange(`i',2,12) la def month `i' "``i''" , modify
if `i'==13 la def month `i' "`=year[`i-12']' ``=`i'-12''" , modify
if `i'>13 la def month `i' "``=`i'-12''" , modify
}
label value aux month
twoway (line ricepr aux), xlabel(#24, labsize(small) ///
angle(forty_five) valuel) xtitle("")
*(Martin Weiss - Stata list, Sun, 02 May 2010)
// Graph Title - Includes years
clear
inp str10 date ricepr
"01/01/2006" 700
"01/02/2006" 700
"01/03/2006" 900
"01/04/2006" 900
"01/05/2006" 900
"01/06/2006" 900
"01/07/2006" 900
"01/08/2006" 933.33
"01/09/2006" 1000
"01/10/2006" 1000
"01/11/2006" 1000
"01/12/2006" 1000
"01/01/2007" 1050
"01/02/2007" 1087.5
"01/03/2007" 1100
"01/04/2007" 1100
"01/05/2007" 1100
"01/06/2007" 1100
"01/07/2007" 1175
"01/08/2007" 1200
"01/09/2007" 1200
"01/10/2007" 1300
"01/11/2007" 1400
"01/12/2007" 1400
end
gen date2=date(date,"DMY")
gen monthlabel = substr("JFMAMJJASOND", month(date2), 1)
replace monthlabel=monthlabel+ " " + string(year(date2),"%5.0f") ///
if month(date2)==1
labmask date2, values(monthlabel) //user written program, must be installed first
levelsof date2, local(levels)
line ricepr date2, xla(`levels', valuelabel angle(45))
*(Nick Cox - Stata list, Tue, 04 May 2010)
// Controlling date labels
clear
input str40 time x
"5jan2011 15:00:30" 1
"1feb2011 17:30:32" 2
"1march2011 18:10:30" 6
"1april2011 05:05:01" 2
"1march2011 15:10:30" 12
end
list
generate double time1=clock(time,"DMY hms")
local t1=tc(1jan2011 12:00:0)
local t2=1000*60*60*24*10
local t3=tc(1may2011 12:0:0)
scatter x time1, xlab(`t1'(`t2')`t3' , ///
format(%tcddmonccyy_hh:mm) angle(45)) xtitle("Time")
*(Nick Cox - Stata list, Monday, 23 Jan 2011)
// Gap between bars in bar graph
sysuse auto, clear
graph bar (mean) price turn mpg weight, over(rep78)
set obs 75
replace rep78=2.5 in 75
graph bar (mean) price turn mpg weight, over(rep78, relabel( 3 " " ))
*(Nick Cox - Stata list, Wed, 26 May 2010)
// Including totals in bar graph
sysuse auto, clear
save auto1 , replace
replace for=3
label define origin 3 Total, add
append using auto1
graph bar (mean) price , over(rep78, ) over(for) showyvars ///
ascategory asyvar legend(off)
exit
// Stacking some levels of a variable and not others
// Has the years 71 and 77 stacked and year 88 normaly displayed
webuse union, clear
keep idcode year age
recode year (71 77=1) (88=2) (*=.), gen(which)
graph bar age, over(year) over(which) asyvars stack
exit
Statalist
Nick Cox 22/12/11
// Coloring bars in order of magnitude. All bars not covered by the first and
// second places are considered (colored) as 3rd place)
clear
input country gdp
1 100
2 110
3 240
4 50
5 10
end
list
gsort -gdp
gen gdpred=gdp if _n==1
gen gdpgreen=gdp if _n==2
gen gdpblue=gdp if _n>2
list
graph hbar gdpred gdpgreen gdpblue, over(country, sort(gdp) ///
descending) nofill legend(label(1 "Best") label(2 "2nd best") ///
label(3 "rest") rows(1)) bar(1, color(cranberry)) ///
bar(2, color(dkgreen)) bar(3, color(dknavy))
*(Sergiy Radyakin - Stata list, Sat, 19 Feb 2011)
// Changing bar labels
sysuse auto, clear
graph bar mpg gear, showyvars ///
yvaroptions(relabel(1 "MPG" 2"GEAR RATIO")) legend(off)
exit
// Changing bar labels
//It is sometimes more convenient to use value lables rather than a graph
//label options to change graph bar labels. In the example it also allows the
//legend to be spread over the width of the graph.
clear all
sysuse auto
lab def origin 0 "Europe de l`=char(146)'Ouest" ///
1 "Asie de l`=char(146)'Est", modify
graph hbar mpg trunk turn, over(foreign) ///
legend(row(1) span) stack name(two,replace)
exit
*( Stata list, 25 Jun 2011)
// Changing bar labels using the Stata graph editor
// The type of bar label is limited in Stata graphs but sometimes
// additional information is required. This can be added with the
// Stata graphics editor commands.
// Below the number of observation for each region is added.
sysuse citytemp, clear
#delimit ;
graph bar tempjuly tempjan, over(region) bargap(-30)
legend( label(1 "July") label(2 "January") )
ytitle("Degrees Fahrenheit")
title("Average July and January temperatures")
subtitle("by regions of the United States")
note("Source: U.S. Census Bureau, U.S. Dept. of Commerce")
blabel(bar, position(inside) format(%9.1f) color(white)) ;
#delimit cr
generate No=_n
collapse (sum) N, by(region)
list
local i1=1
forvalues i=1/`=_N' {
gr_edit plotregion1.barlabels[`i1'].text = {}
gr_edit plotregion1.barlabels[`i1'].text.Arrpush N=`=No[`i']'
local ++i1
gr_edit plotregion1.barlabels[`i1'].text = {}
gr_edit plotregion1.barlabels[`i1'].text.Arrpush N=`=No[`i']'
local ++i1
}
exit
// Adding bar labels
// The type of bar label is limited in Stata graphs but sometimes
// additional information is required. This can be added with the
// by adding another twoway plot.
sysuse census, clear
graph bar (sum) marri div, over(region) stack
more
collapse (sum) marri div, by(region)
gen ratio = div / (marri + div)
gen sratio = string(ratio, "%4.3f")
gen marriPdiv = marria + div
twoway bar marria region, barw(0.6) base(0) ///
|| rbar marria marriPdiv region, barw(0.6) xla(1/4, noticks valuelabel) ///
|| scatter marriPdiv region, ms(none) mla(sratio) mlabpos(12) ///
legend(order(1 "marriages" 2 "divorces")) ///
yla(0 4e5 "400" 8e5 "800" 12e5 "1200" , ang(h)) ytitle(thousands) ///
ysc(r(. 14e5))
*(Nick Cox - Stata list, Thu, 17 Feb 2011)
// Adding average line to bar graph
webuse grunfeld, clear //getting data online
keep if company == 1
keep if year < 1945
generate year2 = year + 0.3
generate year3 = year - 0.3
generate average = (invest + mvalue + kstock) / 3
twoway bar invest year3, barw(0.3) || ///
bar mvalue year, barw(0.3) || ///
bar kstock year2 , barw(0.3) || ///
line average year , name(a2, replace)
*(Nick Cox - Stata list, 13/4/11)
// Adding bar labels in bold font
// Adding bold font using the Stata editor commands. This is produced
// using a program.
program g1
syntax varname, over(varname)
levelsof `over', local(kk)
graph bar `varlist' , over(`over') ///
blabel(bar, position(base) gap(*22.5) format(%9.1f) ///
size(large) color(black))
local x=1
foreach i of local kk {
summ `varlist' if `over'==`i'
local a1=string(r(mean),"%8.0f")
gr_edit plotregion1.barlabels[`x'].text = {}
gr_edit plotregion1.barlabels[`x'].text.Arrpush `"{bf: `a1' }"'
local ++x
}
end
sysuse auto, clear
g1 price , over(rep78)
// Using stack to produce a graph
use http://www.stata-press.com/data/r10/auto, clear
preserve
tempfile mylabel
label save origin using `mylabel'
// map 1..5 -> 3..7 to ensure offset from -foreign-'s values of 0 1
replace rep78 = rep78 + 2
stack price foreign weight rep78, into(data foreign) clear
// bring back labels
label def _stack 1 "`l1'" 2 "`l2'"
label val _stack _stack
do `mylabel'
// reverse -rep78- offset in labels
label def origin 3 "1" 4 "2" 5 "3" 6 "4" 7 "5", add
label val foreign origin
// now...
graph hbox data, over(foreign)
*(Nick Cox - Stata list, Thu, 8 Nov 2007)
// Labelling the over variable
sysuse auto, clear
levelsof for, local(a)
levelsof rep78, local(a1)
tempfile file
foreach i of local a { //for
foreach i1 of local a1 { //rep78
sysuse auto, clear
count if for==`i' & rep78==`i1' //making sure there are obs to regress
if r(N)>=1 {
regress mpg weight if for==`i' & rep78==`i1'
parmest, norestore
egen axis= axis(estimate), label(parm)
gen for=`i'
gen rep78=`i1'
capture append using `file'
save `file', replace
local kk=`i1'+1
}
}
} //end loop
summarize rep78
foreach i of local a { //for
set obs `=_N+1'
replace for=`i' if for==.
local max1=`r(max)'
replace rep78=`max1' +1 if rep78==.
}
gsort for -rep78
drop if parm=="_cons"
gen order=_N-_n
label define jj 9 "foreign=0 " ///
3 "foreign=1 " ///
0 "rep78_3" ///
1 "rep78_4" ///
2 "rep78_5" ///
4 "rep78_1" ///
5 "rep78_2" ///
6 "rep78_3" ///
7 "rep78_4" ///
8 "rep78_5"
label value order jj
twoway (scatter order est , msymbol(none)) ///
(scatter order est ) ///
(rcap min95 max95 order, horizontal ), ///
ylabel(0/9, valuelabel angle(0)) legend(off) ytitle("rep78 over foreign") ///
xtitle("Coefficients and confidence interval")
exit
// Text that varies in size due to a calculated factor
clear programs
*--------------- begin example ---------------------
program s
sysuse auto, clear
tempname hdle
tempfile info
postfile `hdle' str20 variable missings using `info'
qui ds, has(type numeric)
qui foreach var in `r(varlist)'{
/*want more missings
to make this interesting */
replace `var'=. if runiform()<0.1
qui count if `var'==.
post `hdle' ("`var'") (r(N))
}
postclose `hdle'
use `info', clear
gen number=_n
list, noobs
gen x = 1
sum missings, meanonly
gen factor = missings/r(mean)
levelsof variable, local(aa)
local a=1
foreach i of local aa {
local f = factor[`a']
local txt `"`txt' text(`a' 1 "`i'", size(*`f'))"'
capture `++a'
}
scatter number x, msymbol(i) ///
xtitle("") xscale(off) ///
ytitle("") yscale(off) ///
plotregion(lstyle(none)) ///
`txt'
*----------------- end example -----------------------------
end
s //calls program "s" above
// Double graph - if there is too much detail for one
// graph then 2 graphs can be used
clear
set more off
use "http://www.stata-press.com/data/r11/nlswork2.dta"
describe
replace year=1900+year
gen age11= yofd(date(c(current_date)),"DMY") -(1900+birth_yr)
recode age11 (56/58=1) (59/61=2) (62/64=3) (65/67=4) (68/70=5)
collapse (count) union (count) idcode ,by(year age11)
gen per=(union/idcode)*100
twoway ///
(line per year if age11==1 , lpattern(dash) connect(l)) ///
(line per year if age11==2 , lpattern(dash_dot) connect(l)) ///
(line per year if age11==3 , lpattern(dot) connect(l)) , ///
name(a1,replace) legend( bmargin(l=40) stack region(style(none)) ///
label( 1 "56-58") ///
label( 2 "59-61") ///
label( 3 "62-64") ///
rows(1) ) xtitle("") ///
xlabel(1968 " 1968" 1978 "1978 ", noticks)
twoway ///
(line per year if age11==4 , yaxis(1) lpattern(solid) connect(l)) ///
(line per year if age11==5 , yaxis(1) lpattern(longdash) connect(l)), ///
name(a2,replace) legend( bmargin(r=40) stack region(style(none)) ///
label(1 "65-67") ///
label( 2 "68-70") ///
rows(1) ) xtitle("") ///
xlabel(1968 " 1968" 1978 "1978 ", noticks) ///
yscale(alt) xline(1968)
graph combine a1 a2 , imargins(zero) ycommon
//Graphing the mean of a variable by other variables
sysuse auto, clear
collapse (mean) mpg , by(for price)
twoway ///
(line mpg price if for==0) ///
(line mpg price if for==1) ///
,legend( label(2 "foreign" ) label( 1 "domestic" ))
// Difference from Average by levels of groups
sysuse auto, clear
graph drop _all
bysort for rep78 : egen mpg1=mean(mpg)
bysort rep78: egen mpg2=mean(mpg)
gen mpg3=mpg1-mpg2
replace mpg2=0 if rep78==.
twoway ///
(bar mpg3 rep78 if for==0 & rep78==1 ,fcolor(green) ///
lcolor(green) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==0 & rep78==2 ,fcolor(blue) ///
lcolor(blue) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==0 & rep78==3 ,fcolor(red) ///
lcolor(red) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==0 & rep78==4 ,fcolor(pink) ///
lcolor(pink) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==0 & rep78==5 ,fcolor(cyan) ///
lcolor(cyan) barwidth(.5)horiz yscale(noline)) , ///
xtitle(MPG) xline(0) ///
title("Foreign made cars") legend(off) name(k1, replace)
twoway ///
(bar mpg3 rep78 if for==1 & rep78==1 ,fcolor(green) ///
lcolor(green) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==1 & rep78==2 ,fcolor(blue) ///
lcolor(blue) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==1 & rep78==3 ,fcolor(red) ///
lcolor(red) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==1 & rep78==4 ,fcolor(pink) ///
lcolor(pink) barwidth(.5)horiz yscale(noline)) ///
(bar mpg3 rep78 if for==1 & rep78==5 ,fcolor(cyan) ///
lcolor(cyan) barwidth(.5)horiz yscale(noline)) , ///
xtitle(MPG) xline(0) yscale(off) ///
title("Domestically made cars") legend(off) name(k2, replace)
graph combine k1 k2, row(1) imargin(10 0 0 0) ycommon ///
title("Difference from average of mpg for " ///
"Foreign or Domestically produced cars" " " )
// Stacked bar graph - reshaping the data
//creating the data
clear
set obs 1000
egen id = seq(), block(10)
egen time = seq(), to(10)
format time %tq
gen time2=string(time, "%tq")
gen sector = ceil(3 * runiform())
bysort id: replace sector=sector[1]
label define sector 1 "SECTOR 1" 2 "SECTOR 2" 3 "SECTOR 3"
label values sector sector
gen sales=exp(rnormal())*1000
gen sales2=2*sales
//reshaping the data
gen obs = _n
rename sales sales1
reshape long sales, i(obs) j(which)
graph bar (sum) sales , over(sector) over(which) ///
over(time2, label(labsize(vsmall))) asyvars stack legend(size(vsmall))
// Stacked bar graph - percentage
sysuse auto, clear
gen a=_n
collapse a, by(for rep78)
drop if missing(for, rep78)
bysort for rep78: gen sum = sum(a)
replace a=0 if a==.
levelsof rep78, local(aa)
generate csum=.
foreach i of local aa {
summ a if `i'==rep78
replace csum=a/`r(sum)' if `i'==rep78
}
graph bar (sum)csum , over(for) over(rep78) ///
asyvars stack legend(size(vsmall)) per blabel(bar, ///
position(center) format(%3.1f))
*(Also see the user written program catplot on:
http://www.survey-design.com.au/Usergraphs.html
)
// Editing a graph with the gr_edit command
//The graph with the y axis line
sysuse auto, clear
graph hbar weight price, yscale(off)
//The graph with the y axis line removed
sysuse auto, clear
graph hbar weight price, ysc(off)
gr_edit .varaxis.style.editstyle linestyle(color(none)) editcopy
//The above can also be produced with:
graph hbar weight price ,showyvar yvaroptions(axis(lc(none)))
From the Statalist:
Kieran McCaul
11/6/2010
David Airey
11/6/2010
// Breaking up text automatically in a long note
webuse auto, clear
set more off
//input the text to place in the graph - can be on one line if you wish
local z1 "The development of Linux is one of the most prominent"
local z2 " examples of free and open source software"
local z3 "local z3 collaboration; typically all the underlying source code"
local z4 "can beused, freely modified, and redistributed, both commercially"
local z5 "and non-commercially, by anyone under licenses such as the GNU"
local z6 "General Public License."
local z7 "Typically Linux is packaged in a format known as a Linux"
local z8 "distribution for desktop and server use."
local total "`z1' `z2' `z3' `z4' `z5' `z6' `z7' `z8'"
local size=40 //adjust for length of line
local t1
forvalues i=1/12 { //increase the 12 if more lines required
local a`i' : piece `i' `size' of "`total'" //extended macro function
local t1 `"`t1' `=char(34)+ "`a`i''"+char(34)' "'
}
graph bar price,bargap(-30) over(foreign) ///
note( `t1' , margin(large) justification(left))
exit
// Breaking up long labels
sysuse auto, clear
#delimit
label define origin 0 `"group0 group0 group0 group0 group0
group0 group0 group0 group0 group0"', modify;
label define origin 1 "group1 group1 group1 group1 group1
group1 group1 group1 group1 group1 ", modify;
#delimit cr
local relabels
local relabels1
levelsof for, local(groups)
local s_len=20 //Change to required length
foreach g of local groups {
local label : label origin `g'
local len : length local label
if `len'>`s_len' {
forvalues i=1/ `=`len'/`s_len'+1' {
local p1 : piece `i' `s_len' of `"`label'"', nobreak
local relabels `"`relabels' `=char(34)+ "`p1'" +char(34)' "'
}
local relabels1 `relabels1' `=`g'+1' `"`relabels'"'
local relabels
}
}
graph hbar mpg, over(for, relabel(`relabels1' ))
exit
// Table on the side of a graph
sysuse auto, clear
forval n = 1/20 {
loc sidetable `" `sidetable' " `=make[`n']' " "'
}
forval n = 1/20 {
loc sidecap `" `sidecap' " $`=price[`n']'" "'
}
sysuse auto, clear
twoway (scatter mpg rep78) , ///
note("{bf: MAKE }" ///
"-----------------" `sidetable' ///
, size(medsmall) color(green) ///
position(2) margin(small) ///
justification(left)) ///
caption(" {bf: Price }" ///
"---------" `sidecap' ///
, size(medsmall) color(midgreen) ///
position(2) margin(vsmall) ///
justification(right))
exit
//Based statalist Jun 9, 2011
//Eric Booth
// Putting Data set results in a graph
clear all
sysuse auto
quiet: summarize mpg
twoway ///
scatter mpg rep78, ///
text(40 3 "The mean mpg is:`=string(`=r(mean)',"%8.2f")'", placement(right)) ///
text(38 3 "The sd mpg is: `=string(`=r(sd)',"%8.2f")'" , placement(right))
exit
// Adding a picture as background to a graph
//your picture is converted by: http://www.text-image.com/convert/ascii.html
//set width to 500
set more off
infix str1 a1-a500 1-500 using "car ascii to text big.txt", clear
//hexdump may be needed to look for unexpected characters
//hexdump "c:/car ascii to text big.txt", tabulate
//scaling y axis picture data and reversing
generate y=90+((_N-_n+1)*1)
reshape long a, i(y) j(col)
//replacing characters with numbers
replace a="1" if a=="+"
replace a="2" if a=="-"
replace a="3" if a=="."
replace a="4" if a=="/"
replace a="5" if a==":"
replace a="6" if a=="-"
replace a="7" if a=="M"
replace a="8" if a=="N"
replace a="9" if a=="`"
replace a="10" if a=="d"
replace a="11" if a=="h"
replace a="12" if a=="m"
replace a="13" if a=="o"
replace a="14" if a=="s"
replace a="15" if a=="y"
//scaling x axis picture data
replace col=col/10
merge 1:1 _n using "C:\Program Files\stata11\ado\base/a/auto.dta"
local size="tiny"
twoway ///
(scatter y col if a=="1" ,msymbol(circle) ///
mcolor("0 0 0*.20") msize(`size') ) ///
(scatter y col if a=="2" ,msymbol(circle) ///
mcolor("255 255 255*.2") msize(`size')) ///
(scatter y col if a=="3" ,msymbol(circle) ///
mcolor("255 0 0*.2") msize(`size') ) ///
(scatter y col if a=="4" ,msymbol(circle) ///
mcolor("0 0 255*.2") msize(`size') ) ///
(scatter y col if a=="5" ,msymbol(circle) ///
mcolor("0 0 255*.2") msize(`size') ) ///
(scatter y col if a=="6" ,msymbol(circle) ///
mcolor("255 255 0*.2") msize(`size') ) ///
(scatter y col if a=="7" ,msymbol(circle) ///
mcolor("0 255 255*.2") msize(`size') ) ///
(scatter y col if a=="8" ,msymbol(circle) ///
mcolor("255 0 255*.2") msize(`size') ) ///
(scatter y col if a=="9" ,msymbol(circle) ///
mcolor(white) msize(`size') ) ///
(scatter y col if a=="10",msymbol(circle) ///
mcolor(orange*.2) msize(`size') ) ///
(scatter y col if a=="11",msymbol(circle) ///
mcolor(brown*.2) msize(`size') ) ///
(scatter y col if a=="12",msymbol(circle) ///
mcolor(sand*.2) msize(`size') ) ///
(scatter y col if a=="13",msymbol(circle) ///
mcolor(black*.2) msize(`size') ) ///
(scatter y col if a=="14",msymbol(circle) ///
mcolor(black*.2) msize(`size') ) ///
(scatter y col if a=="15",msymbol(circle) ///
mcolor(black*.2) msize(`size') ) ///
(scatter length mpg) ///
(lfit length mpg) ///
,yscale( range(100 250)) ///
ytitle(Length ins.) xtitle(mpg) ///
title(Mpg v Length) legend(off)
exit
// Adding a picture as background to a graph
//The above picture can be improved on, if required, by converting a picture
//into html and then cleaning this file up in Stata.
//The picture is first loaded into GIMP (free software program) and then saved
//as html (Remmember the larger the picture the more computer resources are
//required and the longer it will take to process the code)
clear all
set more off
set memory 900m
program hex
syntax varname , Newv(name)
generate `newv'=((strpos("`=c(alpha)'",`varlist')-1)/2)+10
replace `newv'=real(`varlist') if `newv'==9.5
end
//values that can be changed
local divide=8 //sample size
insheet using "c:/fast car.html", clear
generate row=.
replace row=1 if strpos(v1,"< TR>")
replace row=sum(row)
keep if strpos(v1, " < TD BGCOLOR")
split v1,p(#)
split v12,p(>)
keep row v121
generate obs=_n
bysort row (obs):gen col=_n
rename v121 hex
generate hex1=substr(hex,1,1)
generate hex1a=substr(hex,2,1)
generate hex2=substr(hex,3,1)
generate hex2a=substr(hex,4,1)
generate hex3=substr(hex,5,1)
generate hex3a=substr(hex,6,1)
hex hex1, newv(dec1)
hex hex1a, newv(dec1a)
hex hex2, newv(dec2)
hex hex2a, newv(dec2a)
hex hex3, newv(dec3)
hex hex3a, newv(dec3a)
generate dec1b=16*dec1+dec1a
generate dec2b=16*dec2+dec2a
generate dec3b=16*dec3+dec3a
keep dec1b dec2b dec3b row col
generate dec1c=floor(dec1b/`divide')*`divide'
generate dec2c=floor(dec2b/`divide')*`divide'
generate dec3c=floor(dec3b/`divide')*`divide'
gen str20 color=char(34)+string(dec1c,"%3.0f")+" "+string(dec2c,"%3.0f") ///
+" "+string(dec3c,"%3.0f")+char(34)
egen group_col=group(color)
compress
save pict_graph, replace
use pict_graph, clear
quiet summarize col
local size="*`=33/`r(max)''" //marker size, can be adjusted
levelsof color, local(a)
merge 1:1 _n using "C:\Program Files\stata11\ado\base/a/auto.dta"
summarize length
gen y=90+(((_N-_n+1)-90))/(_N/r(max))
//scaling x axis picture data
replace col=col/9
local z=1
foreach i of local a {
if `z'==1 {
local zz = ///
`"(scatter y col if group_col==`z',msymbol(square) mcolor(`i') msize(`size'))"'
local z=2
}
else {
local zz1 = ///
`"(scatter y col if group_col==`z' ,msymbol(square) mcolor(`i') msize(`size'))"'
local zz "`zz' `zz1'"
local ++z
}
}
twoway `zz' ///
(scatter length mpg, mcolor(red) msize(large)) ///
(lfit length mpg) ///
,yscale( range(100 325)) ///
ytitle(Length ins.) xtitle(mpg) ///
title(Mpg v Length) legend(off)
exit
// Odds ratio graph
** requires 2 user written graphs: parmest and egenmore
** if not already installed:
ssc install parmest
ssc install egenmore
sysuse nlsw88, clear
logit union married never_married collgrad south
parmest, norestore eform
egen axis= axis(estimate), label(parm)
twoway bar estimate axis, base(1) horizontal barw(.5) ///
xline(1) ylab(, valuelabel angle(0)) || ///
rcap min95 max95 axis, horizontal ///
legend(order(1 "point estimate" ///
2 "95% conf. int.") pos(6)) ///
xtitle("odds ratio") ytitle("")
*(Maarten L. Buis - Statalist, 14/09/2010)
// Odds ratio graph
** requires 2 user written graphs: parmest and egenmore
** if not already installed:
ssc install parmest
ssc install egenmore
sysuse nlsw88, clear
logit union married never_married collgrad south
parmest, norestore eform
egen axis= axis(estimate), label(parm)
twoway scatter axis estimate, ///
xline(1) ylab(, valuelabel angle(0)) || ///
rcap min95 max95 axis, horizontal ///
legend(order(1 "point estimate" ///
2 "95% conf. int.") pos(6)) ///
xtitle("odds ratio") ytitle("")
//Paired Plot
sysuse auto, clear
gsort -mpg
keep in 1/25
replace mpg = round(mpg)
gen rank1 = _n
egen rank2 = rank(-price)
gen byte one = 1
gen byte two = 2
gen left = 0.4
twoway ///
(pcspike rank1 one rank2 two, ///
xlabel(none) xscale(noline range(0.3 2.3)) xtitle("") ///
yscale(r(-1 .) reverse off) ylabel(, nogrid)) ///
(scatter rank1 one, mlabel(mpg) mlabpos(9) ms(none)) ///
(scatter rank2 two, mlabel(price) mlabpos(3) ms(none)) ///
( scatter rank1 left, mla(make) mlabpos(3) ms(none) ///
text(-0.5 1 "mpg, US gallons") ///
text(-0.5 2 "price , {c S|}US in 1978") ///
legend(off) graphregion(color(white)))
//Paired Plot 1
//Looking at the effect of treatment on the weight of anorexic patients
//those under the line have lost weight.
//Making up the data; based on the data found at:
//http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt
clear
set obs 76
gen treatment=mod(_n,3)+1
generate w_before1=rnormal(82, 4.8)
replace w_before1=rnormal(81, 5.7) if treatment==2
replace w_before1=rnormal(83, 5) if treatment==3
generate w_after1=rnormal(85.6, 8.3)
replace w_after1=rnormal(81.1, 4.7) if treatment==2
replace w_after1=rnormal(90, 5.4) if treatment==3
//the end of making up the data
label var w_before " Weight before treatment, lb"
label var w_after " Weight after treatment, lb"
label define treat ///
1 "Cognitive behavioural" ///
2 "Control" ///
3 "Family therapy"
label values treatment treat
scatter w_after w_before w_before, ///
msymbol(Oh i) c(. l) lc(none gs12) ///
sort(w_before) yla(, ang(h)) ///
ytitle(" Weight after treatment, lb") ///
by(treatment, row(1) note("weight, lb") legend(off))
//From Stata Journal Article Vol 9 No 4
//Speaking Stata: Paired, parallel, or profile plots
//for changes, correlations, and other comparisons
//Nicholas J. Cox
//Contact us to subscribe to the Stata Journal
//Paired Plot 2
//Looking at the effect of treatment on the weight of anorexic patients.
//Making up the data; based on the data found at:
//http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt
clear
set obs 76
gen treatment=mod(_n,3)+1
gen w_before1=rnormal(82, 4.8)
replace w_before1=rnormal(81, 5.7) if treatment==2
replace w_before1=rnormal(83, 5) if treatment==3
gen w_after1=rnormal(85.6, 8.3)
replace w_after1=rnormal(81.1, 4.7) if treatment==2
replace w_after1=rnormal(90, 5.4) if treatment==3
//end of making up the data
label var w_before " Weight before treatment, lb"
label var w_after " Weight after treatment, lb"
label define treat ///
1 "Cognitive behavioural" ///
2 "Control" ///
3 "Family therapy"
label values treatment treat
gen byte one = 1
gen byte two = 2
twoway ///
pcspike w_before one w_after two if w_before <= w_after, ///
lcolor(gs12) || ///
pcspike w_before one w_after two if w_before > w_after, ///
lw(*1.2) lcolor(gs2) xla(1 " before" 2 "after ") ///
xtitle("") yla(, nogrid ang(h)) ytitle("weight, lb") ///
by(treatment, row(1) note("") legend(off) noixtick)
//From Stata Journal Article Vol 9 No 4
//Speaking Stata: Paired, parallel, or profile plots
//for changes, correlations, and other comparisons
//Nicholas J. Cox
//Contact us to subscribe to the Stata Journal.
//Paired Plot 3
//Looking at the effect of treatment on the weight of anorexic patients.
//Making up the data; based on the data found at:
//http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt
clear
set obs 76
gen treatment=mod(_n,3)+1
gen w_before1=rnormal(82, 4.8)
replace w_before1=rnormal(81, 5.7) if treatment==2
replace w_before1=rnormal(83, 5) if treatment==3
gen w_after1=rnormal(85.6, 8.3)
replace w_after1=rnormal(81.1, 4.7) if treatment==2
replace w_after1=rnormal(90, 5.4) if treatment==3
//end of making up the data
label var w_before " Weight before treatment, lb"
label var w_after " Weight after treatment, lb"
label define treat ///
1 "Cognitive behavioural" ///
2 "Control" ///
3 "Family therapy"
label values treatment treat
bysort treatment (w_before w_after) : gen order1 = _n - _N/2
twoway ///
pcarrow w_before order1 w_after order1, pstyle(p1) ///
|| scatter w_before order1, pstyle(p1) ms(o) ///
xla(none) xtitle("") yla(, ang(h)) ytitle("weight, lb") ///
by(treatment, row(1) note("") legend(off))
//From Stata Journal Article Vol 9 No 4
//Speaking Stata: Paired, parallel, or profile plots
//for changes, correlations, and other comparisons
//Nicholas J. Cox
//Contact us to subscribe to the Stata Journal.
//Parallel Coordinate Plot
//Rescaling variables and plotting
sysuse auto, clear
drop make
set more off
local z=1
foreach i of varlist _all {
summarize `i', detail
#delimit ;
local a `"`a' text( 1.2 `z' `=char(34)'`i'`=char(34)'
`=char(34)'`=string(`r(max)',"%8.2f")'`=char(34)' ,
orientation(vertical) placement(n) size(3) justification(left) )
text( -0.1 `z' `=char(34)' `i'`=char(34)'
`=char(34)'`=string(`r(min)',"%8.2f")'`=char(34)' ,
orientation(vertical) placement(south) size(3) justification(right) ) "';
#delimit cr
//scaling variables
generate x`z'=(`i' -r(min))/(r(max)-r(min))
local ++z
}
drop if rep78==.
generate id=_n
reshape long x, i(id) j(type)
drop if x>100
twoway ///
(line x type if for==1, lcolor(red) c(L)) ///
(line x type if for==0, lcolor(blue) c(L)), ///
xline(1/12) ///
`a' yscale( off range(0 1.1)) xscale(off) ///
text(0 0 "MIN") text(1 0 "Max") ///
graphregion(margin(t=20 b=20)) legend(off)
//Using Arrows
**The example below uses data from: http://www.bom.gov.au/climate/data/
cd "C:\"
insheet using "temperature data.csv", clear //or whatever data you have
set more off
keep if inlist(year,1940, 1950, 1960, 1970,2010)
collapse (mean) max, by(month year)
sort year month
generate date1=_n
tsset date1 //set to time series so lag "l." and
// forward "f." operators can be used
// see help tsvarlist
twoway pcarrow max l.max f.max max, mlab(date1) ///
ytitle("This month Deg. C") ///
xtitle("Previous month Deg. C") ///
title("Temperature Cycle Melbourne - Deg. C") ///
subtitle("Years 1940 1950 1960 1970 2010")
//Also see: Stata Journal Article Vol 5 No 2
//The arrows of outrageous fortune by Nicholas J. Cox
//http://www.stata-journal.com/sjpdf.html?articlenum=gr0015
//Contact us to subscribe to the Stata Journal.
//tsline with confidence intervals - using margins
Requires: parmest
to download this program type the following on the Stata command
line (if not already loaded):
ssc install parmest
Requires: tsmktim
to download this program type the following on the Stata command
line (if not already loaded):
ssc install tsmktim
To run the example: copy the following into a do file and run
webuse grunfeld, clear
qui reg invest i.year i.company
margins i.year, post
// Roger Newson's parmest from SSC
parmest, saving(yeareff, replace)
use yeareff, clear
// Kit Baum's tsmktim from SSC
tsmktim year, start(1935)
tsline estimate || tsrline min95 max95, legend(col(1)) scheme(s2mono)
From Statalist:
22/11/2010 5:47 AM
Kit Baum
//Predictive margins with 95% confidence interval - using margins
Requires: parmest
to download this program type the following on the Stata command
line (if not already loaded):
ssc install parmest
sysuse auto, clear
gen kprice=price/1000
regress mpg c.kprice##c.kprice i.foreign i.rep78 turn trunk headroom
margins, at(kprice=(3/15)) vsquish post
return list
ereturn list
parmest, norestore //saves parameters
l
gen kprice=3 + _n - 1
l
label var kprice "Price(\$1000)"
twoway ///
(rarea max95 min95 kprice, pstyle(ci)) ///
(line estimate kprice), ///
ytitle(Miles per gallon) ///
title(Predictive margins with 95% confidence interval) ///
xlabel(3(3)15 10.4) legend(off)
From Stata News Vol 25 No 3
In Stata 12 see marginsplot
//Generating predictions and graphing them
sysuse auto, clear
// estimate the model
reg price c.mpg##c.weight##c.weight i.foreign
// collect some information about mpg
sum mpg
local m = r(mean)
local sd = r(sd)
// we are going to change the data, so we preserve it first
preserve
// fix the other explanatory variables
// In this case we only look at US ("domestic") cars
replace foreign = 0
// predict the price while fixing mpg at different values
replace mpg = `m'
predict yhatm
replace mpg = `m' - `sd'
predict yhatl
replace mpg = `m' - 2*`sd'
predict yhatll
replace mpg = `m' + `sd'
predict yhath
replace mpg = `m' + 2*`sd'
predict yhathh
// nicer display of large numbers 1,000 instead of 1000
format yhat* %8.0gc
// create graph
sort weight
twoway line yhatll yhatl yhatm yhath yhathh weight, ///
title("predicted price for US cars") ///
ytitle("predicted price (US {c S|})") ///
lpattern(solid solid solid solid solid) ///
lcolor("255 255 204" ///
"161 218 180" ///
" 65 182 196" ///
" 44 127 184" ///
" 37 52 148" ) ///
legend( order( - "mpg" ///
1 "mean-2*sd" ///
2 "mean-1*sd" ///
3 "mean" ///
4 "mean+1*sd" ///
5 "mean+2*sd" ))
// get our original data back
restore
martin buis
Wed, 23 Feb 2011 10:57:36
Statalist
//Labelling in terms of another variable
Requires: labmask
to download this program type the following on the Stata command
line (if not already loaded):
ssc install labmask
sysuse auto, clear
sort price
gen order = _n
labmask order, values(make)
twoway bar price order, horizontal ylabel(8 44 71, valve angle(h)) ytitle("")
M Cox
Statalist 21/12/10
//Changing the order of twoway graphs where the by() option is used
Requires: egenmore
to download this program type the following on the Stata command
line (if not already loaded):
ssc install egenmore
//the following is an example of order that the graphs are in when
//determined by Stata
// ie. lowest rep78 value to highest
scatter mpg weight , by(rep78) name(a2, replace)
//If you require the order to be the mean mpg of the levels of rep78,
//the following can be done:
egen mean1 = mean(mpg) , by(rep78)
egen axis = axis1(mean1 rep78), label(rep78) reverse // egenmore command
scatter mpg weight , by(axis) name(a1, replace)
M Cox
Statalist 21/03/11
//Adding titles to the left hand side of the graph
sysuse auto, clear
fillin rep78 foreign
twoway scatter price mpg, by(foreign rep78, cols(5) compact)
egen group = group(foreign rep78)
label define group 1 "Poor" ///
2 "Fair" ///
3 "Average" ///
4 "Good" ///
5 "Excellent" ///
6" " ///
7" " ///
8" " ///
9" " ///
10 " "
label value group group ///
twoway scatter price mpg, ///
by(group, cols(5) ///
r1title("Car type", ///
orientation(rvertical) ///
size(medsmall)) ///
t1title("Repair status", ///
size(medsmall)) ///
note("") compact)
gr_edit plotregion1.r1title[5].style.editstyle drawbox(yes) editcopy
gr_edit plotregion1.r1title[5].as_textbox.setstyle, style(yes)
gr_edit plotregion1.r1title[10].as_textbox.setstyle, style(yes)
gr_edit plotregion1.r1title[5].text = {}
gr_edit plotregion1.r1title[5].text.Arrpush Domestic
gr_edit plotregion1.r1title[10].text = {}
gr_edit plotregion1.r1title[10].text.Arrpush Foreign
gr_edit plotregion1.r1title[5]._set_orientation rvertical
For instructions on how to do this in the Stata graphics editor
see Stata Journal Vol 9 No 4
For information on how to subscribe to the Stata Journal
please contact us.
//Combining many graphs on a page
sysuse auto, clear
graph drop _all
foreach i of varlist _all {
capture confirm numeric variable `i'
if _rc==0 {
histogram `i', name(`i')
local z "`z' `i'"
}
}
graph combine `z'
//Producing a similar plot to the density plot at:
http://www.survey-design.com.au/Usergraphs.html
using Stata 12's new contour plot
Requires: spgrid, spkde & mylabels
to download these programs type the following
on the Stata command line (if not already loaded):
ssc install spgrid
and then
ssc install spkde
and then
ssc install mylabels
sysuse "auto.dta", clear
set more off
summarize price mpg
clonevar x = mpg
clonevar y = price
replace x = (x-0) / (50-0)
replace y = (y-0) / (20000-0)
mylabels 0(10)50, myscale((@-0) / (50-0)) local(XLAB)
mylabels 0(5000)20000, myscale((@-0) / (20000-0)) local(YLAB)
keep x y
save "xy.dta", replace
* 2. Generate a 100x100 grid
spgrid, shape(hexagonal) xdim(100) ///
xrange(0 1) yrange(0 1) ///
dots replace ///
cells("2D-GridCells.dta") ///
points("2D-GridPoints.dta")
* 3. Estimate the bivariate probability density function
spkde using "2D-GridPoints.dta", ///
xcoord(x) ycoord(y) ///
bandwidth(fbw) fbw(0.1) dots ///
saving("2D-Kde.dta", replace)
use "2D-Kde.dta", clear
merge 1:1 _n using xy.dta
twoway (contour p spgrid_ycoord spgrid_xcoord if p!=0 , ///
levels(15)) ///
(scatter y x, mcolor(black) msize(small) ) ///
,xlab(`XLAB', nogrid) xtitle("Mileage (mpg)") ///
ylab(`YLAB', nogrid) ///
ytitle("Price $US") plotregion(color(blue))
Survivual Analysis Graphs
//Generating Survival curves from two data sets
use http://www.stata-press.com/data/r11/drug2.dta, clear
stset
sts , name(a1, replace)
sts generate newvar =s
set obs `=_N+1'
replace newvar=1 in l
replace _t=0 in l
replace died=0 in l
replace s=1 in l
keep newvar _t
save c:/s1, replace
use http://www.stata-press.com/data/r11/stan3.dta, clear
replace t1=t1/40
stset t1, id(id) failure(died)
sts , name(a1a, replace)
sts generate newvar1 =s
set obs `=_N+1'
replace newvar1=1 in l
replace _t=0 in l
keep newvar1 _t
rename _t _t1
merge 1:1 _n using c:/s1
twoway (line newvar1 _t1, sort c(stairstep)) ///
(line newvar _t, sort c(stairstep)), ///
ylabel(0(.2)1) name(a2, replace) ytitle("") ///
legend(label(1 data set 1) label(2 data set 2))
//Generating Survival curve with confidence interval
use http://www.stata-press.com/data/r11/drug2.dta, clear
stset
sts gen km=s
sts gen ub=ub(s)
sts gen lb=lb(s)
//Then something along the lines:
twoway (rarea ub lb _t, sort) (line km _t, sort)
From Statalist:
Fri, 25 Feb 2011
Phil Clayton