Stata | Graphics

Click the + Expand  button below each graph to explore the corresponding code/script for each.

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))

// Graph - Special Text

twoway                                                                        ///
(scatteri 6.5 0 "Other Symbols Include ",         mlabsize(small) pstyle(p1)) ///
(scatteri 6.5 0.5 "{&trade} {©right} {&loz}", mlabsize(huge) pstyle(p1))  ///
(scatteri 5.5 0 "Bold font",                      mlabsize(small) pstyle(p1)) ///
(scatteri 5.5 0.5 "{bf: Bold font}",              mlabsize(huge) pstyle(p1))  ///
(scatteri 4.5 0 "{it: Italics}",                  mlabsize(small) pstyle(p1)) ///
(scatteri 4.5 0.5 "{it: Italics}",                mlabsize(huge)  pstyle(p1)) ///
(scatteri 3.5 0 "Superscript",                    mlabsize(small) pstyle(p1)) ///
(scatteri 3.5 0.5 "{superscript: Superscript}",   mlabsize(huge) pstyle(p1))  ///
(scatteri 2.5 0 "Subscript",                      mlabsize(small) pstyle(p1)) ///
(scatteri 2.5 0.5 "{subscript: Subscript}",       mlabsize(huge) pstyle(p1))  ///
(scatteri 1.5 0 "Default Symbol font   ",         mlabsize(small) pstyle(p1)) ///
(scatteri 1.5 0.5 " {stSymbol:  adcv}  ",         mlabsize(huge) pstyle(p1))  ///
(scatteri 0.5 0 "Greek Symbols:  ",               mlabsize(small) pstyle(p1)) ///
(scatteri 0.5 0.5 "{&Alpha} {&Pi} {&Sigma}",      mlabsize(huge) pstyle(p1)), ///
title("Special Font that can be used on Stata Graphs", color(red))            ///
xlabel(-.05 1) ylabel(-1 7)                                                   ///
xscale(off) yscale(off)                                                       ///
legend(off)                                                                   ///
note("Stata special font."                                                    ///
"There are many other options see {bf:help graph text}" ,                     ///
ring(0))
//make up some data
set seed 1
clear
set obs 50
generate a=runiform()*1000
generate b=runiform()*10
generate s=runiform()<.5

label define sexlab 1 `"{fontface "Female and male symbols":M }"' 0 ///
`"{fontface "Female and male symbols":F }"'
label values s sexlab

scatter a b , ms(i)  mlab(s) mlabpos(c) ///
mlabsize(*2) title( "Example" `"{fontface "Female and male symbols":M F}"')


//Using imported font in a graph

//Installation of new fonts may vary between operating
systems. These instructions are for Windows XP

//A font can be obtained from: (http://www.fonts4free.net/.html)

//The font we are looking at today is from: (http://www.fonts4free.net/female-and-male-sym-font.html).

//Copy the font file to the disk file (temp directory suggested). Extract file from zip file to a suitable directory //Go to the Control panel and click on font. The File>install new Font .. //The new font "Female and male symbols" has now been installed //To see a graph with the new font

sysuse auto, clear

graph bar  mpg, asyvars  over(rep78) ///
bar(1, bcolor(red*0.4))              ///
bar(2, bcolor(red*0.2))              ///
bar(3, bcolor(blue*0.2))             ///
bar(4, bcolor(blue*0.4))             ///
bar(5, bcolor(blue*0.6))             ///
legend(row(1) title("REP78"))        ///
title("Mean MPG for rep78")
sysuse auto, clear

twoway  ///
(scatter weight mpg if rep78==1)            ///
(scatter weight mpg if rep78==2)            ///
(scatter weight mpg if rep78==3)            ///
(scatter weight mpg if rep78==4)            ///
(scatter weight mpg if rep78==5) , name(a1) ///
legend(label(1 "weight, rep78 == 1"))       ///
legend(label(2 "weight, rep78==2"))         ///
legend(label(3 "weight, rep78==3"))         ///
legend(label(4 "weight, rep78==4"))         ///
legend(label(5 "weight, rep78==5"))

// 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)  ) , ///

// 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)


//Alternatively

sysuse auto, clear

regress mpg weight

set obs `=_N+2'
replace weight=0 in l
replace weight=8000 in -2

predict a

twoway (scatter mpg weight in 1/-2)  ///
(line a 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

// Circle using function

twoway (function y = sqrt(1 - (x)^2),           ///
range(-1 1) lwidth(thick) lcolor(red))          ///
function y = -sqrt(1 - (x)^2),                  ///
range(-1 1) lwidth(thick)lcolor(red)), aspect(1)

// normalden using function
clear
set obs 100

generate x1 = rnormal(0,1)
generate x2 = rnormal(0.5,1.3)

summarize x1
local M1 = r(mean)
local SD1 = r(sd)

summarize x2
local M2 = r(mean)
local SD2 = r(sd)

twoway function x1 = normalden(x, `M1', `SD1') , range(-4 4) || ///
function x2 = normalden(x, `M2', `SD2'), range(-4 5)

// histogram

sysuse auto, clear

twoway histogram mpg if !foreign,                              ///
start(10) width(2) freq bfcolor(none) blcolor(pink)            ///
|| histogram mpg if foreign, freq start(10) width(2) barw(1.8) ///
bfcolor(none) blcolor(blue)                                    ///
legend(order(1 "Domestic" 2 "Foreign") pos(2) ring(0) col(1))

// Displaying part of a histogram

sysuse auto, clear

twoway__histogram_gen mpg , gen(h y, replace) freq bin(10)

twoway ///
(histogram y [fw=h] if inrange(y,19,35),discrete color(green)lcolor(red)freq) ///
(histogram y [fw=h],color(none) lcolor(red) discrete freq name(a1, replace))

twoway ///
(histogram y [fw=h] if inrange(y,19,35),discrete color(green) lcolor(red)freq ///
name(a2, replace))

graph combine a1 a2 ,xcommon cols(1)

// 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.

//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)

// distribution plot

clear
sysuse auto
version 9.2
keep if foreign
sort weight

gen weight2 = weight^2
regress mpg weight weight2
predict fit
predict se , stdp

#delimit ;
twoway sc mpg weight , pstyle(p3) ms(o) ||
fn weight[3]  - 1000 * normden(x, `=fit[3]' , `=se[3]') ,
range(`=fit[3] -5' `=fit[3] +5') horiz pstyle(p1) ||
fn `=fit[3]' , range(`=weight[3]' `=weight[3]-1000*normden(0, se[3])')
pstyle(p1) ||
fn weight[17] - 1000 * normden(x, `=fit[17]', `=se[17]') ,
range(`=fit[17]-5' `=fit[17]+5') horiz pstyle(p1) ||
fn `=fit[17]', range(`=weight[17]' `=weight[17]-1000*normden(0, se[17])')
pstyle(p1) ||
fn weight[21] - 1000 * normden(x, `=fit[21]' , `=se[21]') ,
range(`=fit[21] -7' `=fit[21] +7') horiz pstyle(p1) ||
fn `=fit[21]', range(`=weight[21]' `=weight[21]-1000*normden(0, se[21])')
pstyle(p1) ||
line fit weight
,clwidth(*2) legend(off) ytitle(Miles per gallon) xtitle(Weight)
title("Scatter with Regression Line and Confidence Interval Densities"
,size(4.8) margin(t=0 b=1.5) span)
caption("graph46", box size(vsmall)position(5) ring(10))
;
#delimit cr

// 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)

//2nd Row of axis labels

webuse turksales, clear
line sales t,                              ///
xtick(119.5(4)159.5, tlength(*1.5))    ///
xmlabel(120/159,                       ///
noticks format(%tqq))              ///
xlabel(121.5(4)157.5,                  ///
noticks format(%tqCY) labgap(2.5)) ///
xtitle("")

*(Maarten Buis - Stata list,  Thu, 7 Nov 2013)

// 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)

// bar graph - categorical variables - proportions

sysuse auto, clear
//creating some data
gen mpg1=mpg<30
recode mpg (1/25=1) (26/30=2) (31/50=3), gen(mpg2)
save kk, replace
list

capture erase kk1.dta
local z=1

foreach i of varlist for mpg1 mpg2 {
	use kk, clear
	keep `i'
	contract `i'
	egen total=total(_freq)
	replace _freq=_freq/total
	generate type=`z'
	capture append using kk1
	save kk1, replace
	local ++z
}

drop mp* for
bysort type : gen t1=_n

label define kk 1 cat1 2 cat2 3 cat3
label value type kk

graph bar (mean) _freq , over(t1,sort(_freq) ) over(type, ) showyvars ///
asyvar legend(off) nofill ytitle("Proportion") scheme(s2color)
// 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 average bar to group

sysuse auto, clear

summ price if for==0 & !missing(rep78)
local ave1=r(mean)

summ price if for==1 & !missing(rep78)
local ave2=r(mean)

set obs `=_N+3'

replace for=0 in -3
replace rep78=6 in -3

replace for=0 in -2
replace rep78=7 in -2
replace price=`ave1' in -2

replace for=1 in l
replace rep78=7 in l
replace price=`ave2' in l

label define kk 1 1 2 2 3 3 4 4 5 5 6 "-"  7 "AVE"
label value rep78 kk

graph bar (mean) price , over(rep78 ) over(for) showyvars ///
asyvar legend(off)  scheme(color) bar(7, color(black))

// Different color bars

clear

input ///
id w pos mark
1  1 1  69.55
2  1 2  65.16
3  1 3  64.91
4  1 4  64.53
5  1 5  63.70
6  0 6  84.58
7  0 7  84.51
8  0 8  84.12
9  0 9  83.34
10 0 10 82.8
end

label define kk        ///
1  "Barbora - CZE "    ///
2  "Christina - GER "  ///
3  "Linda - GER "      ///
4  "Sunette - RSA"     ///
5  "Huihui - CHN"      ///
6  "Keshorn - TRI "    ///
7  "Oleksandr - UKR "  ///
8  "Antti - FIN "      ///
9  "Vitezslav - CZE"   ///
10 "Tero - FIN"

label value pos kk

label define w 1 "Women" 0 "Men"
label value w w

graph bar (asis) mark,  over(id ) over(pos,label(angle(45))) over(w) ///
ylabel( 0(10)90, angle(45))                                         ///
blabel(bar, position(inside) format(%9.1f) color(black))            ///
scheme(s2color)  legend(off)                                        ///
bargap(5) title("London Olympics 2012" "Javelin")                   ///
nofill ytitle("Metres")                                             ///
bar(1, color(gold))      ///
bar(6, color(gold))      ///
bar(2, color(gs13))      ///
bar(7, color(gs13))      ///
bar(3, color(sienna))    ///
bar(8, color(sienna))    ///
bar(4, color(cyan))      ///
bar(9, color(cyan))      ///
bar(5, color(pink))      ///
bar(10, color(pink))


exit

// Different color bars for the over option
clear

input ///
id w pos mark
1  1 1  69.55
2  1 2  65.16
3  1 3  64.91
4  1 4  64.53
5  1 5  63.70
6  0 6  84.58
7  0 7  84.51
8  0 8  84.12
9  0 9  83.34
10 0 10 82.8
end

label define w 1 "Women" 0 "Men"
label value w w

gen g1=mark if _n<6
gen g2=mark if _n>5

list

graph bar (asis) g1 g2, ascat over(mark,sort(mark) descending ///
relabel(  ///
5  "Barbora - CZE "    ///
4  "Christina - GER "  ///
3  "Linda - GER "      ///
2  "Sunette - RSA"     ///
1  "Huihui - CHN"      ///
10 "Keshorn - TRI "    ///
9  "Oleksandr - UKR "  ///
8  "Antti - FIN "      ///
7  "Vitezslav - CZE"   ///
6  "Tero - FIN"        ///
) label( angle(45)))   over(w) ylabel(0(10)90)                                 ///
ytitle("Metres") blabel(bar, position(inside) format(%9.1f) color(white))      ///
scheme(s2color)  legend(off) bargap(5) title("London Olympics 2012" "Javelin") ///
nofill  bar(1, color(dknavy)) bar(2, color(red))

exit

// Different color bars for the over option - Using twoway graph
clear all

input ///
id w pos mark
1  0 6  84.58
2  0 7  84.51
3  0 8  84.12
4  0 9  83.34
5  0 10 82.8
6  1 1  69.55
7  1 2  65.16
8  1 3  64.91
9  1 4  64.53
10 1 5  63.70
end

label define w 1 "Women" 0 "Men"
label value w w

label define kk        ///
1  "Keshorn - TRI "    ///
2  "Oleksandr - UKR "  ///
3  "Antti - FIN "      ///
4  "Vitezslav - CZE"   ///
5  "Tero - FIN"        ///
6  " "                 ///
7  "Barbora - CZE "    ///
8  "Christina - GER "  ///
9  "Linda - GER "      ///
10  "Sunette - RSA"    ///
11 "Huihui - CHN"

label value id  kk

replace id=id+1 if id>5

set obs `=_N+1'
replace id=6 in l

twoway (bar mark id if id==1, base(0) ylabel(0(10)90) ///
xtitle("Men                                  Women")  ///
xlabel(1(1)11,valuelabel angle(45))                   ///
barwidth(.4)   color(cyan))                           ///
(bar mark id if id==2,  barwidth(.4)   color(cyan))   ///
(bar mark id if id==3,  barwidth(.4)   color(cyan))   ///
(bar mark id if id==4,  barwidth(.4)   color(cyan))   ///
(bar mark id if id==5,  barwidth(.4)   color(cyan))   ///
(bar mark id if id==7,  barwidth(.4)   color(red))    ///
(bar mark id if id==8,  barwidth(.4)   color(red))    ///
(bar mark id if id==9,  barwidth(.4)   color(red))    ///
(bar mark id if id==10, barwidth(.4)   color(red))    ///
(bar mark id if id==11, barwidth(.4)   color(red))    ///
(scatter mark id,  mlabel(mark) msymbol(none)         ///
mlabposition(12)), legend(off)                        ///
title("London Olympics 2012" "Javelin")               ///
plotregion(margin(l=5 b=0))

exit

// 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)

// Adding frequency count bar labels in bold font

// Adding this using the Stata editor commands. This is produced
// using a program.

program g2
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="N= "+string(r(N),"%8.0f")

	gr_edit plotregion1.barlabels[`x'].text = {}
	gr_edit plotregion1.barlabels[`x']._set_orientation vertical
	gr_edit plotregion1.barlabels[`x'].text.Arrpush `"{bf:  `a1'   }"'
	local ++x
}

end

sysuse auto, clear
egen pr2=cut(price), group(10)

g2 weight , over(pr2)

// 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)

// Detail and Summary box plots together

sysuse auto, clear
generate order = _n
expand 3
bysort order : generate which = _n
drop if which == 1 & price > 5000
drop if which == 2 & price > 10000
label def which 1 "<= $5000" 2 "<= $10000" 3 "all"
label val which which
graph box mpg, over(which) over(foreign)

*(Nick Cox - Stata list,  December 06, 2013)
sysuse nlsw88, clear

clonevar wagelog10=wage
replace wagelog10=log10(wagelog10)

mylabels 0(10)40 , myscale(log10(@)) local(labels)

graph hbox wagelog10, over(ind, sort(1)) nooutside ytitle("") ylabel(`labels') ///
title("Hourly wage, 1988, woman aged 34-46", span) subtitle(" ") ///
note("Source:1988 data from NLS, U.S. Dept. of Labor, "          ///
"Bureau of Labor Statistics",span)
exit
replace lexp = 35 if country == "Haiti"
egen median = median(lexp), by(region)
egen upq = pctile(lexp), p(75) by(region)
egen loq = pctile(lexp), p(25) by(region)
egen iqr = iqr(lexp), by(region)
egen upper = max(min(lexp, upq + 1.5 * iqr)), by(region)
egen lower = min(max(lexp, loq - 1.5 * iqr)), by(region)
twoway rbar med upq region, ///
pstyle(p1) blc(gs15) bfc(gs8) barw(0.35) || ///
rbar med loq region, pstyle(p1) blc(gs15) bfc(gs8) barw(0.35) || ///
rspike upq upper region, pstyle(p1) || ///
rspike loq lower region, pstyle(p1) || ///
rcap upper upper region, pstyle(p1) msize(*2) || ///
rcap lower lower region, pstyle(p1) msize(*2) || ///
scatter lexp region if !inrange(lexp, lower, upper) & lexp > 50, ///
ms(Oh)  mla(country) mlabcolor(gs8) xscale(off)  legend(off) ///
yla(, ang(h)) ytitle(Life expectancy (years)) xtitle("") ///
name(gr1,replace)

scatter lexp region if !inrange(lexp, lower, upper) & lexp < 50, ///
ylabel(35)   fysize(20) ytitle("") ms(Oh) mlabcolor(blue) ///
mcolor(blue)  mla(country)  xlabel(1 `" "Europe and" "Central Asia" "' ///
2 "North America" 3 "South America", noticks) ///
ytitle(Life expectancy (years), color(white))  yla(, ang(h))  ///
xtitle("")  name(gr2,replace)

graph combine gr1 gr2, col(1) xcommon imargin(zero)

// 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

// Using xtset to produce a panel data graph

//produce the panel dataset

clear
set more off

set obs 3
gen id=_n
expand 10
bysort id: gen year=2000+_n-1
gen data=runiform()*100
list, sepby(id)

// producing the graph

xtset id year

bysort year :egen ave=mean(data)
replace ave=. if id!=1

gen mean_lab="mean" if id==1 & year==2009

sort id year
xtline data, overlay addplot(                    ///
(line ave year,lwidth(thick))                    ///
(scatter data year if year==2009,mlab(id))       ///
(scatter ave year if year==2009,mlab(mean_lab)), ///
legend(off))

//alternative to xtset, producing the same graph

twoway (line data year,c(L))                     ///
(line ave year,lwidth(thick))                    ///
(scatter data year if year==2009,mlab(id))       ///
(scatter ave year if year==2009,mlab(mean_lab)), ///
legend(off) ytitle("data")

// 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

sysuse auto, clear

label define rep78 1 "Very low rating in 1978" ///
2 "Mediocre - Low Rating in 1978"              ///
3 "Medium Rating in 1978"                      ///
4 "Not bad rating in 1978, considering"        ///
5 "Excellent in 1978--the best of the year!"

label values rep78 rep78

splitvallabels rep78 if rep78>=3, length(11) recode

graph bar mpg if rep78>=3, over(rep78, relabel(`r(relabel)') ) scheme(s1)

exit

// Combining graphs and including a common legend
//this uses the user written command "grc1leg" which must be downloaded first

net install grc1leg,from( http://www.stata.com/users/vwiggins/)

clear	all

input quest str25 q      a1 a2 a3 a4 a5 a6
1 "Question 1"           0  2  37 45 12 4
1 "Benchmark Q1"         2  5  25 47 17 4
2 "Question 2"           1  37  2 40 17 3
2 "Benchmark Q2"         2  5  25 47  4 17
3 "Question 3"           1  2  40 37 17 3
3 "Benchmark Q3"         2  5  25 47 17 4
4 "Question 4"           1  2  37  17 3 40
4 "Benchmark Q4"         2  5  47 25 17 4
end

graph hbar a1-a6 if quest==1, percent over(quest, gap(1)) over(q, gap(10))   ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
saving(a1, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)

graph hbar a1-a6 if quest==2, percent over(quest, gap(1)) over(q, gap(10))   ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)lcolor(black))  ///
saving(a1a, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)

graph hbar a1-a6 if quest==3, percent over(quest, gap(1)) over(q, gap(10))    ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)  lcolor(black)) ///
saving(a1b, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)

graph hbar a1-a6 if quest==4, percent over(quest, gap(1)) over(q, gap(20))    ///
stack legend(span rows(1) label(1 "Missing") label(2 "Never")                 ///
label(3 "Rarely") label(4 "Occasionly  ") label(5 "Mostly")                   ///
label(6 "Always    ") size(small))                                            ///
yline(20 40 60 80 ,lwidth(0.25 ) lcolor(black)) saving(a2, replace)

grc1leg a1.gph a1a.gph a1b.gph a2.gph, cols(1) ///
imargin(0 0 0 0) ycommon xcommon legendfrom(a2.gph)

exit

*(Also see the user written program catplot on:
http://www.survey-design.com.au/Usergraphs.html)

// Table under x axis

clear all
sysuse auto

twoway ///
(scatter mpg rep78, yscale(range(0 50))) , ysize(2) name(a1)  xlabel( 1(1)5 )

contract rep78 for
fillin rep78 for
replace _freq=0 if missing(_freq)

set obs `=_N+2'

tostring _freq, replace
replace rep78=0 in `=_N-1'
replace rep78=0 in `=_N'

replace for=1 in `=_N-1'
replace for=0 in `=_N'

replace _freq="foreign cars  " in `=_N-1'
replace _freq="Domenstic cars" in `=_N'

twoway ///
(scatter    for rep78 if for==1, mlabel(_freq)  msymbol(none) mlabgap(-2)   ///
mlabposition(12))                                                           ///
(scatter for rep78 if for==0, mlabel(_freq) msymbol(none) mlabgap(-2)       ///
mlabposition(6))                                                            ///
, name(a2, replace) legend(off) ytitle("b")  fysize(5)  xscale( off)        ///
yscale( off fill)  graphregion(color(ltblue ))  plotregion(color(ltblue ))  ///
ylabel(,nogrid) xlabel( 1(1)5 )

graph combine a1 a2, cols(1) xcommon imargin(0 0 0 0)

exit

// Table on the side of a graph

sysuse auto, clear
twoway (scatter mpg rep78) , graphregion( margin(r+30) )

gr_edit AddTextBox added_text editor `=82+8' `=101'
gr_edit added_text_new = 1
gr_edit added_text_rec = 1
gr_edit added_text[1].text = {}
gr_edit added_text[1].text.Arrpush "Mean mpg by rep78"

gr_edit AddTextBox added_text editor `=82+2' `=112'
gr_edit added_text_new = 2
gr_edit added_text_rec = 2
gr_edit added_text[2].text = {}
gr_edit added_text[2].text.Arrpush "rep78  mpg"

collapse mpg, by(rep78)

local z=2

forvalues i=0/4 {  //rows
local ++z
gr_edit AddTextBox added_text editor `=80-(`i'*4)' `=114'
gr_edit added_text_new = `z'
gr_edit added_text_rec = `z'
gr_edit added_text[`z'].text = {}
gr_edit added_text[`z'].text.Arrpush ///
"`=rep78[`z'-2]'      `=string(mpg[`z'-2],"%8.2f")' "
}
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

sysuse auto, clear

egen mpg1=cut(mpg), group(5) icode

levelsof mpg1, local(kk)

gen mean=.
gen ub=.
gen lb=.
gen loc=.
local z=1

foreach i of local kk {
ci for if mpg1==`i', wilson binomial

replace mean=r(mean) in `z'
replace ub=r(ub) in `z'
replace lb=r(lb) in `z'
replace loc=`i' in `z'
local ++z
}

label define a 0 "very poor"  1 poor  2  OK 3 good  4 vgood
label value  loc a

twoway (bar mean loc) (rcap ub lb loc), ///
xlab(, valuelabel angle(45))            ///
xtitle("Fuel Consumption")              ///
ytitle(mean) legend(off)

webuse nlswork, clear

local ytitle "`: var label ln_wage'"

set seed 2803
generate rnd = runiform()
bysort idcode (rnd): generate byte select = _n == 1
statsby mean=r(mean) ub=r(ub) lb=r(lb) N=r(N) if select, ///
by(grade) clear: ci ln_wage

levelsof grade, local(levels)

twoway rcap ub lb grade || ///
scatter mean grade, ytitle(`ytitle') legend(off) ///
subtitle("95% confidence intervals for mean", place(w)) xlabel(`levels')
// 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)
// Adding labels - standard error bar chart

webuse assembly, clear

generate upper=mean+2*std

serrbar mean std date, scale(2) yline(195)              ///
addplot( scatter  up date, msymbol(none) mlabel(upper))
// Odds ratio graph - selecting order of bars

** 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

tab estimate, gen(kk)

//order of bars
input a
1
2
3
4
5

egen axis= axis( a 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("") scheme(color)

*(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)
//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))
//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)
//Transparent graphs
//Using IrfanView - free image software
//http://www.irfanview.com/

histogram mpg if for==0, percent color(red) ///
xscale(range(10(5)45)) barwidth(3) yscale(range(0(10)60))

graph export "c:/a1.png", height(376)  width(506) replace
shell "C:\Program Files\IrfanView\i_view32.exe" "c:\a1.png"

histogram mpg if for==1, percent color(blue) ///
xscale(range(10(5)45)) barwidth(3)  yscale(range(0(10)60))

graph export "c:/a2.png", height(376) width(506) replace

//Then in Irfanview
//Edit pulldown menu
//Insert overlay/watermark image
//select graph  eg. a2.png
//The "overlay transparency" setting used was 80%
//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")
//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)

//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)
//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
//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("")
//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)
//Using the compact option in the by() option

sysuse auto, clear

forvalues i=10(10)40 {
local m`i'=string(`i'*0.4251437,"%6.2f")
}

twoway (scatter mpg weight, yaxis(1 2) xlabel(,angle(45)) ///
yscale( axis(2, alt))                                     ///
ylabel(                                                   ///
10 "`m10'"                                                ///
20 "`m20'"                                                ///
30 "`m30'"                                                ///
40 "`m40'"                                                ///
,axis(2))) ,                                              ///
by(for, title( "km/L",  orientation(vertical)            ///
size(med) position(3)) xrescale compact)
//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
//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))
//Mandelbrot Set
Example taken from Stata News
http://www.stata.com/stata-news/statanews.27.4.pdf
Please note that on my computer this took 3 hours to run.

clear all

mata:
real scalar escape(complex scalar Z, C, real scalar R, maxiter)
{
real scalar i
for (i=0; i < maxiter; i++) {
if (norm(Z) <= R) {
Z = Z*Z+C
}
else return(i)
}
return(i)
}
end

mata:
mandelbrot_set = J(201*201, 3, .)
cnt = 1
C = -0.8+0.156i
for(i=0; i<=200; i++) {
for(j=0; j<=200; j++) {
Z = C(-2+i*4/200, -2+j*4/200)
n = escape(Z, C, 100, 400)
mandelbrot_set[cnt,.]= (i, j, n)
cnt++
}
}
end

getmata (x y escape)= mandelbrot_set
quiet summarize escape
local min = r(min)-1
local max = r(max)+1
twoway contour escape y x, ccuts(`min'(1)`max')  ///
clegend(off) graphr(m(zero)) xscale(off)        ///
xlab(,nogrid)  yscale(off) ylab(,nogrid) scheme(s2color)

exit
//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)
//Generating Survival curve with lines at specific points in time

use http://www.stata-press.com/data/r12/drug2, clear

sts generate newvar1 =s
sts generate lb = lb(s)
sts generate ub = ub(s)

gen kk=newvar1 if studytime==20
sort kk
local a1=kk[1]

twoway  (rarea ub lb _t, sort  )                ///
(line  newvar1 studytime, sort c(stairstep))    ///
(pci  0 20 `a1' 20) (pci  `a1' 0  `a1' 20),     ///
ylabel(0(.2)1) name(a2, replace) ytitle("")     ///
legend(label(1 data set 1) label(2 data set 2)) ///
scheme(s1color) legend(off) xtitle("Studytime") ///
ylabel( .1(.2)1  ) ymlabel(  `a1', angle(45) )
violin plot
Requires: vioplot To download vioplot type the following on the Stata command line: ssc install vioplot
To run the example: copy the following into a do file and run sysuse auto, clear vioplot mpg, over(rep78) horizontal name(myplot) /// title("Violin Plot of Mileage") subtitle("By repair record") /// ytitle(Repair Record) ylab(, angle(horiz)) scheme(s2mono)
sequence plot
Requires: sq
To download sq type the following on the Stata command line:
ssc install sq

To run the example: copy the following into a do file and run


use http://www.wzb.eu/~kohler/ado/youthemp.dta, clear
reshape long st, i(id) j(order)
sqset st id order
sqindexplot, scheme(vg_palec)




strippplot

Requires: stripplot
To download stripplot type the following on the Stata command line:
ssc install stripplot

To run the example: copy the following into a do file and run

sysuse census, clear

local abbrev ///
AK AL AR AZ CA CO CT DE FL GA HI IA ///
ID IL IN KS KY LA MA MD ME MI MN MO ///
MS MT NC ND NE NH NJ NM NV NY OH OK ///
OR PA RI SC SD TN TX UT VA VT WA WI WV WY
gen abbrev = ""
tokenize "`abbrev'"
qui forval i = 1/50 {
replace abbrev = "``i''" in `i'
}
set scheme s1color

stripplot medage, stack ms(Sh) msize(*3.5) width(0.5) ///
mla(abbrev) mlabpos(0) mlabsize(*.9) ysc(off) plotregion(lstyle(none))
strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse auto, clear gen pipe = "|" stripplot price, over(rep78) box(barw(0.3)) ms(none) mla(pipe) boffset(0.3) From the Statalist: Nick cox 15/10/2010
strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse auto, clear gen pipe = "|" stripplot mpg, ms(none) mlabpos(0) mlabel(pipe) mlabsize(*2) stack From striplot help: Nick cox
strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse auto, clear stripplot mpg, over(rep78) stack h(0.5) bar(lcolor(red)) From striplot help: Nick cox
strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot The data (Challenger shuttle O-ring damage) can be obtained from: http://www.stata.com/bookstore/swsdl.html To run the example: copy the following into a do file and run
use shuttle.dta, clear logit damage temp predict pre stripplot pre, over(temp) stack ms(sh) height(0.4) /// title("Challenger shuttle O-ring damage") ylabel(,alt labsize(2)) /// addplot(mspline temp pre, bands(20)) From striplot help: Nick cox
strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse bplong, clear egen group = group(age sex), label stripplot bp*, bar over(when) by(group, compact col(1) note("")) /// ysc(reverse) subtitle(, pos(9) ring(1) nobexpand bcolor(none) /// placement(e)) ytitle("") xtitle(Blood pressure (mm Hg)) From striplot help: Nick cox
ciplot
Example of the use of ciplot. Requires: cipplot To download ciplot type the following on the Stata command line: ssc install ciplot To run the example: copy the following into a do file and run
webuse citytemp, clear ciplot heatdd cooldd, by(division) xla(, ang(45)) From ciplot help: Nick cox
Venn diagram using venndiag 

Requires: venndiag To download venndiag type the following on the Stata command line: ssc install venndiag To run the example below copy the following into a do file and run clear input hayfever eczema asthma freq 1 0 0 31088 1 1 0 9863 0 1 0 43522 0 1 1 9258 0 0 1 35299 1 0 1 11024 1 1 1 6200 0 0 0 345262 end list expand freq venndiag asthma eczema hayfever
Venn diagram - using pvenn


Requires: pvenn
To download pvenn type the following on the Stata command line:
ssc install pvenn
To run the example below copy the following into a do file and run clear input hayfever eczema asthma freq 1 0 0 31088 1 1 0 9863 0 1 0 43522 0 1 1 9258 0 0 1 35299 1 0 1 11024 1 1 1 6200 0 0 0 345262 end list expand freq pvenn asthma eczema hayfever
triplot

Plots 3 variables (proportions or percentages) the total of each to equal either 1 or 100

Requires: triplot
to download triplot type the following on the Stata command line:
ssc install triplot

To run the example below copy the following into a do file and run

clear
input a1 a2 a3 str10 name
10 10 80 John
80 10 10 Fred
25 25 50 Jane
90 5 5 Helen
0 0 100 Ed
50 25 25 Kate
20 60 20 Michael
25 25 50 Darren
5 90 5 Samar
end
list

triplot a1 a2 a3 , mlab(name) mlabcolor(black) mcolor(blue) ///
mlabsize(*0.9) max(100) title("Opinion a1 a2 a3")
catplot


Plots categorical variables

Requires: catplot
Plot categorical variables
To download catplot type the following on the Stata command line:
ssc install catplot

To run the example below copy the following into a do file and run

sysuse auto, clear

catplot bar rep78, by(foreign) percent(foreign)
 catplot
Plots categorical variables -

Requires: catplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install catplot

To run the example: copy the following into a do file and run


sysuse auto, clear

catplot  rep78, over(for) stack asyvars  perc(for)	///
blabel(bar, position(center) format(%3.1f)) ///
legend(off)

 catplot
Plots categorical variables -

Requires: catplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install catplot

To run the example: copy the following into a do file and run


clear	all
set more off

input quest str25 q      a1 a2 a3 a4 a5 a6
1 "Question 1"           0  2  37 45 12 4
1 "Benchmark Q1"         2  5  25 47 17 4
2 "Question 2"           1  37  2 40 17 3
2 "Benchmark Q2"         2  5  25 47  4 17
3 "Question 3"           1  2  40 37 17 3
3 "Benchmark Q3"         2  5  25 47 17 4
4 "Question 4"           1  2  37  17 3 40
4 "Benchmark Q4"         2  5  47 25 17 4
end

sort quest q
gen q_sum=_n

label define kk 1 q1 2 b1 3 q2 4 b2 5 q3 6 b3 7 q4 8 b4
label value q_sum kk

reshape long a, i(quest q) j(data)
label var data "Question No."

expand a

split q

replace q2=substr(q2,2,1) if length(q2)>1

catplot  data, over(q1, gap(40) label(labgap(5)))               ///
over(q2 )  stack asyvars perc(q)	                            ///
blabel(bar, size(1) position(center)orient(vert) format(%4.1f)) ///
legend(                                                         ///
label(1 "Missing")                                              ///
label(2 "Never")                                                ///
label(3 "Rarely")                                               ///
label(4 "Occasionaly")                                          ///
label(5 "Mostly")                                               ///
label(6 "Always") rows(1) keygap(0) symxsize(7) bexpand         ///
span size(small))                                               ///
ytitle("Percentage")

exit


tabplot


Plots categorical variables in the from of a table


Requires: tabplot
to download catplot type the following on the Stata command line:
ssc install tabplot

To run the example below copy the following into a do file and run

sysuse auto, clear

tabplot for rep78, percent(foreign) showval(offset(0.05) format(%2.1f))



spine

Plots width proportional to frequency

Requires: spineplot
to download spineplot type the following on the Stata command line:
ssc install spineplot

To run the example below copy the following into a do file and run

sysuse auto, clear
spineplot foreign rep78, xti(frequency, axis(1)) ///
xla(0(10)60, axis(1)) xmti(1/69, axis(1))


spine

Plots width proportional to frequency Requires: spineplot to download spineplot type the following on the Stata command line: ssc install spineplot
To run the example below copy the following into a do file and run sysuse auto, clear replace rep78=0 if missing(rep78) bysort foreign rep78 : gen N = _N bysort foreign : gen Na1 = (N/_N)*100 by foreign : gen N1 = string(Na1,"%5.2f") +"%" label define kk 0 "missing", label values rep78 kk spineplot rep78 foreign, bar1(bcolor(gs14)) /// percent missing /// bar2(bcolor(gs11)) /// bar3(bcolor(gs8)) /// bar4(bcolor(gs5)) /// bar5(bcolor(gs2)) /// bar6(bcolor(red)) text(N1)

cycleplot

Plots the values for each period vertically

Requires: cycleplot
to download cycleplot type the following on the Stata command line:
ssc install cycleplot

For the example below get the data (Co2 data) from: http://cdiac.ornl.gov/ftp/ndp001/maunaloa.co2

To run the example below copy the following into a do file and run

mvdecode m1-m12 , mv(-99.99=.)
reshape long m, i(year) j(month)
cycleplot m month year, mylabels(`c(Months)') xlabel(, angle(45))




sliceplot

Spreads out the graph over a number of rows; to aid reading the graph

Requires: sliceplot
to download sliceplot type the following on the Stata command line:
ssc install sliceplot

For the example below get the data (Co2 data) from: http://cdiac.ornl.gov/ftp/ndp001/maunaloa.co2

To run the example: copy the following into a do file and run

mvdecode m1-m12 , mv(-99.99=.)
reshape long m, i(year) j(month)
sliceplot line month year, slices(4)




cpyxplot

Plots the cross product of the variables indicted in command


Requires: cpyxplot
to download cpyxplot type the following on the Stata command line:
ssc install cpyxplot

To run the example: copy the following into a do file and run

sysuse auto, clear
cpyxplot for \rep78 mpg turn length, graphregion(color(ltblue))




beamplot

The seasaw way of looking at the data

Requires: beamplot
to download beamplot type the following on the Stata command line:
ssc install beamplot

To run the example: copy the following into a do file and run

sysuse auto, clear
beamplot mpg, by(foreign) over(rep78)




hangroot

Distribution checking graph

Requires: hangroot
to download hangroot type the following on the Stata command line:
ssc install hangroot

To run the example: copy the following into a do file and run

sysuse nlsw88, clear
gen ln_w = ln(wage)
reg ln_w grade age ttl_exp tenure
predict resid, resid
hangroot resid, bar




byhist

Draws a binary variable next to each other for each bin width

Requires: byhist
to download byhist type the following on the Stata command line:
ssc install byhist

To run the example: copy the following into a do file and run

sysuse auto, clear
byhist mpg, by(foreign)




bihist

Draws a binary variable; one going up and the other going down, for each bin width

Requires: byhist
to download bihist type the following on the Stata command line:
ssc install bihist

To run the example: copy the following into a do file and run

sysuse auto, clear
byhist mpg, by(foreign)




plotmatrix

Display the values of a matrix using twoway area

Requires: plotmatrix
to download plotmatrix type the following on the Stata command line:
ssc install plotmatrix

To run the example: copy the following into a do file and run

sysuse auto
reg price mpg trunk weight length turn, nocons
mat regmat = e(V)
plotmatrix, m(regmat) c(red) ylabel(,angle(0))



Density Plot

Requires: spgrid, spkde, spmap & 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 spmap
and then
ssc install mylabels

*------------------------------------------------

* 1. Normalize variables in the range [0,1]

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)

* 4. Draw the density plot

use "2D-Kde.dta", clear
recode lambda (.=0)
spmap lambda using "2D-GridCells.dta",  ///
id(spgrid_id) clnum(20) fcolor(Rainbow) ///
ocolor(none ..) legend(off)             ///
point(data("xy.dta") x(x) y(y))         ///
freestyle aspectratio(1)                ///
xtitle(" " "Mileage (mpg)")             ///
xlab( `XLAB')                           ///
ytitle(" " "Price ({c S|}US)")          ///
ylab(`YLAB', angle(0))

exit

eclplot
Plot estimates with confidence limits
Requires: eclplot, parmby & sencode
to download these programs type the following on the Stata command line (if not already loaded):
ssc install eclplot
and then
ssc install parmby
and then
ssc install sencode

To run the example: copy the following into a do file and run

sysuse auto, clear

tabulate rep78, gene(rep78_)

parmby "regress mpg rep78_*, noconst", by(foreign) label norestore

sencode label if parm!="_cons", gene(parmlab)
label var parmlab "Repair record 1978"

label var estimate "Mean mileage (mpg)"
eclplot estimate min95 max95 parmlab, eplot(bar) estopts(barwidth(0.25)) ///
supby(foreign, ///
spaceby(0.25)) xscale(range(0 6)) xlabel(1(1)5, angle(30))





grcomb
Combines graphs

Requires: grcomb
to download this program type the following on the Stata command line (if not already loaded):
ssc install grcomb

To run the example: copy the following into a do file and run

webuse nhanes2f, clear

grcomb graph box copper zinc iron , v(1)




sixplot
Displays six diagnostic and descriptive graphs for a single variable Requires: sixplot to download this program type the following on the Stata command line (if not already loaded): ssc install sixplot To run the example: copy the following into a do file and run sysuse uslifeexp.dta
sixplot le_male
zmap
zmap graphs (or maps) binned values of a variable z with respect to two variables x and y treated as Cartesian coordinates. Requires: zmap to download this program type the following on the Stata command line (if not already loaded): ssc install zmap To run the example: copy the following into a do file and run
webuse nlswork, clear egen mean = mean(ln_wage), by(age grade) egen tag = tag(age grade) label variable mean "mean ln wage" summarize ln_wage if !missing(age, grade), detail zmap mean age grade if tag, breaks(.993 1.166 1.361 1.641 /// 1.964 2.275 2.456) ms(S ..) ysc(on) xsc(on) yla(0/18, ang(h)) /// ytitle(`: var label grade') mxla(15(5)45) note("") /// color(blue blue*0.5 orange*0.5 orange)
netplot
An important part of what makes social network analysis so fascinating to broad audiences is the possibility of visualization of networks. netplot produces a network plot Requires: netplot to download this program type the following on the Stata command line (if not already loaded): ssc install netplot To run the example: copy the following into a do file and run //create a random network with 10 nodes //(code adopted from the helpfile of stata2pajek) clear set obs 200 gen i=int(uniform()*10)+1 gen j=int(uniform()*10)+1 contract i j, freq(strength) drop if i==j sort i j drop if strength <3 // --> keep only the "stronger" links // Draw the network netplot i j, type(circle) label
cmogram
cmogram graphs the means, medians, frequencies, or proportions
of yvar, conditional on xvar.

Requires: cmogram
to download this program type the following on the Stata command
line (if not already loaded):
ssc install cmogram

To run the example: copy the following into a do file and run

sysuse auto, clear

cmogram mpg weight, histopts(bin(5)) lfit cutpoint(3250) ///
lineat(3000 3250 3500) controls(price)

ellip
ellip calculates a confidence ellipse from the elliptically
distributed variables yvar and xvar, and then graphs the
confidence ellipse using graph twoway line.

Requires: ellip
to download this program type the following on the Stata command
line (if not already loaded):
ssc install ellip

To run the example: copy the following into a do file and run

sysuse auto, clear

ellip mpg weight, by(foreign, total legend(off)) ///
total tlabel(Total as a by-group) plot(scatter mpg weight)


drarea
Multiple graphics are produced in Stata by the rule "last drawn
first seen". So the objects that are drawn last are the ones
that are observed. The implication of this principle is that
when overlapping two rarea graphs the overlapping area is hidden
by the second rarea graph.  This command drarea overlays two
range area plots by merging the two colours and hence highlighting
the true overlap.

Requires: drarea
to download this program type the following on the Stata command
line (if not already loaded):
ssc install drarea

To run the example: copy the following into a do file and run


sysuse sp500, clear

generate high2 = high+15*uniform()

generate low2 = low+15*uniform()

drarea high low high2 low2 date in 1/20


linkplot
linkplot plots yvarlist versus xvar such that data points are
linked (i.e. connected) within groups defined by distinct values of
linkvar. For example, with paired data it might be desired to
link each pair, or with panel data it might be desired to link
observations within each panel.

Requires: linkplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install linkplot

To run the example: copy the following into a do file and run


clear
input ///
A          B         id
13.2       14.0          1
8.2        8.8          2
10.9       11.2          3
14.3       14.2          4
10.7       11.8          5
6.6        6.4          6
9.5        9.8          7
10.8       11.3          8
8.8        9.3          9
13.3       13.6         10
end

rename A wearA
rename B wearB
reshape long wear, string i(id) j(j)
encode j, gen(material)

linkplot material wear, link(id) yla(1 2, valuelabel) ///
ysc(r(0.5 2.5)) yla(, ang(h))

tddens
Bivariate kernel density graphs over a grid

Requires: tddens
to download this program type the following on the Stata command
line (if not already loaded):
ssc install tddens

To run the example: copy the following into a do file and run


sysuse auto, clear

tddens price mpg, s b


Lexis diagram
Two dimensional diagram that is used to represent events
(such as births or deaths) that occur to individuals belonging
to different cohorts.

Requires: evhistplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install evhistplot

To run the example: copy the following into a do file and run


clear
webuse set ///
"http://oldsite.soziologie-blossfeld.de/eha/stata/do_files/Data/"

webuse rrdat1.dta

* Convert event times to dates. Are arbitrarily set to fifteenth
day of month.

foreach var of varlist tstart tb te tmar ti tfin {
gen `var'date = mdy(mod(`var' - 1, 12) + 1, 15, (`var' - 1) / 12 + 1900)
local labvar : variable label `var'
la var `var'date "`labvar'"
format %d `var'date
}

* replace with missing where observation is actually right censored
replace tmardate = . if tmar == 0
la var tmardate "Date of marriage"
replace tfindate = . if tfin == ti

* Lexis plot with date of marriage and job starts labelled
* according to rank

evhistplot tstartdate tmardate, id(id) start(1jan1940) ///
end(31dec1981) birth(tbdate) evtype(noj) nsub(20) xtitle(Calendar time)

Deviation plots
Plots each data point relative to some appropriate mean with a marker
symbol and a spike connecting marker and mean.

Requires: devnplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install devnplot

To run the example: copy the following into a do file and run


webuse systolic, clear

version 9: anova systolic drug disease drug*disease
predict predict
predict residual, residual

devnplot systolic drug disease, level(predict) superplines


Statplot
Statplot is an alternative to graph hplot, graph hbar and graph dot.
It allows grouping on the axis and other options.

Requires: statplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install statplot

To run the example: copy the following into a do file and run


webuse systolic, clear

sysuse census, clear
statplot marriage divorce, over(region) s(sum) xpose varnames


 mm_plot
mm_plot is a user written Mata graph command that allow you
to plot a Mata matrix.

Requires: moremata
to download this program type the following on the Stata command
line (if not already loaded):
ssc install moremata

To run the example: copy the following into a do file and run


set more off
mata:

for(i=0;i<=2*pi()*1000; i++) {

a=(i/1000)*cos(i/1000)
a1=(i/1000)*sin(i/1000)

if(i==0) {
za=a
za1=a1
}
else {
za=za\a
za1=za1\a1
}
c=za,za1
}

mata: mm_plot(c)

end


 cdfplot
cdfplot plots the sample cumulative distribution function.
Distributions can be compared within subgroups defined by
a second variable. The best fitting normal (Gaussian)
model may be superimposed over the sample c.d.f.

Requires: cdfplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install cdfplot

To run the example: copy the following into a do file and run


sysuse auto,replace
cdfplot length [fw=rep78], by(foreign) norm saving(mygraph,replace)

 riskplot
The riskplot is a graphical aid to the investigation
of the contributions of risk factors on outcomes of interest.
(see SJ Vol 10 No 1)

Requires: riskplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install risk,all
(The all option ensures that the data (data_riskplot.dta)
will also be downloaded)

To run the example: copy the following into a do file and run


use c:/data/data_riskplot.dta, clear  //path may need to be changed
set scheme sj

riskplot depr1995 sex sclass if Idep91==1 [pw=wg], path obs        /*
*/ thick(20) c(. red)  title(Risk plot for subjects with mild   /*
*/ or severe depression at baseline) subtitle((results using    /*
*/ sampling weights), margin(b+5)) scale(0.9) ytitle(depression /*
*/ score 1995) saving(riskplotWG, replace)

 aaplot
aaplot graphs a scatter plot for yvar versus xvar with linear
and/or quadratic fit superimposed.  The equation(s) and
R-square statistics of the fits shown are also shown at the
top of the graph.

Requires: aaplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install aaplot

To run the example: copy the following into a do file and run


sysuse auto, clear
gen gpm = 1000 / mpg
label var gpm "Gallons per thousand miles"

aaplot gpm weight, name(g1)

 hull plot
cvxplot makes a scatterplot and draw convex hulls of a group of
points in two-dimensional space. Each hull is defined by two lines
joining the bottom-left point to the top-right point.

Requires: cvxplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install cvxhull

To run the example: copy the following into a do file and run


sysuse auto, clear

gen foreign1=foreign
label define for 1 For 0 Dom
label values  foreign1 for

cvxhull  mpg weight, group(foreign) hulls(2) ///
scat(mlab(foreign1) mlabpos(c) msym(i) ysc(r(0,60)))

 Pareto plot
pdplot produces a Pareto dot plot

Requires: pdplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install pdplot

To run the example: copy the following into a do file and run


sysuse auto, clear
pdplot mpg

 Centred bar plots
Centred bar plots shows absolute or relative frequencies of
yvar as centred bar plots.

Centred bar plots are often used in archaeology to show
frequencies of different kinds of artefact at different
levels or ages, sometimes under the title of battleship
curves or battleship diagrams.

Requires: cbarplot
to download this program type the following on the Stata
command line (if not already loaded):
ssc install cbarplot

To run the example: copy the following into a do file and run


clear
input levels freqcores freqblanks freqtools
25 21 32 70
24 36 52 115
23 126 650 549
22 159 2342 1633
21 75 487 511
20 176 1090 912
19 132 713 578
18 46 374 266
17 550 6182 1541
16 76 846 349
15 17 182 51
14 4 51 14
13 29 228 130
12 135 2227 729
end
reshape long freq, i(levels) j(kind) string

cbarplot levels kind [fw=freq], percent(levels) mlabsize(*.6)

 heatmap
Graphs a heatmap.

Requires: cbarplot
to download this program type the following on the Stata
command line (if not already loaded):
ssc install hmap

To run the example: copy the following into a do file and run


// producing the data
clear all
set obs 64
generate n=int(uniform()*10)
generate x=1+int((_n-1)/8)
generate y=1+mod((_n-1),8)
label define xlab 1 "one" 2 "two" 7 "seven" 8 "eight"
label define ylab 3 "three" 4 "four" 5 "five" 6 "six"
label value x xlab
label value y ylab
table y x [fw=n]

hmap x y n

simpplot
// Plot describing p-values from a simulation by comparing
//nominal significance levels with the coverages

Requires: simpplot
to download this program type the following on the Stata
command line (if not already loaded):
ssc install simpplot

program drop _all
program define sim, rclas
drop _all
set obs 500
gen x = rchi2(2)

ttest x=2 in 1/50
return scalar p50 = r(p)

ttest x=2
return scalar p500 = r(p)
end

set seed 12345
simulate p50=r(p50) p500=r(p500), ///
reps(5000) : sim

label var p50 "N=50"
label var p500 "N=500"

simpplot p50 p500, main1opt(mcolor(red)) ///
main2opt(mcolor(blue))
Fractal
// Generates fractals

Requires: fractal
to download this program type the following on the Stata
command line (if not already loaded):
ssc install fractal

clear

fractal  ,hs(0,.2,.3,.40,.5,.618,.75,.85,1.0)   ///
vs(0,.25,.2,.35,.25,.516,.366,.42,.2) hr(0 100) ///
vr(0 200) iter(3) savegraph

graph combine _frctl1.gph _frctl2.gph _frctl3.gph
Motion Chart
A "Motion Chart" is a scatter plot for time-series data.
You can hit "play" and see how the points move over time.
They were made famous, for example, by Hans Rosling's
internet videos: http://www.gapminder.org/

"Example of motion graph for code below"

Requires: motionchart
to download this program:
Open Stata viewer
type the following in:
net describe motionchart, from(http://kk-adofiles.googlecode.com/hg)

type help motionchart

Read the pharagraph on setting the Adobe Flash Player

sysuse bplong.dta , clear
generate year = 2007+when
decode sex , gen(gender)

motionchart patient year bp sex using "my_test.html" in 115/125 ,   ///
replace title("Blood Pressure")                                     ///
subtitle("Example motionchart: created in Stata with motionchart.ado")
binscatter
binscatter generates binned scatterplots, and is optimized for
speed in large datasets.

Binned scatterplots provide a non-parametric way of visualizing the
relationship between two variables. With a large number of observations,
a scatterplot that plots every data point would become too crowded to
interpret visually.  binscatter groups the x-axis variable into
equal-sized bins, computes the mean of the x-axis and y-axis variables
within each bin, then creates a scatterplot of these data points.

Requires: binscatter
to download this program type the following on the Stata
command line (if not already loaded):
ssc install binscatter

clear all

sysuse nlsw88, clear

keep if inrange(age,35,44) & inrange(race,1,2)

scatter wage tenure ,  title("Graph produced by Scatter") name(g1)

// The scatter was too crowded to be easily interpetable. The binscatter
is cleaner, try a quadratic fit.

binscatter wage tenure, line(qfit) xscale(range(0,25)) ///
xlabel(0(5)25) ylabel(0(10)40) yscale(range(0,40))     ///
title("Graph produced by Binscatter") name(g2)

graph combine g1 g2
							

Violin Plot

violin plot
Requires: vioplot To download vioplot type the following on the Stata command line: ssc install vioplot
To run the example: copy the following into a do file and run sysuse auto, clear vioplot mpg, over(rep78) horizontal name(myplot) /// title("Violin Plot of Mileage") subtitle("By repair record") /// ytitle(Repair Record) ylab(, angle(horiz)) scheme(s2mono)
						

Sequence Plot

sequence plot Requires: sq To download sq type the following on the Stata command line: ssc install sq To run the example: copy the following into a do file and run use http://www.wzb.eu/~kohler/ado/youthemp.dta, clear reshape long st, i(id) j(order) sqset st id order sqindexplot, scheme(vg_palec)
						

asciiplot

asciiplot

Requires: asciiplot
To download asciiplot type the following on the Stata command line:
ssc install asciiplot

To run the example: copy the following into a do file and run

asciiplot
							

strippplot

strippplot

Requires: stripplot
To download stripplot type the following on the Stata command line:
ssc install stripplot

To run the example: copy the following into a do file and run

sysuse census, clear

local abbrev ///
AK AL AR AZ CA CO CT DE FL GA HI IA ///
ID IL IN KS KY LA MA MD ME MI MN MO ///
MS MT NC ND NE NH NJ NM NV NY OH OK ///
OR PA RI SC SD TN TX UT VA VT WA WI WV WY
gen abbrev = ""
tokenize "`abbrev'"
qui forval i = 1/50 {
replace abbrev = "``i''" in `i'
}
set scheme s1color

stripplot medage, stack ms(Sh) msize(*3.5) width(0.5) ///
mla(abbrev) mlabpos(0) mlabsize(*.9) ysc(off) plotregion(lstyle(none))
						

strippplot

strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse auto, clear gen pipe = "|" stripplot price, over(rep78) box(barw(0.3)) ms(none) mla(pipe) boffset(0.3) From the Statalist: Nick cox 15/10/2010
						

strippplot

strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse auto, clear gen pipe = "|" stripplot mpg, ms(none) mlabpos(0) mlabel(pipe) mlabsize(*2) stack From striplot help: Nick cox
								

strippplot

strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse auto, clear stripplot mpg, over(rep78) stack h(0.5) bar(lcolor(red)) From striplot help: Nick cox
						

strippplot

strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot The data (Challenger shuttle O-ring damage) can be obtained from: http://www.stata.com/bookstore/swsdl.html To run the example: copy the following into a do file and run
use shuttle.dta, clear logit damage temp predict pre stripplot pre, over(temp) stack ms(sh) height(0.4) /// title("Challenger shuttle O-ring damage") ylabel(,alt labsize(2)) /// addplot(mspline temp pre, bands(20)) From striplot help: Nick cox
							

strippplot

strippplot
Another example of the use of striplot. Requires: stripplot To download stripplot type the following on the Stata command line: ssc install stripplot To run the example: copy the following into a do file and run
sysuse bplong, clear egen group = group(age sex), label stripplot bp*, bar over(when) by(group, compact col(1) note("")) /// ysc(reverse) subtitle(, pos(9) ring(1) nobexpand bcolor(none) /// placement(e)) ytitle("") xtitle(Blood pressure (mm Hg)) From striplot help: Nick cox
							

ciplot

ciplot
Example of the use of ciplot. Requires: cipplot To download ciplot type the following on the Stata command line: ssc install ciplot To run the example: copy the following into a do file and run
webuse citytemp, clear ciplot heatdd cooldd, by(division) xla(, ang(45)) From ciplot help: Nick cox
								

Venn diagram using venndiag

Venn diagram using venndiag

Requires: venndiag To download venndiag type the following on the Stata command line: ssc install venndiag To run the example below copy the following into a do file and run clear input hayfever eczema asthma freq 1 0 0 31088 1 1 0 9863 0 1 0 43522 0 1 1 9258 0 0 1 35299 1 0 1 11024 1 1 1 6200 0 0 0 345262 end list expand freq venndiag asthma eczema hayfever
							

Venn diagram - using pvenn

Venn diagram - using pvenn


Requires: pvenn
To download pvenn type the following on the Stata command line:
ssc install pvenn
To run the example below copy the following into a do file and run clear input hayfever eczema asthma freq 1 0 0 31088 1 1 0 9863 0 1 0 43522 0 1 1 9258 0 0 1 35299 1 0 1 11024 1 1 1 6200 0 0 0 345262 end list expand freq pvenn asthma eczema hayfever
						

triplot

triplot

Plots 3 variables (proportions or percentages) the total of each to equal either 1 or 100

Requires: triplot
to download triplot type the following on the Stata command line:
ssc install triplot

To run the example below copy the following into a do file and run

clear
input a1 a2 a3 str10 name
10 10 80 John
80 10 10 Fred
25 25 50 Jane
90 5 5 Helen
0 0 100 Ed
50 25 25 Kate
20 60 20 Michael
25 25 50 Darren
5 90 5 Samar
end
list

triplot a1 a2 a3 , mlab(name) mlabcolor(black) mcolor(blue) ///
mlabsize(*0.9) max(100) title("Opinion a1 a2 a3")

catplot

catplot


Plots categorical variables

Requires: catplot
Plot categorical variables
To download catplot type the following on the Stata command line:
ssc install catplot

To run the example below copy the following into a do file and run

sysuse auto, clear

catplot bar rep78, by(foreign) percent(foreign)
 catplot
Plots categorical variables -

Requires: catplot
to download this program type the following on the Stata command
line (if not already loaded):
ssc install catplot

To run the example: copy the following into a do file and run


 sysuse auto, clear

catplot  rep78, over(for) stack asyvars  perc(for)	///
blabel(bar, position(center) format(%3.1f)) ///
legend(off)

									

catplot

catplot Plots categorical variables - Requires: catplot to download this program type the following on the Stata command line (if not already loaded): ssc install catplot To run the example: copy the following into a do file and run clear all set more off input quest str25 q a1 a2 a3 a4 a5 a6 1 "Question 1" 0 2 37 45 12 4 1 "Benchmark Q1" 2 5 25 47 17 4 2 "Question 2" 1 37 2 40 17 3 2 "Benchmark Q2" 2 5 25 47 4 17 3 "Question 3" 1 2 40 37 17 3 3 "Benchmark Q3" 2 5 25 47 17 4 4 "Question 4" 1 2 37 17 3 40 4 "Benchmark Q4" 2 5 47 25 17 4 end sort quest q gen q_sum=_n label define kk 1 q1 2 b1 3 q2 4 b2 5 q3 6 b3 7 q4 8 b4 label value q_sum kk reshape long a, i(quest q) j(data) label var data "Question No." expand a split q replace q2=substr(q2,2,1) if length(q2)>1 catplot data, over(q1, gap(40) label(labgap(5))) /// over(q2 ) stack asyvars perc(q) /// blabel(bar, size(1) position(center)orient(vert) format(%4.1f)) /// legend( /// label(1 "Missing") /// label(2 "Never") /// label(3 "Rarely") /// label(4 "Occasionaly") /// label(5 "Mostly") /// label(6 "Always") rows(1) keygap(0) symxsize(7) bexpand /// span size(small)) /// ytitle("Percentage") exit

spine


tabplot


Plots categorical variables in the from of a table


Requires: tabplot
to download catplot type the following on the Stata command line:
ssc install tabplot

To run the example below copy the following into a do file and run

sysuse auto, clear

tabplot for rep78, percent(foreign) showval(offset(0.05) format(%2.1f))



spine

Plots width proportional to frequency

Requires: spineplot
to download spineplot type the following on the Stata command line:
ssc install spineplot

To run the example below copy the following into a do file and run

sysuse auto, clear
spineplot foreign rep78, xti(frequency, axis(1)) ///
xla(0(10)60, axis(1)) xmti(1/69, axis(1))


spine

Plots width proportional to frequency Requires: spineplot to download spineplot type the following on the Stata command line: ssc install spineplot
To run the example below copy the following into a do file and run sysuse auto, clear replace rep78=0 if missing(rep78) bysort foreign rep78 : gen N = _N bysort foreign : gen Na1 = (N/_N)*100 by foreign : gen N1 = string(Na1,"%5.2f") +"%" label define kk 0 "missing", label values rep78 kk spineplot rep78 foreign, bar1(bcolor(gs14)) /// percent missing /// bar2(bcolor(gs11)) /// bar3(bcolor(gs8)) /// bar4(bcolor(gs5)) /// bar5(bcolor(gs2)) /// bar6(bcolor(red)) text(N1)

Density Plot


cycleplot

Plots the values for each period vertically

Requires: cycleplot
to download cycleplot type the following on the Stata command line:
ssc install cycleplot

For the example below get the data (Co2 data) from: http://cdiac.ornl.gov/ftp/ndp001/maunaloa.co2

To run the example below copy the following into a do file and run

mvdecode m1-m12 , mv(-99.99=.)
reshape long m, i(year) j(month)
cycleplot m month year, mylabels(`c(Months)') xlabel(, angle(45))




sliceplot

Spreads out the graph over a number of rows; to aid reading the graph

Requires: sliceplot
to download sliceplot type the following on the Stata command line:
ssc install sliceplot

For the example below get the data (Co2 data) from: http://cdiac.ornl.gov/ftp/ndp001/maunaloa.co2

To run the example: copy the following into a do file and run

mvdecode m1-m12 , mv(-99.99=.)
reshape long m, i(year) j(month)
sliceplot line month year, slices(4)




cpyxplot

Plots the cross product of the variables indicted in command


Requires: cpyxplot
to download cpyxplot type the following on the Stata command line:
ssc install cpyxplot

To run the example: copy the following into a do file and run

sysuse auto, clear
cpyxplot for \rep78 mpg turn length, graphregion(color(ltblue))




beamplot

The seasaw way of looking at the data

Requires: beamplot
to download beamplot type the following on the Stata command line:
ssc install beamplot

To run the example: copy the following into a do file and run

sysuse auto, clear
beamplot mpg, by(foreign) over(rep78)




surface

3d plot

Requires: surface
to download surfacet type the following on the Stata command line:
ssc install surface

To run the example: copy the following into a do file and run

use surf3,clear
set more off
surface x y z, orient(xzy) saving(s3d)




hangroot

Distribution checking graph

Requires: hangroot
to download hangroot type the following on the Stata command line:
ssc install hangroot

To run the example: copy the following into a do file and run

sysuse nlsw88, clear
gen ln_w = ln(wage)
reg ln_w grade age ttl_exp tenure
predict resid, resid
hangroot resid, bar




byhist

Draws a binary variable next to each other for each bin width

Requires: byhist
to download byhist type the following on the Stata command line:
ssc install byhist

To run the example: copy the following into a do file and run

sysuse auto, clear
byhist mpg, by(foreign)




bihist

Draws a binary variable; one going up and the other going down, for each bin width

Requires: byhist
to download bihist type the following on the Stata command line:
ssc install bihist

To run the example: copy the following into a do file and run

sysuse auto, clear
byhist mpg, by(foreign)




plotmatrix

Display the values of a matrix using twoway area

Requires: plotmatrix
to download plotmatrix type the following on the Stata command line:
ssc install plotmatrix

To run the example: copy the following into a do file and run

sysuse auto
reg price mpg trunk weight length turn, nocons
mat regmat = e(V)
plotmatrix, m(regmat) c(red) ylabel(,angle(0))




full_palette

Displays a palette of Stata's graph colors, along with their names and RGB numbers

Requires: full_palette
to download full_palette type the following on the Stata command line:
ssc install full_palette

To run the example: copy the following into a do file and run

full_palette




parea

(Windows only) Allows the use of patterns to fill in areas under graphs

Requires: parea
to download parea type the following on the Stata command line:
ssc install parea

To run the example: copy the following into a do file and run

sysuse auto, clear
twoway parea d w, sort pattern(pattern9) || ///
parea l w if w<=3600, sort pattern(pattern6) || ///
parea l w if w>=3600, sort pattern(pattern4) ,legend(rows(1))





spkde

Requires: spgrid, spkde & spmap
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 spmap

Also remember to download the following dataset:
Italy-OutlineCoordinates.dta
(from where spmap is downloaded) or use the following
net get spmap.pkg


Italy-DataPoints.dta
(from where spkde is downloaded) or use the following
net get spkde.pkg


To run the example: copy the following into a do file and run

set more off

spgrid using "Italy-OutlineCoordinates.dta",   ///
resolution(w10) unit(kilometers) ///
cells("GridCells.dta") ///
points("GridPoints.dta") ///
replace compress dots

use "Italy-DataPoints.dta", clear

spkde dcvd95 pop95 using "GridPoints.dta", ///
xcoord(xcoord) ycoord(ycoord) ///
bandwidth(fbw) fbw(100) dots ///
saving("Kde.dta", replace)

use "Kde.dta", clear

generate ratio = dcvd95_lambda / pop95_lambda * 1000

spmap ratio using "GridCells.dta", ///
id(spgrid_id) clnum(20) ///
fcolor(Rainbow) ocolor(none ..) ///
legend(off)
Density Plot

Requires: spgrid, spkde, spmap & 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 spmap
and then
ssc install mylabels

*------------------------------------------------

* 1. Normalize variables in the range [0,1]

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)

* 4. Draw the density plot

use "2D-Kde.dta", clear
recode lambda (.=0)
spmap lambda using "2D-GridCells.dta",  ///
id(spgrid_id) clnum(20) fcolor(Rainbow) ///
ocolor(none ..) legend(off)             ///
point(data("xy.dta") x(x) y(y))         ///
freestyle aspectratio(1)                ///
xtitle(" " "Mileage (mpg)")             ///
xlab( `XLAB')                           ///
ytitle(" " "Price ({c S|}US)")          ///
ylab(`YLAB', angle(0))

exit

sixplot


eclplot
Plot estimates with confidence limits
Requires: eclplot, parmby & sencode
to download these programs type the following on the Stata command line (if not already loaded):
ssc install eclplot
and then
ssc install parmby
and then
ssc install sencode

To run the example: copy the following into a do file and run

sysuse auto, clear

tabulate rep78, gene(rep78_)

parmby "regress mpg rep78_*, noconst", by(foreign) label norestore

sencode label if parm!="_cons", gene(parmlab)
label var parmlab "Repair record 1978"

label var estimate "Mean mileage (mpg)"
eclplot estimate min95 max95 parmlab, eplot(bar) estopts(barwidth(0.25)) ///
supby(foreign, ///
spaceby(0.25)) xscale(range(0 6)) xlabel(1(1)5, angle(30))





Radar Plot

Requires: radar
to download this program type the following on the Stata command line (if not already loaded):
ssc install radar

To run the example: copy the following into a do file and run

sysuse auto, clear

radar make turn mpg trunk if foreign, title(Nice Radar graph) ///
lc(red blue green) lw(*1 *2 *4) r(0 12 14 18 50)




Batplot
Produces a Bland-Altman plot when there is a relationship between paired differences and their average

Requires: batplot
to download this program type the following on the Stata command line (if not already loaded):
ssc install batplot

To run the example: copy the following into a do file and run

sysuse auto, clear

batplot mpg turn, title(Agreement between mpg and turn) ///
info valabel(make) notrend xlab(26(4)38) moptions(mlabp(9))




Chernoff faces

Requires: chernoff
to download this program type the following on the Stata command line (if not already loaded):
net install gr0038

To run the example: copy the following into a do file and run

sysuse auto, clear
drop if rep78==.

keep in 41/55

chernoff, isize(rep78) hdark(mpg) hslant(mpg) fline(weight) ///
order(foreign price) saving(c:/face1)

chernoff, isize(rep78,0) hdark(mpg) hslant(mpg) fline(weight) ///
nose(price) legend(2 nolabel) cols(3) rhalf saving(c:/face2)

generate s = 1+runiform()

chernoff, isize(rep78,. 6) hdark(mpg) hslant(mpg) fline(weight) ///
bvert(_null_) inote(make) iscale(s) saving(mygraph, replace)





Trellis plot

Requires: trellis
to download this program type the following on the Stata command line (if not already loaded):
ssc install trellis

To run the example: copy the following into a do file and run

webuse nhanes2f, clear

trellis,by(health region) f(graph box copper zinc iron) fopt(legend(off) ///
ylab(50 175 300) yscale(r(50,310))) sr(2) sc(2) ///
singleopt(legend(on ring(0) pos(1) col(1) bm(tiny) ///
symx(*0.2) keyg(*0.2) region(m(zero) lw(none))) ///
yscale(r(50,310))) name(trellis)





grcomb
Combines graphs

Requires: grcomb
to download this program type the following on the Stata command line (if not already loaded):
ssc install grcomb

To run the example: copy the following into a do file and run

webuse nhanes2f, clear

grcomb graph box copper zinc iron , v(1)





pieplot
Makes it easier to draw pie charts of categorical frequencies

Requires: plieplot
to download this program type the following on the Stata command line (if not already loaded):
ssc install pieplot

To run the example: copy the following into a do file and run

sysuse auto, clear

pieplot rep78 foreign, sum plabelsubopts(size(*2)) ///
pie(1, color(red*2)) pie(2, color(red)) ///
pie(3, color(red*0.7)) pie(4, color(red*0.5)) ///
pie(5, color(red*0.3)) legend(row(1)) name(pieplot)





sixplot
Displays six diagnostic and descriptive graphs for a single variable Requires: sixplot to download this program type the following on the Stata command line (if not already loaded): ssc install sixplot To run the example: copy the following into a do file and run sysuse uslifeexp.dta
sixplot le_male
									

zmap

zmap
zmap graphs (or maps) binned values of a variable z with respect to two variables x and y treated as Cartesian coordinates. Requires: zmap to download this program type the following on the Stata command line (if not already loaded): ssc install zmap To run the example: copy the following into a do file and run
webuse nlswork, clear egen mean = mean(ln_wage), by(age grade) egen tag = tag(age grade) label variable mean "mean ln wage" summarize ln_wage if !missing(age, grade), detail zmap mean age grade if tag, breaks(.993 1.166 1.361 1.641 /// 1.964 2.275 2.456) ms(S ..) ysc(on) xsc(on) yla(0/18, ang(h)) /// ytitle(`: var label grade') mxla(15(5)45) note("") /// color(blue blue*0.5 orange*0.5 orange)
						

netplot

netplot
An important part of what makes social network analysis so fascinating to broad audiences is the possibility of visualization of networks. netplot produces a network plot Requires: netplot to download this program type the following on the Stata command line (if not already loaded): ssc install netplot To run the example: copy the following into a do file and run //create a random network with 10 nodes //(code adopted from the helpfile of stata2pajek) clear set obs 200 gen i=int(uniform()*10)+1 gen j=int(uniform()*10)+1 contract i j, freq(strength) drop if i==j sort i j drop if strength <3 // --> keep only the "stronger" links // Draw the network netplot i j, type(circle) label
							

cmogram

cmogram cmogram graphs the means, medians, frequencies, or proportions of yvar, conditional on xvar. Requires: cmogram to download this program type the following on the Stata command line (if not already loaded): ssc install cmogram To run the example: copy the following into a do file and run sysuse auto, clear cmogram mpg weight, histopts(bin(5)) lfit cutpoint(3250) /// lineat(3000 3250 3500) controls(price)
								

ellip

ellip ellip calculates a confidence ellipse from the elliptically distributed variables yvar and xvar, and then graphs the confidence ellipse using graph twoway line. Requires: ellip to download this program type the following on the Stata command line (if not already loaded): ssc install ellip To run the example: copy the following into a do file and run sysuse auto, clear ellip mpg weight, by(foreign, total legend(off)) /// total tlabel(Total as a by-group) plot(scatter mpg weight)
							

drarea

drarea Multiple graphics are produced in Stata by the rule "last drawn first seen". So the objects that are drawn last are the ones that are observed. The implication of this principle is that when overlapping two rarea graphs the overlapping area is hidden by the second rarea graph. This command drarea overlays two range area plots by merging the two colours and hence highlighting the true overlap. Requires: drarea to download this program type the following on the Stata command line (if not already loaded): ssc install drarea To run the example: copy the following into a do file and run sysuse sp500, clear generate high2 = high+15*uniform() generate low2 = low+15*uniform() drarea high low high2 low2 date in 1/20
							

linkplot

linkplot linkplot plots yvarlist versus xvar such that data points are linked (i.e. connected) within groups defined by distinct values of linkvar. For example, with paired data it might be desired to link each pair, or with panel data it might be desired to link observations within each panel. Requires: linkplot to download this program type the following on the Stata command line (if not already loaded): ssc install linkplot To run the example: copy the following into a do file and run clear input /// A B id 13.2 14.0 1 8.2 8.8 2 10.9 11.2 3 14.3 14.2 4 10.7 11.8 5 6.6 6.4 6 9.5 9.8 7 10.8 11.3 8 8.8 9.3 9 13.3 13.6 10 end rename A wearA rename B wearB reshape long wear, string i(id) j(j) encode j, gen(material) linkplot material wear, link(id) yla(1 2, valuelabel) /// ysc(r(0.5 2.5)) yla(, ang(h))
						

tddens

tddens Bivariate kernel density graphs over a grid Requires: tddens to download this program type the following on the Stata command line (if not already loaded): ssc install tddens To run the example: copy the following into a do file and run sysuse auto, clear tddens price mpg, s b
							

Lexis diagram

Lexis diagram Two dimensional diagram that is used to represent events (such as births or deaths) that occur to individuals belonging to different cohorts. Requires: evhistplot to download this program type the following on the Stata command line (if not already loaded): ssc install evhistplot To run the example: copy the following into a do file and run clear webuse set /// "http://oldsite.soziologie-blossfeld.de/eha/stata/do_files/Data/" webuse rrdat1.dta * Convert event times to dates. Are arbitrarily set to fifteenth day of month. foreach var of varlist tstart tb te tmar ti tfin { gen `var'date = mdy(mod(`var' - 1, 12) + 1, 15, (`var' - 1) / 12 + 1900) local labvar : variable label `var' la var `var'date "`labvar'" format %d `var'date } * replace with missing where observation is actually right censored replace tmardate = . if tmar == 0 la var tmardate "Date of marriage" replace tfindate = . if tfin == ti * Lexis plot with date of marriage and job starts labelled * according to rank evhistplot tstartdate tmardate, id(id) start(1jan1940) /// end(31dec1981) birth(tbdate) evtype(noj) nsub(20) xtitle(Calendar time)
						

Deviation plots

Deviation plots Plots each data point relative to some appropriate mean with a marker symbol and a spike connecting marker and mean. Requires: devnplot to download this program type the following on the Stata command line (if not already loaded): ssc install devnplot To run the example: copy the following into a do file and run webuse systolic, clear version 9: anova systolic drug disease drug*disease predict predict predict residual, residual devnplot systolic drug disease, level(predict) superplines
						

Statplot

Statplot Statplot is an alternative to graph hplot, graph hbar and graph dot. It allows grouping on the axis and other options. Requires: statplot to download this program type the following on the Stata command line (if not already loaded): ssc install statplot To run the example: copy the following into a do file and run webuse systolic, clear sysuse census, clear statplot marriage divorce, over(region) s(sum) xpose varnames
						

mm_plot

mm_plot mm_plot is a user written Mata graph command that allow you to plot a Mata matrix. Requires: moremata to download this program type the following on the Stata command line (if not already loaded): ssc install moremata To run the example: copy the following into a do file and run set more off mata: for(i=0;i<=2*pi()*1000; i++) { a=(i/1000)*cos(i/1000) a1=(i/1000)*sin(i/1000) if(i==0) { za=a za1=a1 } else { za=za\a za1=za1\a1 } c=za,za1 } mata: mm_plot(c) end
							

cdfplot

cdfplot cdfplot plots the sample cumulative distribution function. Distributions can be compared within subgroups defined by a second variable. The best fitting normal (Gaussian) model may be superimposed over the sample c.d.f. Requires: cdfplot to download this program type the following on the Stata command line (if not already loaded): ssc install cdfplot To run the example: copy the following into a do file and run sysuse auto,replace cdfplot length [fw=rep78], by(foreign) norm saving(mygraph,replace)
								

riskplot

riskplot The riskplot is a graphical aid to the investigation of the contributions of risk factors on outcomes of interest. (see SJ Vol 10 No 1) Requires: riskplot to download this program type the following on the Stata command line (if not already loaded): ssc install risk,all (The all option ensures that the data (data_riskplot.dta) will also be downloaded) To run the example: copy the following into a do file and run use c:/data/data_riskplot.dta, clear //path may need to be changed set scheme sj riskplot depr1995 sex sclass if Idep91==1 [pw=wg], path obs /* */ thick(20) c(. red) title(Risk plot for subjects with mild /* */ or severe depression at baseline) subtitle((results using /* */ sampling weights), margin(b+5)) scale(0.9) ytitle(depression /* */ score 1995) saving(riskplotWG, replace)
						

aaplot

aaplot aaplot graphs a scatter plot for yvar versus xvar with linear and/or quadratic fit superimposed. The equation(s) and R-square statistics of the fits shown are also shown at the top of the graph. Requires: aaplot to download this program type the following on the Stata command line (if not already loaded): ssc install aaplot To run the example: copy the following into a do file and run sysuse auto, clear gen gpm = 1000 / mpg label var gpm "Gallons per thousand miles" aaplot gpm weight, name(g1)
							

hull plot

hull plot cvxplot makes a scatterplot and draw convex hulls of a group of points in two-dimensional space. Each hull is defined by two lines joining the bottom-left point to the top-right point. Requires: cvxplot to download this program type the following on the Stata command line (if not already loaded): ssc install cvxhull To run the example: copy the following into a do file and run sysuse auto, clear gen foreign1=foreign label define for 1 For 0 Dom label values foreign1 for cvxhull mpg weight, group(foreign) hulls(2) /// scat(mlab(foreign1) mlabpos(c) msym(i) ysc(r(0,60)))
							

Pareto plot

Pareto plot pdplot produces a Pareto dot plot Requires: pdplot to download this program type the following on the Stata command line (if not already loaded): ssc install pdplot To run the example: copy the following into a do file and run sysuse auto, clear pdplot mpg
							

Centred bar plots

Centred bar plots Centred bar plots shows absolute or relative frequencies of yvar as centred bar plots. Centred bar plots are often used in archaeology to show frequencies of different kinds of artefact at different levels or ages, sometimes under the title of battleship curves or battleship diagrams. Requires: cbarplot to download this program type the following on the Stata command line (if not already loaded): ssc install cbarplot To run the example: copy the following into a do file and run clear input levels freqcores freqblanks freqtools 25 21 32 70 24 36 52 115 23 126 650 549 22 159 2342 1633 21 75 487 511 20 176 1090 912 19 132 713 578 18 46 374 266 17 550 6182 1541 16 76 846 349 15 17 182 51 14 4 51 14 13 29 228 130 12 135 2227 729 end reshape long freq, i(levels) j(kind) string cbarplot levels kind [fw=freq], percent(levels) mlabsize(*.6)
							

heatmap

heatmap Graphs a heatmap. Requires: cbarplot to download this program type the following on the Stata command line (if not already loaded): ssc install hmap To run the example: copy the following into a do file and run // producing the data clear all set obs 64 generate n=int(uniform()*10) generate x=1+int((_n-1)/8) generate y=1+mod((_n-1),8) label define xlab 1 "one" 2 "two" 7 "seven" 8 "eight" label define ylab 3 "three" 4 "four" 5 "five" 6 "six" label value x xlab label value y ylab table y x [fw=n] hmap x y n
							

simpplot

simpplot // Plot describing p-values from a simulation by comparing //nominal significance levels with the coverages Requires: simpplot to download this program type the following on the Stata command line (if not already loaded): ssc install simpplot program drop _all program define sim, rclas drop _all set obs 500 gen x = rchi2(2) ttest x=2 in 1/50 return scalar p50 = r(p) ttest x=2 return scalar p500 = r(p) end set seed 12345 simulate p50=r(p50) p500=r(p500), /// reps(5000) : sim label var p50 "N=50" label var p500 "N=500" simpplot p50 p500, main1opt(mcolor(red)) /// main2opt(mcolor(blue))
							

Fractal

Fractal // Generates fractals Requires: fractal to download this program type the following on the Stata command line (if not already loaded): ssc install fractal clear fractal ,hs(0,.2,.3,.40,.5,.618,.75,.85,1.0) /// vs(0,.25,.2,.35,.25,.516,.366,.42,.2) hr(0 100) /// vr(0 200) iter(3) savegraph graph combine _frctl1.gph _frctl2.gph _frctl3.gph
						

Motion Chart

Motion Chart A "Motion Chart" is a scatter plot for time-series data. You can hit "play" and see how the points move over time. They were made famous, for example, by Hans Rosling's internet videos: http://www.gapminder.org/ "Example of motion graph for code below" Requires: motionchart to download this program: Open Stata viewer type the following in: net describe motionchart, from(http://kk-adofiles.googlecode.com/hg) type help motionchart Read the pharagraph on setting the Adobe Flash Player sysuse bplong.dta , clear generate year = 2007+when decode sex , gen(gender) motionchart patient year bp sex using "my_test.html" in 115/125 , /// replace title("Blood Pressure") /// subtitle("Example motionchart: created in Stata with motionchart.ado")
						

binscatter

binscatter binscatter generates binned scatterplots, and is optimized for speed in large datasets. Binned scatterplots provide a non-parametric way of visualizing the relationship between two variables. With a large number of observations, a scatterplot that plots every data point would become too crowded to interpret visually. binscatter groups the x-axis variable into equal-sized bins, computes the mean of the x-axis and y-axis variables within each bin, then creates a scatterplot of these data points. Requires: binscatter to download this program type the following on the Stata command line (if not already loaded): ssc install binscatter clear all sysuse nlsw88, clear keep if inrange(age,35,44) & inrange(race,1,2) scatter wage tenure , title("Graph produced by Scatter") name(g1) // The scatter was too crowded to be easily interpetable. The binscatter is cleaner, try a quadratic fit. binscatter wage tenure, line(qfit) xscale(range(0,25)) /// xlabel(0(5)25) ylabel(0(10)40) yscale(range(0,40)) /// title("Graph produced by Binscatter") name(g2) graph combine g1 g2
							

missingplot

missingplot // Generates a graph of missing values Requires: missingplot to download this program type the following on the Stata command line (if not already loaded): ssc install missingplot webuse nlsw88, clear missingplot, var labels mlabcolor(blue ..)
								

sparkline

sparkline // sparkline graphs sparkline-type plots for one or more y // variables against a single x variable. Typically, plots for // different y variables or for different subsets of one y variable // are stacked vertically into one image. Requires: sparkline to download this program type the following on the Stata command line (if not already loaded): ssc install sparkline webuse grunfeld, clear sparkline invest year, by(company) extremes
						

Program that helps with specification of regular linear and logarithmic

Program that helps with specification of regular linear and logarithmic axis scales, ranges and tick lists Requires: regaxis to download this program type the following on the Stata command line (if not already loaded): ssc install regaxis To run the example: copy the following into a do file and run sysuse auto, clear regaxis rep78, cycle(.5) singleok margin(0.5) lrange(yrange) lticks(ylabs) regaxis weight, include(0) lticks(xlabs) scatter rep78 weight, yscale(range(`yrange')) ylabel(`ylabs') xlabel(`xlabs')
						

Program that helps with specification of label positions

Program that helps with specification of label positions Requires: egenmore to download this program type the following on the Stata command line (if not already loaded): ssc egenmore To run the example: copy the following into a do file and run clear all //graph without the egenmore generated label position sysuse auto, clear egen clock = mlabvpos(mpg weight) scatter mpg weight, mlab(make) mlabvpos(clock) name(a1) /// scheme(s1) // look at a suitable matrix // for the egen mlabvpos option matrix z= 11, 1, 12, 11, 1 \ 10, 2, 12, 10, 2 \ 9, 3, 12, 9 ,3 \ /// 8, 4, 6, 8, 4 \ 7, 5 ,6, 7, 5 matrix list z //graph with the egenmore generated label position egen clock2 = mlabvpos(mpg weight), matrix(11 1 12 11 1 \\ /// 10 2 12 10 2 \\ 9 3 12 9 3 \\ 8 4 6 8 4 \\ 7 5 6 7 5) scatter mpg weight, mlab(make) mlabvpos(clock2) name(a2) /// title("Graph using egenmore mlabvpos generated data") /// scheme(s1)