Stata Graphs - Examples
Stata Graphs - Examples
Home
Products
Overview of Stata Why buy Stata? Stata Journal Stat/Transfer
Stata Graphs
Below are some Stata graphs. These are presented as they have used Stata in some interesting ways. From time to time more graphs will be added to this page. The graph below have generally be taken from the Statalist and in some cases To see the original Statalist entry do a Google search on a line of code. Also see : Offical Stata Graphs - UCLA Site. Official Stata Graphs - Stata Also, see: Stata User written Graphs.
Prices
Australia
New purchases Upgrade Bookshop GradPlan
New Zealand
New purchases Upgrade Bookshop GradPlan
Support
Starting Stata Stata tips - General Stata tips - Graphs Stata tips - Tables Technical Stata Courses & Training
s y s u s en l s w 8 8 ,c l e a r
g e n e r a t eg = m a x ( g r a d e , 8 ) l a b e l v a rg" Y e a r so fe d u c a t i o n " r e n a m e m a r r i e dm c o l l a p s ew a g e ,b y ( gm ) g e n e r a t e w = r o u n d ( w a g e , . 0 1 ) s u m m a r i z ew a g e ,m e a n o n l y g e n e r a t e c = r o u n d ( ( w a g e r ( m i n ) ) / ( r ( m a x ) r ( m i n ) q u i e tl e v e l s o fc ,l o c ( c s ) l o c a lg f o r e a c hco fl o cc s{ l o c a lc 1 = 1 2 0 + r o u n d ( ` c ' / 2 ) l o c a lc 2 = 2 5 5 r o u n d ( ` c ' / 2 ) l o c a lmm c ( " ` c 2 '` c 2 '` c 1 ' " ) l o c a lg` g ' | | s cmgi fc = = ` c ' ,m s ( S )m s i z e ( }
l o c a lg` g ' | | s cmg ,m s ( i )m l a b ( w )m l a b p ( 0 ) l o c a lg` g 'l e g ( o f f )y l a ( . 7 5""0" N "1" Y " l o c a lg` g 'x l a ( 7""8 / 1 81 9"" ,n o t i c k )
t w o w a y` g ' s c h e m e ( s 1 m o n o )t i ( W a g eh e a tm a p )x
www.survey-design.com.au/Stata Graphs.html
1/42
12/28/12
l a b e lv a rl o w " L o wv a l u e " l a b e lv a rh i g h " H i g hv a l u e " l a b e lv a rm e a n " M e a n " l a b e ld e f i n ei n p u t l b l 1" A n n u a lD o n a t i o n si nU . S . " 2" S e r o p r e v a l e n c e " 3" E x p o s u r e sp e rD o n a t i o n " 4" P r o b a b i l i t yo fT r a n s m i s s i o n " 5" S e n s i t i v i t yo fT e s t " l a b e lv a li n p u ti n p u t l b l g e ns e n s=h i g h l o w s o r ts e n s l o c a lm l 1=( 2 . 2 + 1 . 9 ) / 2 l o c a lm h 1=( 1 . 6 + 1 . 9 ) / 2 l o c a lm l 2=( 2 . 3 + 1 . 9 ) / 2 l o c a lm h 2=( 1 . 5 + 1 . 9 ) / 2 l o c a lm l 3=( 2 . 9 + 1 . 9 ) / 2 l o c a lm h 3=( 1 . 0 + 1 . 9 ) / 2 l o c a lm l 4=( 3 . 2 + 1 . 9 ) / 2 l o c a lm h 4=( 0 . 5 + 1 . 9 ) / 2 l o c a lm l 5=( 2 . 9 + 1 . 9 ) / 2 l o c a lm h 5=( 0 + 1 . 9 ) / 2 / / / / / / / / / / / / / / /
l o c a lf o r m a tf c o l o r ( w h i t e )l c o l o r ( w h i t e )
t w o w a y ( r b a rl o wm e a ni n p u t ,h o r i z o n t a l ) ( r b a rm e a nh i g hi n p u t ,h o r i z o n t a l ) , y l a b e l ( ,v a la n g l e ( h o r i z o n t a l ) )x s i z e ( 7 . 1 )y s i z x t i t l e ( """ P r e d i c t e dC a s e so fT r a n s f u s i o n T r a n t i t l e ( " S e n s i t i v i t yo fR e s u l t st oI n p u t s " )s u b t i l e g e n d ( l a b e l ( 1" L o wI n p u tV a l u e s " )l a b e l ( 2" H i g t e x t ( 5` m l 5 '" 9 3 . 5 % " ,b o xj ( c e n t e r ) m a r g i n ( l + 1 t e x t ( 5` m h 5 '" 9 9 . 9 9 % " ,b o xj ( c e n t e r )m a r g i n ( l + 1 t e x t ( 4` m l 4 '" 5 % " ,b o xj ( c e n t e r ) m a r g i n ( l + 1 t e x t ( 4` m h 4 '" 0 . 8 % " ,b o xj ( c e n t e r ) m a r g i n ( l + 1 t e x t ( 3` m l 3 '" 3 " ,b o xj ( c e n t e r ) m a r g i n ( l + 1 t e x t ( 3` m h 3 '" 1 " ,b o xj ( c e n t e r ) m a r g i n ( l + 1 t e x t ( 21 . 4" 1 : 2 8 , 6 0 0 " ,b o x m a r g i n ( l + 1 p l a c e m e n t ( 9 )j ( r i g h t ) ) t e x t ( 22 . 4" 1 : 1 8 , 2 0 0 " ,b o x m a r g i n ( l + 1 p l a c e m e n t ( 3 )j ( l e f t ) ) t e x t ( 11 . 5" 1 3 . 4m i l l i o n " ,b o x m a r g i n ( l + 1 p l a c e m e n t ( 9 )j ( r i g h t ) ) t e x t ( 12 . 4" 1 9m i l l i o n " ,b o x m a r g i n ( l + 1 p l a c e m e n t ( 3 )j ( l e f t ) )
/ /C r e a t i n ga x i s
s y s u s ea u t o ,c l e a r g e ny = ( p r i c e 5 0 0 0 ) / 1 0 0 0 g e nx = m p g 2 0
l o c a lx` " | | s c a t t e r i01 0" 1 0 "01 0" 1 0 "02 l o c a lx` " ` x 'm l a b p o s ( 6 )m l a b s i z e ( * 1 . 2 5 ) " '
l o c a lx` " ` x 'm s y m b o l ( n o n e ) m l a b c o l o r ( b l a c k ) " l o c a lx` " ` x ' | | s c a t t e r i01 0" | "01 0" | "02 l o c a lx` " ` x 'm l a b p o s ( 0 )m s y m b o l ( n o n e ) m l a b c o l o c a ly` " | | s c a t t e r i30" 3 "30" 3 "60" 6 " l o c a ly` " ` y 'm l a b p o s ( 9 )m l a b s i z e ( * 1 . 2 5 ) " ' l o c a ly` " ` y 'm s y m b o l ( n o n e ) m l a b c o l o r ( b l a c k ) " l o c a ly` " ` y ' | | s c a t t e r i30" _ "30" _ "60" l o c a ly` " ` y 'm l a b p o s ( 0 )m s y m b o l ( n o n e ) m l a b c o l o c a la x" y l i ( 0 , l c ( b l a c k )l w ( t h i n ) )x l i ( 0 , l c ( b l o c a la x" ` a x 'y s c ( o f f )x s c ( o f f )l e g ( o f f ) "
www.survey-design.com.au/Stata Graphs.html
2/42
12/28/12
/ /C r e a t i n ga x i si nt h em i d d l eo fag r a p h
s y s u s ea u t o ,c l e a r
s c a t t e rp r i c ew e i g h ti fw e i g h t < 3 0 0 0 , g r a p h r e g x s c a l e ( r a n g e ( 2 0 0 03 0 1 0 ) )p l o t r e g i o n ( m a r g i n ( 0 ) ) n a m e ( f i r s t ,r e p l a c e ) y s c a l e ( o f f ) n o d r a w
s c a t t e rp r i c ew e i g h ti fw e i g h t > 3 0 0 0 , g r a p h r x s c a l e (r a n g e ( 3 0 1 05 0 0 0 ) ) p l o t r e g i o n ( m a r g i n ( 0 n a m e ( s e c o n d ,r e p l a c e )x t i t l e ( , c o l o r ( b g )) n g r a p hc o m b i n e f i r s ts e c o n d
/ /C o n t r o l l i n gt h es i d eo ft h eg r a p ht h a tt h e
s y s u s ea u t o ,c l e a r
t w o w a y/ / / ( h i s t o g r a mm p g ,w i d t h ( 5 )y s c a l e ( a l t a x i s ( 1 ) ) ( l i n e w e i g h tm p g ,y a x i s ( 2 )y s c a l e ( a l ta x i s ( 2 )
www.survey-design.com.au/Stata Graphs.html
3/42
12/28/12
/ /G r i do nt o po fg r a p h
s s ci n s t a l lf r e d u s e ,r e p l a c e / / g e t sp r o g r a mo f r e d u s eM P R I M E ,c l e a r g e n e r a t ey m=m o f d ( d a t e n )
t s s e ty m ,m o n t h l y / / s e t sf o rt i m e t w o w a y f u n c t i o ny = 2 0 . 7 0 5 , r a n g e ( 1 1 91 3 0 )r e c a s t ( a r e a ) f u n c t i o ny = 2 0 . 7 0 5 , r a n g e ( 1 6 61 8 2 )r e c a s t ( a r e a ) f u n c t i o ny = 2 0 . 7 0 5 , r a n g e ( 2 4 02 7 4 )r e c a s t ( a r e a ) f u n c t i o ny =5 , r a n g e ( 1 1 92 7 4 )l s t y l e ( g r i d ) f u n c t i o ny = 1 0 , r a n g e ( 1 1 92 7 4 )l s t y l e ( g r i d ) f u n c t i o ny = 1 5 , r a n g e ( 1 1 92 7 4 )l s t y l e ( g r i d ) f u n c t i o ny = 2 0 , r a n g e ( 1 1 92 7 4 )l s t y l e ( g r i d ) f u n c t i o ny = y m ( 1 9 8 0 , 1 1 ) ,r a n g e ( 4 . 7 0 2 52 0 . 7 0 5 )h t s l i n eM P R I M Ei ft i n ( 1 9 7 0 m 1 , 1 9 9 0 m 1 ) ,x l a b e l ( , f l e g e n d ( o r d e r ( 51" R e c e s s i o n " ) )t l a b e l ( , g r i d )s
* ( M a a r t e nB u i s-S t a t al i s t , 3 0O c t o b e r2 0 1 1 )
/ /S h a d eP a r to faG r a p h s y s u s ea u t o ,c l e a r s o r tm p g g e n e r a t ex = _ n g e nm a x = c e i l ( _ N / 1 0 ) * 1 0 s u m m a r i z em p g ,d e t a i l
/ /G r a p h-S l i d e
t w o w a y ( s c a t t e r i60" K i n d so fg r a p h sS t a t ac a nm a k e " , ( s c a t t e r i50" S c h e m e sf o rc o n t r o l l i n gl o o ko f p s t y l e ( p 1 ) ) ( s c a t t e r i40" S t r u c t u r eo fg r a p hc o m m a n d s " ,m l ( s c a t t e r i30" g r a p ho p t i o n s " ,m l a b s i z e ( h u g e )p ( s c a t t e r i20" B u i l d i n gc o m p l e xg r a p h s " ,m l a b s i ( s c a t t e r i10" A d d i t i o n a le x a m p l e s " ,m l a b s i z e ( h t i t l e ( " O v e r v i e w " ) x l a b e l ( . 0 51 )y l a b e l ( 17 ) x s c a l e ( o f f )y s c a l e ( o f f ) l e g e n d ( o f f ) n o t e ( " S t a t ag r a p h i c sa r ef u na n de a s y "" S t a t a r i n g ( 0 ) )
www.survey-design.com.au/Stata Graphs.html
4/42
12/28/12
/ /G r a p h-S p e c i a lT e x t
t w o w a y ( s c a t t e r i6 . 50" O t h e rS y m b o l sI n c l u d e" , ( s c a t t e r i6 . 50 . 5" { & t r a d e }{ r i g h t }{ & l o z } " ,m l a b ( s c a t t e r i5 . 50" B o l df o n t " , ( s c a t t e r i5 . 50 . 5" { b f :B o l df o n t } " , ( s c a t t e r i4 . 50" { i t :I t a l i c s } " , ( s c a t t e r i4 . 50 . 5" { i t :I t a l i c s } " , ( s c a t t e r i3 . 50" S u p e r s c r i p t " , ( s c a t t e r i3 . 50 . 5" { s u p e r s c r i p t :S u p e r s c r i p t } " , ( s c a t t e r i2 . 50" S u b s c r i p t " , ( s c a t t e r i2 . 50 . 5" { s u b s c r i p t :S u b s c r i p t } " , ( s c a t t e r i1 . 50" D e f a u l tS y m b o lf o n t " , ( s c a t t e r i1 . 50 . 5"{ s t S y m b o l : a d c v } " , ( s c a t t e r i0 . 50" G r e e kS y m b o l s : " , ( s c a t t e r i0 . 50 . 5" { & A l p h a }{ & P i }{ & S i g m a } " , t i t l e ( " S p e c i a lF o n tt h a tc a nb eu s e do nS t a t aG r a p x l a b e l ( . 0 51 )y l a b e l ( 17 ) x s c a l e ( o f f )y s c a l e ( o f f ) l e g e n d ( o f f ) n o t e ( " S t a t as p e c i a lf o n t . " " T h e r ea r em a n yo t h e ro p t i o n ss e e{ b f : h e l pg r a p ht r i n g ( 0 ) )
/ /s t a c k e db a rg r a p h
s y s u s ea u t o ,c l e a r t w o w a y _ _ h i s t o g r a m _ g e nm p gi ff o r e i g n ,g e n ( f r e q _ f o r f r e qs t a r t ( 0 )w i d t h ( 2 ) d r o pi fm i s s i n g ( x ) k e e pf r e q _ f o r e i g nx s a v ea 1 ,r e p l a c e
s y s u s ea u t o ,c l e a r t w o w a y _ _ h i s t o g r a m _ g e nm p gi f! f o r e i g n ,g e n ( f r e q _ d o f r e qs t a r t ( 0 )w i d t h ( 2 ) d r o pi fm i s s i n g ( x ) k e e pf r e q _ d o m e s t i cxm p g m e r g e1 : 1 xu s i n ga 1
m v e n c o d ef r e q _ d o m e s t i c f r e q _ f o r e i g n,m v ( . = 0 )
e g e nf r e q=r o w t o t a l ( f r e q _ d o m e s t i c f r e q _ f o r e i g n)
t w o w a y ( b a rf r e q _ d o m e s t i cxi ff r e q<. ,b a r w ( 2 )b c o l o r ( b r b a rf r e q _ d o m e s t i cf r e qxi ff r e q<. ,b a r w ( 2 )b c o l e g e n d ( o r d e r ( 1" D o m e s t i c "2" F o r e i g n " )p o s ( 1 1 )c o l x t i t l e ( " ` :v a rl a b e lm p g ' " )y t i t l e ( F r e q u e n c y )x l a ( x s c ( r ( 1 0. ) )
/ /b o xp l o t s y s u s ea u t o ,c l e a r
www.survey-design.com.au/Stata Graphs.html
5/42
12/28/12
/ /u s el i n e -o rc o n n e c t e d -n o ts c a t t e r t w o w a ys c a t t e rm e a nr e p 7 8| | r b a rm e a nm a xr e p 7 8 ,b c o l o r ( n o n e )b a r w ( 0 . 2 )| | r b a rm e a nm i nr e p 7 8 ,b c o l o r ( n o n e )b a r w ( 0 . 2 )| | r b a rm e a n m s dm e a n p s dr e p 7 8,b a r w ( 0 . 1 )b c o l o r ( l e g e n d ( o f f )y t i t l e ( M i l e sp e rg a l l o n )y l a ( ,a n g n o t e ( r e db a r s :m e a n+ / -s d ;e m p t yb a r ss h o wr a
/ /C a n d l eS t i c kp l o t c l e a r i n p u t/ / / s t r 3s h a r e a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c e n d l
s t r 2 0 t i m e p r i c e " 1 / 5 / 2 0 1 1 : 1 0 : 3 4 "1 0 . 3 4 " 1 / 5 / 2 0 1 1 : 1 0 : 3 5 "1 0 . 2 5 " 1 / 5 / 2 0 1 1 : 1 0 : 3 6 "1 0 . 1 5 " 1 / 5 / 2 0 1 1 : 1 0 : 3 7 "1 0 . 7 3 " 1 / 5 / 2 0 1 1 : 1 0 : 3 9 "1 0 . 9 5 " 1 / 5 / 2 0 1 1 : 1 0 : 4 1 "1 0 . 5 3 " 1 / 5 / 2 0 1 1 : 1 0 : 4 5 "1 0 . 9 9 " 1 / 5 / 2 0 1 1 : 1 0 : 4 9 "1 1 . 0 5 " 2 / 5 / 2 0 1 1 : 1 0 : 3 4 "9 . 3 4 " 2 / 5 / 2 0 1 1 : 1 0 : 3 5 "9 . 2 5 " 2 / 5 / 2 0 1 1 : 1 0 : 3 6 "9 . 1 5 " 2 / 5 / 2 0 1 1 : 1 0 : 3 7 "9 . 7 3 " 2 / 5 / 2 0 1 1 : 1 0 : 3 9 "9 . 9 5 " 2 / 5 / 2 0 1 1 : 1 0 : 4 1 "9 . 5 3 " 2 / 5 / 2 0 1 1 : 1 0 : 4 5 "9 . 9 9 " 2 / 5 / 2 0 1 1 : 1 0 : 4 9 "9 . 0 5 " 3 / 5 / 2 0 1 1 : 1 0 : 3 4 "8 . 3 4 " 3 / 5 / 2 0 1 1 : 1 0 : 3 5 "8 . 2 5 " 3 / 5 / 2 0 1 1 : 1 0 : 3 6 "8 . 1 5 " 3 / 5 / 2 0 1 1 : 1 0 : 3 7 "8 . 7 3 " 3 / 5 / 2 0 1 1 : 1 0 : 3 9 "8 . 9 5 " 3 / 5 / 2 0 1 1 : 1 0 : 4 1 "8 . 5 3 " 3 / 5 / 2 0 1 1 : 1 0 : 4 5 "8 . 9 9 " 3 / 5 / 2 0 1 1 : 1 0 : 4 9 "1 2 . 0 5 " 4 / 5 / 2 0 1 1 : 1 0 : 3 4 "1 2 . 3 4 " 4 / 5 / 2 0 1 1 : 1 0 : 3 5 "1 2 . 2 5 " 4 / 5 / 2 0 1 1 : 1 0 : 3 6 "1 2 . 1 5 " 4 / 5 / 2 0 1 1 : 1 0 : 3 7 "1 2 . 7 3 " 4 / 5 / 2 0 1 1 : 1 0 : 3 9 "1 2 . 9 5 " 4 / 5 / 2 0 1 1 : 1 0 : 4 1 "1 2 . 5 3 " 4 / 5 / 2 0 1 1 : 1 0 : 4 5 "1 2 . 9 9 " 4 / 5 / 2 0 1 1 : 1 0 : 4 9 "1 7 . 0 5
t w o w a y( r b a rp 7 5p 2 5d a t ei fc o l = = 0 ,b a r w i d t h ( ( r b a rp 7 5p 2 5d a t ei fc o l = = 1 ,b a r w i d t h ( . 6 )c o l ( r b a rp 7 5p 2 5d a t ei fc o l = = 2 ,b a r w i d t h ( . 6 )c o l ( r s p i k eu a vl a vd a t ei fc o l = = 2 ,l w i d t h ( t h i c k ) ( r s p i k eu a vl a vd a t e i fc o l = = 1,l w i d t h ( t h i c k ( r s p i k eu a vl a vd a t ei fc o l = = 0,l w i d t h ( t h i c k ) l e g e n d ( o f f )
www.survey-design.com.au/Stata Graphs.html
6/42
12/28/12
/ /E x t r a p o l a t e s y s u s ea u t o ,c l e a r r e g r e s sm p gw e i g h t
g e nx =w e i g h t s o r tx t w o w a y ( l f i tm p gw e i g h t ,l w i d t h ( v t h i c k )c o l o r ( f u n c t i o n y=_ b [ w e i g h t ] * x + _ b [ _ c o n s ] ,r a n g e ( 0 ( s c a t t e rm p gw e i g h t )
/ / A l t e r n a t i v e l y s y s u s ea u t o ,c l e a r r e g r e s sm p gw e i g h t s e to b s` = _ N + 2 ' r e p l a c ew e i g h t = 0i nl r e p l a c ew e i g h t = 8 0 0 0i n2 p r e d i c ta t w o w a y( s c a t t e rm p gw e i g h ti n1 / 2 ) / / / ( l i n eaw e i g h t )
/ /A r e aP l o t / /p r e p a r et h ee x a m p l ed a t a s y s u s en l s w 8 8 ,c l e a r
g e ni n d _ g r=i n d u s t r y r e c o d ei n d _ g r1 / 5 = 16 = 27 = 38 / 1 0 = 41 1 = 51 2 = 6 l a b e ld e f i n ei n d _ g r1" m a n u a l " 2" t r a d e " 3" f i n a n c e " 4" o t h e rs e r v i c e s " 5" p r o f e s s i o n a ls e r v i c e s " 6" p u b l i ca d m i n i s t r a t i o n " l a b e lv a l u ei n d _ g ri n d _ g r
/ /c o m p u t ep e r c e n t i l er a n k s e g e nn=c o u n t ( w a g e ) e g e ni=r a n k ( w a g e ) g e nh a z e n=( i-0 . 5 )/n*1 0 0 l a b e lv a r i a b l eh a z e n" p e r c e n t i l er a n ko fi n c o m / /s m o o t ht h ep r o p o r t i o n s m k s p l i n es _ w = h a z e n ,c u b i cn k n o t s ( 5 ) m l o g i ti n d _ g rs _ w * p r e d i c tp r * / /c r e a t et h eg r a p h g e nz e r o=0 g e no n e=1 0 0 g e nl 1=( p r 1 ) * 1 0 0 g e nl 2=( p r 1+p r 2 ) * 1 0 0 g e nl 3=( p r 1+p r 2+p r 3 ) * 1 0 0 g e nl 4=( p r 1+p r 2+p r 3+p r 4 ) * 1 0 0 g e nl 5=( p r 1+p r 2+p r 3+p r 4+p r 5 ) * 1 0 0 s o r th a z e n
www.survey-design.com.au/Stata Graphs.html
7/42
/ /c o l l e c tt h el a b e l sf o rt h es e c o n dy a x i s l o c a lm i d=l 1 [ _ N ] / 2 l o c a ly a x i s` " ` m i d '" m a n u a l " " ' l o c a lm i d=( l 2 [ _ N ] l 1 [ _ N ] ) / 2+l 1 [ _ N ] l o c a ly a x i s` " ` y a x i s '` m i d '" t r a d e " " ' l o c a lm i d=( l 3 [ _ N ] l 2 [ _ N ] ) / 2+l 2 [ _ N ] l o c a ly a x i s` " ` y a x i s '` m i d '" f i n a n c e " " '
l o c a lm i d=( 1 0 0 l 5 [ _ N ] ) / 2+l 5 [ _ N ] l o c a ly a x i s` " ` y a x i s '` m i d '" p u b l i ca d m i n i s t r a t w o w a yr a r e az e r ol 1h a z e n ,y a x i s ( 1 )| | / / / r a r e al 1l 2h a z e n ,y a x i s ( 2 ) | | / / / r a r e al 2l 3h a z e n | | / / / r a r e al 3l 4h a z e n | | / / / r a r e al 4l 5h a z e n | | / / / r a r e al 5o n eh a z e n , / / / y t i t l e ( " p e r c e n t a g e " ) / / / y l a b ( ` y a x i s ' ,a x i s ( 2 )a n g l e ( 4 5 ) )/ / / y s c a l e ( r a n g e ( 01 0 0 )a x i s ( 1 ) ) / / / y s c a l e ( r a n g e ( 01 0 0 )a x i s ( 2 ) ) / / / y t i t l e ( " " ,a x i s ( 2 ) ) / / / p l o t r e g i o n ( m a r g i n ( z e r o ) ) / / / a s p e c t ( 1 ) / / / l e g e n d ( o f f )
s c a t t e rw e i g h tp r i c e| |s c a t t e r i00` m a x '` m a
/ /F o u r i e rp l o t c l e a r s e to b s8 g e nx = ( _ n 1 ) * ( 2 * _ p i ) / 8 g e ny = s i n ( x ) m a t a :h = s t _ d a t a ( . , " y " ) m a t a :f f t ( h )
12/28/12
/ /h i s t o g r a m s y s u s ea u t o ,c l e a r
/ /D i s p l a y i n gp a r to fah i s t o g r a m s y s u s ea u t o ,c l e a r
t w o w a y _ _ h i s t o g r a m _ g e nm p g,g e n ( hy ,r e p l a c e )f r e q
t w o w a y/ / / ( h i s t o g r a my[ f w = h ]i fi n r a n g e ( y , 1 9 , 3 5 ) , d i s c r e t ec ( h i s t o g r a my[ f w = h ] , c o l o r ( n o n e )l c o l o r ( r e d )d i s c r e
t w o w a y/ / / ( h i s t o g r a my[ f w = h ]i fi n r a n g e ( y , 1 9 , 3 5 ) , d i s c r e t ec n a m e ( a 2 ,r e p l a c e ) ) g r a p hc o m b i n ea 1a 2, x c o m m o nc o l s ( 1 )
/ /h i s t o g r a m-a d dxb a rl a b e l sf o rt h ec e n t r e s y s u s ea u t o ,c l e a r
t w o w a y _ _ h i s t o g r a m _ g e nm p g ,b i n ( 6 )g e n e r a t e ( h c o u n ti f! m i s s i n g ( x ) f o r v a l u e si = 1 / ` = r ( N ) '{
www.survey-design.com.au/Stata Graphs.html
9/42
12/28/12
/ /I n c l u d i n gad i s t r i b u t i o no nt o po fah i s t o g
/ / T h i si se a s i l yd o n ew i t hS t a t a ' s :g r a p hh i s t / / a n dt h en o r m a lo p t i o n s .H o w e v e r ,t h i se x a m p l / / s h o wt h i sw i t ht h et w o w a yh i s t o g r a m . s y s u s ea u t o ,c l e a r s u m m a r i z em p g
l o c a lw i d t h = 4 / / < c h a n g ew i d t ha sr e q u i r e
/ /I n c l u d i n gad i s t r i b u t i o no nt o po fah i s t o g
/ / T h en o r m a lo p t i o nf o rg r a p hh i s t o g r a mu s e st / / a n ds t a n d a r dd e v i a t i o n . / / T h i sm e t h o da l l o w sy o ut os p e c i f yt h e s e .
s y s u s ea u t o ,c l e a r h i s t o g r a mm p g,a d d p l o t ( f u n c t i o nn o r m a l d e n ( x , 2
* ( N i c kC o x-S t a t al i s t , F r i ,0 3J u n e2 0 1 1 )
/ /d i s t r i b u t i o np l o t c l e a r s y s u s ea u t o v e r s i o n9 . 2 k e e pi ff o r e i g n s o r tw e i g h t g e nw e i g h t 2=w e i g h t ^ 2 r e g r e s sm p gw e i g h tw e i g h t 2 p r e d i c tf i t p r e d i c ts e,s t d p
# d e l i m i t; t w o w a ys cm p gw e i g h t,p s t y l e ( p 3 )m s ( o )| | f nw e i g h t [ 3 ] -1 0 0 0*n o r m d e n ( x ,` = f i t [ 3 ] ', r a n g e ( ` = f i t [ 3 ]5 '` = f i t [ 3 ]+ 5 ' )h o r i zp s t y l e ( f n` = f i t [ 3 ] ',r a n g e ( ` = w e i g h t [ 3 ] '` = w e i g h t [ 3 ] p s t y l e ( p 1 )| | f nw e i g h t [ 1 7 ]-1 0 0 0*n o r m d e n ( x ,` = f i t [ 1 7 ] ' , r a n g e ( ` = f i t [ 1 7 ] 5 '` = f i t [ 1 7 ] + 5 ' )h o r i zp s t y l e ( f n` = f i t [ 1 7 ] ' ,r a n g e ( ` = w e i g h t [ 1 7 ] '` = w e i g h t [ 1 7 p s t y l e ( p 1 )| | f nw e i g h t [ 2 1 ]-1 0 0 0*n o r m d e n ( x ,` = f i t [ 2 1 ] ', r a n g e ( ` = f i t [ 2 1 ]7 '` = f i t [ 2 1 ]+ 7 ' )h o r i zp s t y l f n` = f i t [ 2 1 ] ' ,r a n g e ( ` = w e i g h t [ 2 1 ] '` = w e i g h t [ 2 1 p s t y l e ( p 1 )| | l i n ef i tw e i g h t , c l w i d t h ( * 2 )l e g e n d ( o f f )y t i t l e ( M i l e sp e rg a l l
www.survey-design.com.au/Stata Graphs.html
10/42
12/28/12
/ /G r a p hT i t l e-I n c l u d e sy e a r s c l e a r i n ps t r 1 0d a t er i c e p r " 0 1 / 0 1 / 2 0 0 6 " 7 0 0 " 0 1 / 0 2 / 2 0 0 6 " 7 0 0 " 0 1 / 0 3 / 2 0 0 6 " 9 0 0 " 0 1 / 0 4 / 2 0 0 6 " 9 0 0 " 0 1 / 0 5 / 2 0 0 6 " 9 0 0 " 0 1 / 0 6 / 2 0 0 6 " 9 0 0 " 0 1 / 0 7 / 2 0 0 6 " 9 0 0 " 0 1 / 0 8 / 2 0 0 6 " 9 3 3 . 3 3 " 0 1 / 0 9 / 2 0 0 6 " 1 0 0 0 " 0 1 / 1 0 / 2 0 0 6 " 1 0 0 0 " 0 1 / 1 1 / 2 0 0 6 " 1 0 0 0 " 0 1 / 1 2 / 2 0 0 6 " 1 0 0 0 " 0 1 / 0 1 / 2 0 0 7 " 1 0 5 0 " 0 1 / 0 2 / 2 0 0 7 " 1 0 8 7 . 5 " 0 1 / 0 3 / 2 0 0 7 " 1 1 0 0 " 0 1 / 0 4 / 2 0 0 7 " 1 1 0 0 " 0 1 / 0 5 / 2 0 0 7 " 1 1 0 0 " 0 1 / 0 6 / 2 0 0 7 " 1 1 0 0 " 0 1 / 0 7 / 2 0 0 7 " 1 1 7 5 " 0 1 / 0 8 / 2 0 0 7 " 1 2 0 0 " 0 1 / 0 9 / 2 0 0 7 " 1 2 0 0 " 0 1 / 1 0 / 2 0 0 7 " 1 3 0 0 " 0 1 / 1 1 / 2 0 0 7 " 1 4 0 0 " 0 1 / 1 2 / 2 0 0 7 " 1 4 0 0 e n d g e ni n ty e a r= y o f d ( d a t e ( d a t e ," D M Y " ) ) g e nb y t ea u x = _ n
/ / f r o mh t t p : / / w w w . s t a t a j o u r n a l . c o m / s j p d f . h t m l / / f o l l o ws p e c i a ls e q u e n c e s / / g e n e r a t el a b e lf o rm o n t h s t o k e n` c ( M o n s ) ' f o r vi=1 / 2 4 {
i f` i ' = = 1l ad e fm o n t h` i '" ` = y e a r [ ` i ' i fi n r a n g e ( ` i ' , 2 , 1 2 )l ad e fm o n t h` i ' i f` i ' = = 1 3l ad e fm o n t h` i '" ` = y e a r [ ` i i f` i ' > 1 3l ad e fm o n t h` i '" ` ` = ` i ' 1 2 ' } l a b e lv a l u ea u xm o n t h
t w o w a y( l i n er i c e p ra u x ) ,x l a b e l ( # 2 4 ,l a b s i z e ( a n g l e ( f o r t y _ f i v e )v a l u e l )x t i t l e ( " " )
* ( M a r t i nW e i s s-S t a t al i s t , S u n ,0 2M a y2 0 1 0 )
/ /G r a p hT i t l e-I n c l u d e sy e a r s c l e a r i n ps t r 1 0d a t er i c e p r " 0 1 / 0 1 / 2 0 0 6 " 7 0 0 " 0 1 / 0 2 / 2 0 0 6 " 7 0 0 " 0 1 / 0 3 / 2 0 0 6 " 9 0 0 " 0 1 / 0 4 / 2 0 0 6 " 9 0 0 " 0 1 / 0 5 / 2 0 0 6 " 9 0 0 " 0 1 / 0 6 / 2 0 0 6 " 9 0 0 " 0 1 / 0 7 / 2 0 0 6 " 9 0 0
www.survey-design.com.au/Stata Graphs.html
11/42
12/28/12
r e p l a c em o n t h l a b e l = m o n t h l a b e l +""+s t r i n g ( y e a r ( d i fm o n t h ( d a t e 2 ) = = 1
l a b m a s kd a t e 2 ,v a l u e s ( m o n t h l a b e l )/ / u s e rw r i t t e np l e v e l s o fd a t e 2 ,l o c a l ( l e v e l s ) l i n er i c e p rd a t e 2 ,x l a ( ` l e v e l s ' ,v a l u e l a b e la n g l e (
* ( N i c kC o x-S t a t al i s t , T u e ,0 4M a y2 0 1 0 )
/ /C o n t r o l l i n gd a t el a b e l s c l e a r i n p u ts t r 4 0t i m e " 5 j a n 2 0 1 11 5 : 0 0 : 3 0 " " 1 f e b 2 0 1 11 7 : 3 0 : 3 2 " " 1 m a r c h 2 0 1 11 8 : 1 0 : 3 0 " " 1 a p r i l 2 0 1 10 5 : 0 5 : 0 1 " " 1 m a r c h 2 0 1 11 5 : 1 0 : 3 0 " e n d l i s t x 1 2 6 2 1 2
g e n e r a t ed o u b l et i m e 1 = c l o c k ( t i m e , " D M Yh m s " ) l o c a lt 1 = t c ( 1 j a n 2 0 1 11 2 : 0 0 : 0 ) l o c a lt 2 = 1 0 0 0 * 6 0 * 6 0 * 2 4 * 1 0 l o c a lt 3 = t c ( 1 m a y 2 0 1 11 2 : 0 : 0 )
/ /G a pb e t w e e nb a r si nb a rg r a p h
s y s u s ea u t o ,c l e a r
g r a p hb a r( m e a n )p r i c et u r nm p gw e i g h t ,o v e r ( r s e to b s7 5 r e p l a c er e p 7 8 = 2 . 5i n7 5
g r a p hb a r( m e a n )p r i c et u r nm p gw e i g h t ,o v e r ( r
www.survey-design.com.au/Stata Graphs.html
12/42
12/28/12
* ( N i c kC o x-S t a t al i s t , W e d ,2 6M a y2 0 1 0 )
/ /b a rg r a p h-c a t e g o r i c a lv a r i a b l e s-p r o p o r t
/ /I n c l u d i n gt o t a l si nb a rg r a p h s y s u s ea u t o ,c l e a r s a v ea u t o 1,r e p l a c e r e p l a c ef o r = 3 l a b e ld e f i n e o r i g i n3T o t a l ,a d d a p p e n du s i n ga u t o 1
g r a p hb a r( m e a n )p r i c e,o v e r ( r e p 7 8 ,)o v e r ( f o a s c a t e g o r ya s y v a rl e g e n d ( o f f )
e x i t
www.survey-design.com.au/Stata Graphs.html
13/42
12/28/12
/ /S t a c k i n gs o m el e v e l so fav a r i a b l ea n dn o t / /H a st h ey e a r s7 1a n d7 7s t a c k e da n dy e a r8 8
w e b u s eu n i o n ,c l e a r k e e pi d c o d ey e a ra g e r e c o d ey e a r( 7 17 7 = 1 )( 8 8 = 2 )( * = . ) ,g e n ( w h i c h ) g r a p hb a ra g e ,o v e r ( y e a r )o v e r ( w h i c h )a s y v a r s
e x i t
S t a t a l i s t N i c kC o x2 2 / 1 2 / 1 1
/ /C o l o r i n gb a r si no r d e ro fm a g n i t u d e .A l lb a r / /s e c o n dp l a c e sa r ec o n s i d e r e d( c o l o r e d )a s3 r
c l e a r i n p u tc o u n t r yg d p 1 1 0 0 2 1 1 0 3 2 4 0 4 5 0 5 1 0 e n d l i s t g s o r tg d p g e ng d p r e d = g d pi f_ n = = 1 g e ng d p g r e e n = g d pi f_ n = = 2 g e ng d p b l u e = g d pi f_ n > 2 l i s t
g r a p hh b a rg d p r e dg d p g r e e ng d p b l u e ,o v e r ( c o u n t r d e s c e n d i n g )n o f i l l l e g e n d ( l a b e l ( 1" B e s t "
l a b e l ( 3" r e s t " )r o w s ( 1 ) )b a r ( 1 ,c o l o r b a r ( 2 ,c o l o r ( d k g r e e n ) )b a r ( 3 ,c o l o r ( d
* ( S e r g i yR a d y a k i n-S t a t al i s t , S a t ,1 9F e b2 0 1 1 )
/ /C h a n g i n gb a rl a b e l s
www.survey-design.com.au/Stata Graphs.html
14/42
12/28/12
/ /C h a n g i n gb a rl a b e l s / / I ti ss o m e t i m e sm o r ec o n v e n i e n tt ou s ev a l u e / / l a b e lo p t i o n st oc h a n g eg r a p hb a rl a b e l s .I n / / l e g e n dt ob es p r e a do v e rt h ew i d t ho ft h eg r c l e a ra l l s y s u s ea u t o
* (S t a t al i s t , 2 5J u n2 0 1 1 )
/ /C h a n g i n gb a rl a b e l su s i n gt h eS t a t ag r a p he
/ /T h et y p eo fb a rl a b e li sl i m i t e di nS t a t ag / /a d d i t i o n a li n f o r m a t i o ni sr e q u i r e d .T h i sc a / /S t a t ag r a p h i c se d i t o rc o m m a n d s . / /B e l o wt h en u m b e ro fo b s e r v a t i o nf o re a c hr e s y s u s ec i t y t e m p ,c l e a r
# d e l i m i t; g r a p hb a rt e m p j u l yt e m p j a n ,o v e r ( r e g i o n )b a r g l e g e n d (l a b e l ( 1" J u l y " )l a b e l ( 2" J a n u a r y " )) y t i t l e ( " D e g r e e sF a h r e n h e i t " ) t i t l e ( " A v e r a g eJ u l ya n dJ a n u a r yt e m p e r a t u r e s s u b t i t l e ( " b yr e g i o n so ft h eU n i t e dS t a t e s " ) n o t e ( " S o u r c e : U . S .C e n s u sB u r e a u ,U . S .D e p t b l a b e l ( b a r ,p o s i t i o n ( i n s i d e )f o r m a t ( % 9 . 1 f )c o # d e l i m i tc r g e n e r a t eN o = _ n c o l l a p s e( s u m )N ,b y ( r e g i o n ) l i s t l o c a li 1 = 1 f o r v a l u e si = 1 / ` = _ N '{
g r _ e d i tp l o t r e g i o n 1 . b a r l a b e l s [ ` i 1 ' ] . t e x t={ }
www.survey-design.com.au/Stata Graphs.html
15/42
12/28/12
/ /A d d i n gb a rl a b e l s
/ /T h et y p eo fb a rl a b e li sl i m i t e di nS t a t ag / /a d d i t i o n a li n f o r m a t i o ni sr e q u i r e d .T h i sc a / /b ya d d i n ga n o t h e rt w o w a yp l o t .
* ( N i c kC o x-S t a t al i s t , T h u ,1 7F e b2 0 1 1 )
/ /A d d i n ga v e r a g el i n et ob a rg r a p h w e b u s eg r u n f e l d ,c l e a r k e e pi fc o m p a n y= =1 k e e pi fy e a r<1 9 4 5
/ / g e t t i n gd a t ao n l i n e
* ( N i c kC o x-S t a t al i s t , 1 3 / 4 / 1 1 )
/ /A d d i n ga v e r a g eb a rt og r o u p s y s u s ea u t o ,c l e a r s u m mp r i c ei ff o r = = 0&! m i s s i n g ( r e p 7 8 ) l o c a la v e 1 = r ( m e a n )
www.survey-design.com.au/Stata Graphs.html
16/42
12/28/12
g r a p hb a r( m e a n )p r i c e,o v e r ( r e p 7 8)o v e r ( f o r a s y v a rl e g e n d ( o f f ) s c h e m e ( c o l o r )b a r ( 7 ,c o l
/ /D i f f e r e n tc o l o rb a r s c l e a r i n p u t/ / / i dwp o sm a r k 1 11 6 9 . 5 5 2 12 6 5 . 1 6 3 13 6 4 . 9 1 4 14 6 4 . 5 3 5 15 6 3 . 7 0 6 06 8 4 . 5 8 7 07 8 4 . 5 1 8 08 8 4 . 1 2 9 09 8 3 . 3 4 1 001 08 2 . 8 e n d l a b e ld e f i n ek k 1 " B a r b o r a-C Z E" 2 " C h r i s t i n a-G E R" 3 " L i n d a-G E R" 4 " S u n e t t e-R S A " 5 " H u i h u i-C H N " 6 " K e s h o r n-T R I" 7 " O l e k s a n d r-U K R" 8 " A n t t i-F I N" 9 " V i t e z s l a v-C Z E " 1 0" T e r o-F I N " l a b e lv a l u ep o sk k l a b e ld e f i n ew1" W o m e n "0" M e n " l a b e lv a l u eww / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
e x i t
www.survey-design.com.au/Stata Graphs.html
17/42
12/28/12
g r a p hb a r( a s i s )g 1g 2 ,a s c a to v e r ( m a r k , s o r t ( m a r k ) r e l a b e l ( / / / 5 " B a r b o r a-C Z E" / / / 4 " C h r i s t i n a-G E R" / / / 3 " L i n d a-G E R" / / / 2 " S u n e t t e-R S A " / / / 1 " H u i h u i-C H N " / / / 1 0" K e s h o r n-T R I" / / / 9 " O l e k s a n d r-U K R" / / / 8 " A n t t i-F I N" / / / 7 " V i t e z s l a v-C Z E " / / / 6 " T e r o-F I N " / / / )l a b e l (a n g l e ( 4 5 ) ) ) o v e r ( w )y l a b e l ( 0 ( 1 0 ) 9 0 ) y t i t l e ( " M e t r e s " )b l a b e l ( b a r ,p o s i t i o n ( i n s i d e )f o r m a s c h e m e ( s 2 c o l o r ) l e g e n d ( o f f )b a r g a p ( 5 )t i t l e ( " L o n d o n o f i l l b a r ( 1 ,c o l o r ( d k n a v y ) )b a r ( 2 ,c o l o r ( r e d ) ) e x i t
/ /D i f f e r e n tc o l o rb a r sf o rt h eo v e ro p t i o nc l e a ra l l i n p u t/ / / i dwp o sm a r k 1 06 8 4 . 5 8 2 07 8 4 . 5 1 3 08 8 4 . 1 2 4 09 8 3 . 3 4 5 01 08 2 . 8 6 11 6 9 . 5 5 7 12 6 5 . 1 6 8 13 6 4 . 9 1 9 14 6 4 . 5 3 1 015 6 3 . 7 0 e n d l a b e ld e f i n ew1" W o m e n "0" M e n " l a b e lv a l u eww l a b e ld e f i n ek k 1 " K e s h o r n-T R I" 2 " O l e k s a n d r-U K R" 3 " A n t t i-F I N" 4 " V i t e z s l a v-C Z E " 5 " T e r o-F I N " 6 "" 7 " B a r b o r a-C Z E" 8 " C h r i s t i n a-G E R" 9 " L i n d a-G E R" 1 0 " S u n e t t e-R S A " 1 1" H u i h u i-C H N " / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
www.survey-design.com.au/Stata Graphs.html
18/42
12/28/12
t w o w a y( b a rm a r ki di fi d = = 1 ,b a s e ( 0 )y l a b e l ( 0 x t i t l e ( " M e n W x l a b e l ( 1 ( 1 ) 1 1 , v a l u e l a b e la n g l e ( 4 5 ) ) b a r w i d t h ( . 4 ) c o l o r ( c y a n ) )
/ /A d d i n gb a rl a b e l si nb o l df o n t
/ /A d d i n gb o l df o n tu s i n gt h eS t a t ae d i t o rc o m / /u s i n gap r o g r a m . p r o g r a mg 1 s y n t a xv a r n a m e ,o v e r ( v a r n a m e ) l e v e l s o f` o v e r ' ,l o c a l ( k k )
s y s u s ea u t o ,c l e a r g 1p r i c e,o v e r ( r e p 7 8 )
/ /U s i n gs t a c kt op r o d u c eag r a p h
u s eh t t p : / / w w w . s t a t a p r e s s . c o m / d a t a / r 1 0 / a u t o , p r e s e r v e t e m p f i l em y l a b e l l a b e ls a v eo r i g i nu s i n g` m y l a b e l '
www.survey-design.com.au/Stata Graphs.html
19/42
12/28/12
* ( N i c kC o x-S t a t al i s t , T h u ,8N o v2 0 0 7 )
// Log scale - not working as expected - fix // Also see: How can I best get box plots on logarithmic
Requires: myscale To download myscale type the following on the Stata co ssc install myscale
s y s u s en l s w 8 8 ,c l e a r c l o n e v a rw a g e l o g 1 0 = w a g e r e p l a c ew a g e l o g 1 0 = l o g 1 0 ( w a g e l o g 1 0 )
m y l a b e l s0 ( 1 0 ) 4 0,m y s c a l e ( l o g 1 0 ( @ ) )l o c a l ( l a b e l s )
/ /L a b e l l i n gt h eo v e rv a r i a b l e
s y s u s ea u t o ,c l e a r l e v e l s o ff o r ,l o c a l ( a ) l e v e l s o fr e p 7 8 ,l o c a l ( a 1 ) t e m p f i l ef i l e
f o r e a c hio fl o c a la{ f o r e a c hi 1o fl o c a la 1{ s y s u s ea u t o ,c l e a r
/ / f o r / / r e p 7 8
c o u n ti ff o r = = ` i '&r e p 7 8 = = ` i 1 ' / / m a k i n gs u r e
www.survey-design.com.au/Stata Graphs.html
20/42
12/28/12
} / / e n dl o o p
s u m m a r i z er e p 7 8 f o r e a c hio fl o c a la{ / / f o r s e to b s` = _ N + 1 ' r e p l a c ef o r = ` i 'i ff o r = = . l o c a lm a x 1 = ` r ( m a x ) ' r e p l a c er e p 7 8 = ` m a x 1 ' + 1i fr e p 7 8 = = . } g s o r tf o rr e p 7 8 d r o pi fp a r m = = " _ c o n s " g e no r d e r = _ N _ n l a b e ld e f i n ej j9" f o r e i g n = 0 3" f o r e i g n = 1 " / / / " / / / 0" r e p 7 8 _ 3 " 1" r e p 7 8 _ 4 " 2" r e p 7 8 _ 5 " 4" r e p 7 8 _ 1 " 5" r e p 7 8 _ 2 " / / / 7" r e p 7 8 _ 4 " 8" r e p 7 8 _ 5 "
6" r e p 7 8 _ 3 "
l a b e lv a l u eo r d e r j j
e x i t
/ /T e x tt h a tv a r i e si ns i z ed u et oac a l c u l a t e
c l e a rp r o g r a m s
* -b e g i ne x a m p l ep r o g r a ms
s y s u s ea u t o ,c l e a r t e m p n a m eh d l e t e m p f i l ei n f o p o s t f i l e` h d l e 's t r 2 0 v a r i a b l em i s s i n g su s i n g q u id s ,h a s ( t y p en u m e r i c ) q u if o r e a c hv a ri n` r ( v a r l i s t ) ' { / * w a n tm o r em i s s i n g s t om a k et h i si n t e r e s t i n g* / r e p l a c e` v a r ' = .i fr u n i f o r m ( ) < 0 . 1 q u ic o u n ti f` v a r ' = = . p o s t` h d l e '( " ` v a r ' " )( r ( N ) ) } p o s t c l o s e` h d l e ' u s e` i n f o ' ,c l e a r g e nn u m b e r = _ n l i s t ,n o o b s g e nx=1 s u mm i s s i n g s ,m e a n o n l y g e nf a c t o r=m i s s i n g s / r ( m e a n ) l e v e l s o fv a r i a b l e ,l o c a l ( a a ) l o c a la = 1 f o r e a c hio fl o c a la a{
www.survey-design.com.au/Stata Graphs.html
21/42
12/28/12
/ /D o u b l eg r a p h-i ft h e r ei st o om u c hd e t a i lf o / /g r a p ht h e n2g r a p h sc a nb eu s e d
c l e a r s e tm o r eo f f u s e" h t t p : / / w w w . s t a t a p r e s s . c o m / d a t a / r 1 1 / n l s w o r k d e s c r i b e r e p l a c ey e a r = 1 9 0 0 + y e a r
g e na g e 1 1 =y o f d ( d a t e ( c ( c u r r e n t _ d a t e ) ) , " D M Y " )( 1
r e c o d ea g e 1 1( 5 6 / 5 8 = 1 )( 5 9 / 6 1 = 2 )( 6 2 / 6 4 = 3 )( 6 5 / 6
c o l l a p s e( c o u n t )u n i o n ( c o u n t )i d c o d e, b y ( y e a r g e np e r = ( u n i o n / i d c o d e ) * 1 0 0
t w o w a y ( l i n e p e ry e a r i fa g e 1 1 = = 1,l p a t t e r n ( d a s h ) ( l i n e p e ry e a r i fa g e 1 1 = = 2,l p a t t e r n ( d a s h _ d o ( l i n e p e ry e a r i fa g e 1 1 = = 3,l p a t t e r n ( d o t )c n a m e ( a 1 , r e p l a c e )l e g e n d ( b m a r g i n ( l = 4 0 ) s t a c l a b e l (1" 5 6 5 8 " ) l a b e l (2" 5 9 6 1 " ) l a b e l (3" 6 2 6 4 " ) r o w s ( 1 ) )x t i t l e ( " " ) x l a b e l ( 1 9 6 8" 1 9 6 8 " 1 9 7 8" 1 9 7 8
t w o w a y ( l i n e p e ry e a r i fa g e 1 1 = = 4, y a x i s ( 1 ) l p a t t ( l i n e p e ry e a r i fa g e 1 1 = = 5, y a x i s ( 1 ) l p a t t n a m e ( a 2 , r e p l a c e )l e g e n d ( b m a r g i n ( r = 4 0 ) s t a c k l a b e l ( 1" 6 5 6 7 " ) l a b e l (2" 6 8 7 0 " ) r o w s ( 1 ) )x t i t l e ( " " ) x l a b e l ( 1 9 6 8" 1 9 6 8 " 1 9 7 8" 1 9 7 8 y s c a l e ( a l t )x l i n e ( 1 9 6 8 ) g r a p hc o m b i n ea 1a 2,i m a r g i n s ( z e r o )y c o m m o n
/ / G r a p h i n gt h em e a no fav a r i a b l eb yo t h e rv a r s y s u s ea u t o ,c l e a r c o l l a p s e( m e a n )m p g,b y ( f o rp r i c e )
www.survey-design.com.au/Stata Graphs.html
22/42
12/28/12
/ /D i f f e r e n c ef r o mA v e r a g eb yl e v e l so fg r o u p s s y s u s ea u t o ,c l e a r g r a p hd r o p_ a l l b y s o r tf o rr e p 7 8:e g e nm p g 1 = m e a n ( m p g ) b y s o r t r e p 7 8 :e g e nm p g 2 = m e a n ( m p g ) g e nm p g 3 = m p g 1 m p g 2 r e p l a c em p g 2 = 0i fr e p 7 8 = = .
t w o w a y/ / / ( b a r m p g 3r e p 7 8i ff o r = = 1&r e p 7 8 = = 1, f c o l o r (
/ /S t a c k e db a rg r a p h-r e s h a p i n gt h ed a t a
/ / c r e a t i n gt h ed a t a
www.survey-design.com.au/Stata Graphs.html
23/42
12/28/12
g r a p hb a r( s u m )s a l e s, o v e r ( s e c t o r )o v e r ( w h i o v e r ( t i m e 2 ,l a b e l ( l a b s i z e ( v s m a l l ) ) )a s y v a r ss t
g r a p hb a r( s u m ) c s u m, o v e r ( f o r )o v e r ( r e p 7 8 ) a s y v a r ss t a c kl e g e n d ( s i z e ( v s m a l l ) )p e r b l a b e l p o s i t i o n ( c e n t e r )f o r m a t ( % 3 . 1 f ) )
* ( A l s os e et h eu s e rw r i t t e np r o g r a mc a t p l o to n h t t p : / / w w w . s u r v e y d e s i g n . c o m . a u / U s e r g r a p h s . h t m )
/ /E d i t i n gag r a p hw i t ht h eg r _ e d i tc o m m a n d
/ / T h eg r a p hw i t ht h eya x i sl i n e s y s u s ea u t o ,c l e a r g r a p hh b a r w e i g h tp r i c e ,y s c a l e ( o f f )
/ / T h eg r a p hw i t ht h eya x i sl i n er e m o v e d
s y s u s ea u t o ,c l e a r g r a p hh b a r w e i g h tp r i c e ,y s c ( o f f ) g r _ e d i t. v a r a x i s . s t y l e . e d i t s t y l el i n e s t y l e ( c o l
/ / T h ea b o v ec a na l s ob ep r o d u c e dw i t h :
g r a p hh b a rw e i g h tp r i c e, s h o w y v a r y v a r o p t i o n s
F r o mt h eS t a t a l i s t : K i e r a nM c C a u l 1 1 / 6 / 2 0 1 0 D a v i dA i r e y 1 1 / 6 / 2 0 1 0
www.survey-design.com.au/Stata Graphs.html
24/42
12/28/12
/ /B r e a k i n gu pt e x ta u t o m a t i c a l l yi nal o n gn o w e b u s ea u t o ,c l e a r s e tm o r eo f f
/ / i n p u tt h et e x tt op l a c ei nt h eg r a p h-c a nb
l o c a lz 1" T h ed e v e l o p m e n to fL i n u xi so n eo ft l o c a lz 2"e x a m p l e so ff r e e a n d o p e ns o u r c e l o c a lz 3" l o c a lz 3c o l l a b o r a t i o n ;t y p i c a l l ya l l o c a lz 4" c a nb e u s e d ,f r e e l y m o d i f i e d ,a n dr e l o c a lz 5" a n d n o n c o m m e r c i a l l y ,b ya n y o n eu n d l o c a lz 6" G e n e r a lP u b l i cL i c e n s e . " l o c a lz 7" T y p i c a l l yL i n u xi sp a c k a g e di naf o r l o c a lz 8" d i s t r i b u t i o nf o rd e s k t o pa n ds e r v e r
g r a p hb a rp r i c e , b a r g a p ( 3 0 )o v e r ( f o r e i g n ) n o t e (` t 1 ', m a r g i n ( l a r g e )j u s t i f i c a t i o n ( l e f e x i t
/ /B r e a k i n gu pl o n gl a b e l s s y s u s ea u t o ,c l e a r
l a b e ld e f i n eo r i g i n1" g r o u p 1g r o u p 1g r o u p 1g r g r o u p 1g r o u p 1g r o u p 1g r o u p 1g r o u p 1" ,m o d i f y ; # d e l i m i tc r
i f` l e n ' > ` s _ l e n '{ f o r v a l u e si = 1 / ` = ` l e n ' / ` s _ l e n ' + 1 '{ l o c a lp 1:p i e c e` i '` s _ l e n 'o f` " l o c a lr e l a b e l s` " ` r e l a b e l s '` = c h a r } l o c a lr e l a b e l s 1 ` r e l a b e l s 1 ' l o c a lr e l a b e l s } }
` = ` g ' + 1
g r a p hh b a rm p g ,o v e r ( f o r ,r e l a b e l ( ` r e l a b e l s 1 ' e x i t
www.survey-design.com.au/Stata Graphs.html
25/42
12/28/12
e x i t
n e ti n s t a l lg r c 1 l e g , f r o m (h t t p : / / w w w . s t a t a . c o m / u s e c l e a r a l l
i n p u tq u e s ts t r 2 5q 1" Q u e s t i o n1 " 1" B e n c h m a r kQ 1 " 2" Q u e s t i o n2 " 2" B e n c h m a r kQ 2 " 3" Q u e s t i o n3 " 3" B e n c h m a r kQ 3 " 4" Q u e s t i o n4 " 4" B e n c h m a r kQ 4 " e n d
a 1a 2a 3a 4a 5a 6 0 2 3 74 51 24 2 5 2 54 71 74 1 3 7 24 01 73 2 5 2 54 7 41 7 1 2 4 03 71 73 2 5 2 54 71 74 1 2 3 7 1 734 0 2 5 4 72 51 74
g r a p hh b a ra 1 a 6i fq u e s t = = 1 ,p e r c e n to v e r ( q u e s t , s t a c kl e g e n d ( o f f )y s c a l e ( o f f )y l i n e ( 2 04 06 08 0, l s a v i n g ( a 1 ,r e p l a c e ) y s c a l e ( o f f )p l o t r e g i o n ( m a r g i n (
g r a p hh b a ra 1 a 6i fq u e s t = = 2 ,p e r c e n to v e r ( q u e s t , s t a c kl e g e n d ( o f f )y s c a l e ( o f f )y l i n e ( 2 04 06 08 0, l s a v i n g ( a 1 a ,r e p l a c e ) y s c a l e ( o f f )p l o t r e g i o n ( m a r g i n (
g r a p hh b a ra 1 a 6i fq u e s t = = 3 ,p e r c e n to v e r ( q u e s t , s t a c kl e g e n d ( o f f )y s c a l e ( o f f )y l i n e ( 2 04 06 08 0, l s a v i n g ( a 1 b ,r e p l a c e ) y s c a l e ( o f f )p l o t r e g i o n ( m a r g i n (
g r c 1 l e ga 1 . g p ha 1 a . g p ha 1 b . g p ha 2 . g p h ,c o l s ( 1 )/ / / i m a r g i n ( 0000 )y c o m m o nx c o m m o nl e g e n d f r o m ( a 2 . g p h e x i t
* ( A l s os e et h eu s e rw r i t t e np r o g r a mc a t p l o to n : h t t p : / / w w w . s u r v e y d e s i g n . c o m . a u / U s e r g r a p h s . h t m l )
/ /T a b l eu n d e rxa x i s c l e a ra l l s y s u s ea u t o
www.survey-design.com.au/Stata Graphs.html
26/42
t w o w a y/ / / ( s c a t t e r f o rr e p 7 8i ff o r = = 1 ,m l a b e l ( _ f r e q ) m l a b p o s i t i o n ( 1 2 ) ) ( s c a t t e rf o rr e p 7 8i ff o r = = 0 ,m l a b e l ( _ f r e q )m s y m m l a b p o s i t i o n ( 6 ) ) ,n a m e ( a 2 ,r e p l a c e )l e g e n d ( o f f )y t i t l e ( " b " ) f y s y s c a l e (o f ff i l l ) g r a p h r e g i o n ( c o l o r ( l t b l u e) ) y l a b e l ( , n o g r i d )x l a b e l (1 ( 1 ) 5)
g r a p hc o m b i n ea 1a 2 ,c o l s ( 1 )x c o m m o ni m a r g i n ( 00 e x i t
/ /T a b l eo nt h es i d eo fag r a p h
s y s u s ea u t o ,c l e a r t w o w a y( s c a t t e rm p gr e p 7 8 ),g r a p h r e g i o n (m a r g
f o r v a l u e si = 0 / 4{ / / r o w s l o c a l+ + z g r _ e d i tA d d T e x t B o xa d d e d _ t e x te d i t o r` = 8 0 ( ` g r _ e d i ta d d e d _ t e x t _ n e w=` z ' g r _ e d i ta d d e d _ t e x t _ r e c=` z ' g r _ e d i ta d d e d _ t e x t [ ` z ' ] . t e x t={ } g r _ e d i ta d d e d _ t e x t [ ` z ' ] . t e x t . A r r p u s h/ / / " ` = r e p 7 8 [ ` z ' 2 ] ' ` = s t r i n g ( m p g [ ` z ' 2 ] , " % } e x i t
/ /T a b l eo nt h es i d eo fag r a p h s y s u s ea u t o ,c l e a r
f o r v a ln=1 / 2 0{ l o cs i d e t a b l e` "` s i d e t a b l e ' "` = m a k e } f o r v a ln=1 / 2 0{ l o cs i d e c a p` "` s i d e c a p ' " } s y s u s ea u t o ,c l e a r t w o w a y( s c a t t e rm p gr e p 7 8 ), n o t e ( " { b f :M A K E } " " " ` s i d e t a b l e ' ,s i z e ( m e d s m a l l )c o l o r ( g r e e n ) p o s i t i o n ( 2 )m a r g i n ( s m a l l ) j u s t i f i c a t i o n ( l e f t ) ) c a p t i o n ( "{ b f : P r i c e} " " " ` s i d e c a p ' ,s i z e ( m e d s m a l l )c o l o r ( m i d g r e e n ) p o s i t i o n ( 2 )m a r g i n ( v s m a l l )
$ ` = p r
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
p o s i t i o n ( 2 )m a r g i n ( v s m a l l ) j u s t i f i c a t i o n ( r i g h t ) ) e x i t
/ / /
/ / B a s e ds t a t a l i s tJ u n9 ,2 0 1 1 / / E r i cB o o t h
/ /P u t t i n gD a t as e tr e s u l t si nag r a p h c l e a ra l l s y s u s ea u t o q u i e t :s u m m a r i z em p g
/ /A d d i n gap i c t u r ea sb a c k g r o u n dt oag r a p h
/ / y o u rp i c t u r ei sc o n v e r t e db y : h t t p : / / w w w . t e / / s e tw i d t ht o5 0 0
s e tm o r eo f f i n f i xs t r 1a 1 a 5 0 01 5 0 0u s i n g" c a ra s c i it ot
/ / h e x d u m pm a yb en e e d e dt ol o o kf o ru n e x p e c t e d / / h e x d u m p" c : / c a ra s c i it ot e x tb i g . t x t " ,t a b u / / s c a l i n gya x i sp i c t u r ed a t aa n dr e v e r s i n g g e n e r a t ey = 9 0 + ( ( _ N _ n + 1 ) * 1 ) r e s h a p el o n ga ,i ( y )j ( c o l ) / / r e p l a c i n gc h a r a c t e r sw i t hn u m b e r s r e p l a c ea = " 1 "i fa = = " + " r e p l a c ea = " 2 "i fa = = " " r e p l a c ea = " 3 "i fa = = " . " r e p l a c ea = " 4 "i fa = = " / " r e p l a c ea = " 5 "i fa = = " : " r e p l a c ea = " 6 "i fa = = " " r e p l a c ea = " 7 "i fa = = " M " r e p l a c ea = " 8 "i fa = = " N " r e p l a c ea = " 9 "i fa = = " ` " r e p l a c ea = " 1 0 "i fa = = " d " r e p l a c ea = " 1 1 "i fa = = " h " r e p l a c ea = " 1 2 "i fa = = " m " r e p l a c ea = " 1 3 "i fa = = " o " r e p l a c ea = " 1 4 "i fa = = " s " r e p l a c ea = " 1 5 "i fa = = " y "
/ / s c a l i n gxa x i sp i c t u r ed a t a r e p l a c ec o l = c o l / 1 0
t w o w a y/ / / ( s c a t t e ryc o li fa = = " 1 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 000 * . 2 0 " )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 2 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 2 5 52 5 52 5 5 * . 2 " )m s i z e ( ` s i z e ' ) ) / / / ( s c a t t e ryc o li fa = = " 3 ", m s y m b o l ( c i r c l e )/ / /
12/28/12
( s c a t t e ryc o li fa = = " 3 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 2 5 500 * . 2 " )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 4 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 002 5 5 * . 2 " )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 5 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 002 5 5 * . 2 " )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 6 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 2 5 52 5 50 * . 2 " )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 7 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 02 5 52 5 5 * . 2 " )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 8 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( " 2 5 502 5 5 * . 2 " )m s i z e ( ` s i z e ' )) / / /
( s c a t t e ryc o li fa = = " 9 ", m s y m b o l ( c i r c l e )/ / / m c o l o r ( w h i t e )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 1 0 " , m s y m b o l ( c i r c l e )/ / / m c o l o r ( o r a n g e * . 2 )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 1 1 " , m s y m b o l ( c i r c l e )/ / / m c o l o r ( b r o w n * . 2 )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 1 2 " , m s y m b o l ( c i r c l e )/ / / m c o l o r ( s a n d * . 2 )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 1 3 " , m s y m b o l ( c i r c l e )/ / / m c o l o r ( b l a c k * . 2 )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 1 4 " , m s y m b o l ( c i r c l e )/ / / m c o l o r ( b l a c k * . 2 )m s i z e ( ` s i z e ' )) / / / ( s c a t t e ryc o li fa = = " 1 5 " , m s y m b o l ( c i r c l e )/ / / m c o l o r ( b l a c k * . 2 )m s i z e ( ` s i z e ' )) / / / ( s c a t t e r l e n g t hm p g ) / / / ( l f i t l e n g t hm p g ) / / / , y s c a l e (r a n g e ( 1 0 02 5 0 ) ) / / / y t i t l e ( L e n g t hi n s . )x t i t l e ( m p g ) / / / t i t l e ( M p gvL e n g t h )l e g e n d ( o f f ) e x i t
/ /A d d i n gap i c t u r ea sb a c k g r o u n dt oag r a p h
/ / T h ea b o v ep i c t u r ec a nb ei m p r o v e do n ,i fr e q u i r / / i n t oh t m la n dt h e nc l e a n i n gt h i sf i l eu pi nS t a
/ / T h ep i c t u r ei sf i r s tl o a d e di n t oG I M P( f r e es o f / / a sh t m l( R e m m e m b e rt h el a r g e rt h ep i c t u r et h em / / r e q u i r e da n dt h el o n g e ri tw i l lt a k et op r o c e s s
c l e a ra l l s e tm o r eo f f s e tm e m o r y9 0 0 m
p r o g r a mh e x s y n t a xv a r n a m e,N e w v ( n a m e )
/ / v a l u e st h a tc a nb ec h a n g e d l o c a ld i v i d e = 8 / / s a m p l es i z e
www.survey-design.com.au/Stata Graphs.html
29/42
12/28/12
g e ns t r 2 0c o l o r = c h a r ( 3 4 ) + s t r i n g ( d e c 1 c , " % 3 . 0 f " ) + " + "" + s t r i n g ( d e c 3 c , " % 3 . 0 f " ) + c h a r ( 3 4 ) e g e ng r o u p _ c o l = g r o u p ( c o l o r ) c o m p r e s s s a v ep i c t _ g r a p h ,r e p l a c e u s ep i c t _ g r a p h ,c l e a r q u i e ts u m m a r i z ec o l l o c a ls i z e = " * ` = 3 3 / ` r ( m a x ) ' ' " l e v e l s o fc o l o r ,l o c a l ( a )
/ / m a r k e rs i z e ,c
l o c a lz = 2 } e l s e{ l o c a lz z 1=/ / / ` " ( s c a t t e ryc o li fg r o u p _ c o l = = ` z ', m s y m b o l ( s q u a r l o c a lz z" ` z z '` z z 1 ' " l o c a l+ + z } } t w o w a y` z z ' ( s c a t t e r l e n g t hm p g ,m c o l o r ( r e d )m s i z e ( l a r g e ) ) ( l f i t l e n g t hm p g ) , y s c a l e (r a n g e ( 1 0 03 2 5 ) ) y t i t l e ( L e n g t hi n s . )x t i t l e ( m p g ) t i t l e ( M p gvL e n g t h )l e g e n d ( o f f ) e x i t
www.survey-design.com.au/Stata Graphs.html
30/42
12/28/12
/ /O d d sr a t i og r a p h
* *r e q u i r e s2u s e rw r i t t e ng r a p h s :p a r m e s ta n d * *i fn o ta l r e a d yi n s t a l l e d : s s ci n s t a l lp a r m e s t s s ci n s t a l le g e n m o r e
s y s u s en l s w 8 8 ,c l e a r l o g i tu n i o nm a r r i e dn e v e r _ m a r r i e dc o l l g r a ds o u p a r m e s t ,n o r e s t o r ee f o r m e g e na x i s =a x i s ( e s t i m a t e ) ,l a b e l ( p a r m )
* ( M a a r t e nL .B u i s-S t a t a l i s t , 1 4 / 0 9 / 2 0 1 0 )
/ /O d d sr a t i og r a p h-s e l e c t i n go r d e ro fb a r s
* *r e q u i r e s2u s e rw r i t t e ng r a p h s :p a r m e s ta n d * *i fn o ta l r e a d yi n s t a l l e d : s s ci n s t a l lp a r m e s t s s ci n s t a l le g e n m o r e
s y s u s en l s w 8 8 ,c l e a r l o g i tu n i o nm a r r i e dn e v e r _ m a r r i e dc o l l g r a ds o u p a r m e s t ,n o r e s t o r ee f o r m t a be s t i m a t e ,g e n ( k k ) / / o r d e ro fb a r s i n p u ta 1 2 3 4 5 e g e na x i s =a x i s (ae s t i m a t e ) ,l a b e l ( p a r m )
www.survey-design.com.au/Stata Graphs.html
31/42
/ /O d d sr a t i og r a p h
* *r e q u i r e s2u s e rw r i t t e ng r a p h s :p a r m e s ta n d * *i fn o ta l r e a d yi n s t a l l e d : s s ci n s t a l lp a r m e s t s s ci n s t a l le g e n m o r e
s y s u s en l s w 8 8 ,c l e a r l o g i tu n i o nm a r r i e dn e v e r _ m a r r i e dc o l l g r a ds o u p a r m e s t ,n o r e s t o r ee f o r m e g e na x i s =a x i s ( e s t i m a t e ) ,l a b e l ( p a r m )
t w o w a ys c a t t e r a x i se s t i m a t e ,/ / / x l i n e ( 1 )y l a b ( ,v a l u e l a b e la n g l e ( 0 r c a pm i n 9 5m a x 9 5a x i s ,h o r i z o n t a l
/ / P a i r e dP l o t1 / / L o o k i n ga tt h ee f f e c to ft r e a t m e n to nt h ew e / / t h o s eu n d e rt h el i n eh a v el o s tw e i g h t .
/ / M a k i n gu pt h ed a t a ;b a s e do nt h ed a t af o u n d / / h t t p : / / u s e r s . p o l i s c i . w i s c . e d u / k r i t z e r / T e a c h i
c l e a r s e to b s7 6 g e nt r e a t m e n t = m o d ( _ n , 3 ) + 1
12/28/12
g e n e r a t ew _ b e f o r e 1 = r n o r m a l ( 8 2 ,4 . 8 ) r e p l a c ew _ b e f o r e 1 = r n o r m a l ( 8 1 ,5 . 7 ) i ft r e a t m e r e p l a c ew _ b e f o r e 1 = r n o r m a l ( 8 3 ,5 ) i ft r e a t m e
g e n e r a t ew _ a f t e r 1 = r n o r m a l ( 8 5 . 6 ,8 . 3 ) r e p l a c ew _ a f t e r 1 = r n o r m a l ( 8 1 . 1 ,4 . 7 )i ft r e a t m e r e p l a c ew _ a f t e r 1 = r n o r m a l ( 9 0 ,5 . 4 ) i ft r e a t m e / / t h ee n do fm a k i n gu pt h ed a t a
/ / F r o mS t a t aJ o u r n a lA r t i c l eV o l9N o4 / / S p e a k i n gS t a t a :P a i r e d ,p a r a l l e l ,o rp r o f i l ep l o t s / / f o rc h a n g e s ,c o r r e l a t i o n s ,a n do t h e rc o m p a r i s o n s / / N i c h o l a sJ .C o x / / C o n t a c tu st os u b s c r i b et ot h eS t a t aJ o u r n a l
/ / P a i r e dP l o t2 / / L o o k i n ga tt h ee f f e c to ft r e a t m e n to nt h ew e
/ / M a k i n gu pt h ed a t a ;b a s e do nt h ed a t af o u n d
/ / h t t p : / / u s e r s . p o l i s c i . w i s c . e d u / k r i t z e r / T e a c h i c l e a r s e to b s7 6 g e nt r e a t m e n t = m o d ( _ n , 3 ) + 1
g e nw _ b e f o r e 1 = r n o r m a l ( 8 2 ,4 . 8 ) r e p l a c ew _ b e f o r e 1 = r n o r m a l ( 8 1 ,5 . 7 ) i ft r e a t m e r e p l a c ew _ b e f o r e 1 = r n o r m a l ( 8 3 ,5 ) i ft r e a t m e
g e nw _ a f t e r 1 = r n o r m a l ( 8 5 . 6 ,8 . 3 ) r e p l a c ew _ a f t e r 1 = r n o r m a l ( 8 1 . 1 ,4 . 7 )i ft r e a t m e r e p l a c ew _ a f t e r 1 = r n o r m a l ( 9 0 ,5 . 4 ) i ft r e a t m e / / e n do fm a k i n gu pt h ed a t a
l a b e lv a rw _ b e f o r e"W e i g h tb e f o r et r e a t m e n t , l a b e lv a rw _ a f t e r "W e i g h ta f t e rt r e a t m e n t ,l l a b e ld e f i n et r e a t/ / / 1 " C o g n i t i v eb e h a v i o u r a l "/ / / 2 " C o n t r o l " / / / 3 " F a m i l yt h e r a p y " l a b e lv a l u e st r e a t m e n tt r e a t g e nb y t eo n e=1 g e nb y t et w o=2
/ / F r o mS t a t aJ o u r n a lA r t i c l eV o l9N o4 / / S p e a k i n gS t a t a :P a i r e d ,p a r a l l e l ,o rp r o f i l ep l o t s / / f o rc h a n g e s ,c o r r e l a t i o n s ,a n do t h e rc o m p a r i s o n s / / N i c h o l a sJ .C o x / / C o n t a c tu st os u b s c r i b et ot h eS t a t aJ o u r n a l .
www.survey-design.com.au/Stata Graphs.html
33/42
12/28/12
/ / P a i r e dP l o t3 / / L o o k i n ga tt h ee f f e c to ft r e a t m e n to nt h ew e
/ / M a k i n gu pt h ed a t a ;b a s e do nt h ed a t af o u n d / / h t t p : / / u s e r s . p o l i s c i . w i s c . e d u / k r i t z e r / T e a c h i c l e a r s e to b s7 6 g e nt r e a t m e n t = m o d ( _ n , 3 ) + 1
g e nw _ b e f o r e 1 = r n o r m a l ( 8 2 ,4 . 8 ) r e p l a c ew _ b e f o r e 1 = r n o r m a l ( 8 1 ,5 . 7 ) i ft r e a t m e r e p l a c ew _ b e f o r e 1 = r n o r m a l ( 8 3 ,5 ) i ft r e a t m e
g e nw _ a f t e r 1 = r n o r m a l ( 8 5 . 6 ,8 . 3 ) r e p l a c ew _ a f t e r 1 = r n o r m a l ( 8 1 . 1 ,4 . 7 )i ft r e a t m e r e p l a c ew _ a f t e r 1 = r n o r m a l ( 9 0 ,5 . 4 ) i ft r e a t m e / / e n do fm a k i n gu pt h ed a t a
b y s o r tt r e a t m e n t( w _ b e f o r ew _ a f t e r ):g e no r d e
/ / F r o mS t a t aJ o u r n a lA r t i c l eV o l9N o4 / / S p e a k i n gS t a t a :P a i r e d ,p a r a l l e l ,o rp r o f i l ep l o t s / / f o rc h a n g e s ,c o r r e l a t i o n s ,a n do t h e rc o m p a r i s o n s / / N i c h o l a sJ .C o x / / C o n t a c tu st os u b s c r i b et ot h eS t a t aJ o u r n a l .
/ / P a r a l l e lC o o r d i n a t eP l o t / / R e s c a l i n gv a r i a b l e sa n dp l o t t i n g
s y s u s ea u t o ,c l e a r d r o pm a k e s e tm o r eo f f l o c a lz = 1 f o r e a c hio fv a r l i s t_ a l l{ s u m m a r i z e` i ' ,d e t a i l
# d e l i m i t; l o c a la` " ` a 't e x t (1 . 2` z '` = c h a r ( 3 4 ) ' ` i ' ` = ` = c h a r ( 3 4 ) ' ` = s t r i n g ( ` r ( m a x ) ' , " % 8 . 2 f " ) ' ` = c h a r o r i e n t a t i o n ( v e r t i c a l )p l a c e m e n t ( n )s i z e ( 3 )j t e x t (0 . 1` z '` = c h a r ( 3 4 ) '` i ' ` = c h a r ( 3 4 ) ' ` = c h a r ( 3 4 ) ' ` = s t r i n g ( ` r ( m i n ) ' , " % 8 . 2 f " ) ' ` = c h a r o r i e n t a t i o n ( v e r t i c a l )p l a c e m e n t ( s o u t h )s i z e ( # d e l i m i tc r / / s c a l i n gv a r i a b l e s g e n e r a t ex ` z ' = ( ` i 'r ( m i n ) ) / ( r ( m a x ) r ( m i n ) ) l o c a l+ + z } d r o pi fr e p 7 8 = = . g e n e r a t ei d = _ n r e s h a p el o n gx ,i ( i d )j ( t y p e ) d r o pi fx > 1 0 0
t w o w a y / ( l i n e xt y p ei ff o r = = 1 ,l c o l o r ( r e d )c ( L ) ) / ( l i n e xt y p ei ff o r = = 0 ,l c o l o r ( b l u e )c ( L ) ) ,/
www.survey-design.com.au/Stata Graphs.html
34/42
12/28/12
/ / A l s os e e :S t a t aJ o u r n a lA r t i c l eV o l5N o / / T h ea r r o w so fo u t r a g e o u sf o r t u n eb yN i c h o / / h t t p : / / w w w . s t a t a j o u r n a l . c o m / s j p d f . h t m l ? a / / C o n t a c tu st os u b s c r i b et ot h eS t a t aJ o u r
/ / t s l i n ew i t hc o n f i d e n c ei n t e r v a l s-u s i n gm a r
R e q u i r e s :p a r m e s t t od o w n l o a dt h i sp r o g r a mt y p et h ef o l l o w i n go n l i n e( i fn o ta l r e a d yl o a d e d ) : s s ci n s t a l lp a r m e s t
R e q u i r e s :t s m k t i m t od o w n l o a dt h i sp r o g r a mt y p et h ef o l l o w i n go n l i n e( i fn o ta l r e a d yl o a d e d ) : s s ci n s t a l lt s m k t i m
T or u nt h ee x a m p l e :c o p yt h ef o l l o w i n gi n t oa
w e b u s eg r u n f e l d ,c l e a r q u ir e gi n v e s ti . y e a ri . c o m p a n y m a r g i n si . y e a r ,p o s t / /R o g e rN e w s o n ' sp a r m e s tf r o mS S C p a r m e s t ,s a v i n g ( y e a r e f f ,r e p l a c e ) u s ey e a r e f f ,c l e a r / /K i tB a u m ' st s m k t i mf r o mS S C t s m k t i my e a r ,s t a r t ( 1 9 3 5 ) t s l i n ee s t i m a t e| |t s r l i n em i n 9 5m a x 9 5 ,l e g e n d
F r o mS t a t a l i s t :
www.survey-design.com.au/Stata Graphs.html
35/42
12/28/12
/ / P r e d i c t i v em a r g i n sw i t h9 5 %c o n f i d e n c ei n t e r
R e q u i r e s :p a r m e s t t od o w n l o a dt h i sp r o g r a mt y p et h ef o l l o w i n go n l i n e( i fn o ta l r e a d yl o a d e d ) : s s ci n s t a l lp a r m e s t
s y s u s ea u t o ,c l e a r g e nk p r i c e = p r i c e / 1 0 0 0 r e g r e s s m p gc . k p r i c e # # c . k p r i c ei . f o r e i g ni . r e
t w o w a y/ / / ( r a r e am a x 9 5m i n 9 5k p r i c e ,p s t y l e ( c i ) )/ / / ( l i n ee s t i m a t ek p r i c e ) ,/ / / y t i t l e ( M i l e sp e rg a l l o n )/ / / t i t l e ( P r e d i c t i v em a r g i n sw i t h9 5 %c o n f i d e n c ei x l a b e l ( 3 ( 3 ) 1 51 0 . 4 )l e g e n d ( o f f )
F r o mS t a t aN e w sV o l2 5N o3
I nS t a t a1 2s e em a r g i n s p l o t
/ / G e n e r a t i n gp r e d i c t i o n sa n dg r a p h i n gt h e m
s y s u s ea u t o ,c l e a r
/ /e s t i m a t et h em o d e l r e gp r i c ec . m p g # # c . w e i g h t # # c . w e i g h ti . f o r e i g n / /c o l l e c ts o m ei n f o r m a t i o na b o u tm p g s u mm p g l o c a lm=r ( m e a n ) l o c a ls d=r ( s d )
/ /w ea r eg o i n gt oc h a n g et h ed a t a ,s ow ep r e s p r e s e r v e
/ /p r e d i c tt h ep r i c ew h i l ef i x i n gm p ga td i f f e r e p l a c em p g=` m ' p r e d i c ty h a t m r e p l a c em p g=` m '-` s d ' p r e d i c ty h a t l r e p l a c em p g=` m '-2 * ` s d ' p r e d i c ty h a t l l r e p l a c em p g=` m '+` s d ' p r e d i c ty h a t h r e p l a c em p g=` m '+2 * ` s d ' p r e d i c ty h a t h h
/ /n i c e rd i s p l a yo fl a r g en u m b e r s1 , 0 0 0i n s t e a f o r m a ty h a t *% 8 . 0 g c
www.survey-design.com.au/Stata Graphs.html
36/42
12/28/12
/ /c r e a t eg r a p h s o r tw e i g h t t w o w a yl i n ey h a t l ly h a t ly h a t my h a t hy h a t h hw e t i t l e ( " p r e d i c t e dp r i c ef o rU Sc a r s " ) y t i t l e ( " p r e d i c t e dp r i c e( U S{ cS | } ) " ) l p a t t e r n ( s o l i ds o l i ds o l i ds o l i ds o l i d ) l c o l o r ( " 2 5 52 5 52 0 4 " " 1 6 12 1 81 8 0 " "6 51 8 21 9 6 " "4 41 2 71 8 4 " "3 7 5 21 4 8 ") l e g e n d (o r d e r (-" m p g " 1" m e a n 2 * s d " 2" m e a n 1 * s d " 3" m e a n " 4" m e a n + 1 * s d " 5" m e a n + 2 * s d ") ) / /g e to u ro r i g i n a ld a t ab a c k r e s t o r e
m a r t i nb u i s W e d ,2 3F e b2 0 1 11 0 : 5 7 : 3 6 S t a t a l i s t
/ / L a b e l l i n gi nt e r m so fa n o t h e rv a r i a b l e
R e q u i r e s :l a b m a s k t od o w n l o a dt h i sp r o g r a mt y p et h ef o l l o w i n go n l i n e( i fn o ta l r e a d yl o a d e d ) : s s ci n s t a l ll a b m a s k
s y s u s ea u t o ,c l e a r s o r tp r i c e g e no r d e r=_ n l a b m a s ko r d e r ,v a l u e s ( m a k e ) t w o w a yb a rp r i c eo r d e r ,h o r i z o n t a ly l a b e l ( 84 4
MC o x S t a t a l i s t2 1 / 1 2 / 1 0
/ / C h a n g i n gt h eo r d e ro ft w o w a yg r a p h sw h e r et h
R e q u i r e s :e g e n m o r e t od o w n l o a dt h i sp r o g r a mt y p et h ef o l l o w i n go n l i n e( i fn o ta l r e a d yl o a d e d ) : s s ci n s t a l le g e n m o r e
/ / t h ef o l l o w i n gi sa ne x a m p l eo fo r d e rt h a tt h / / d e t e r m i n e db yS t a t a / /i e .l o w e s tr e p 7 8v a l u et oh i g h e s t
s c a t t e rm p gw e i g h t,b y ( r e p 7 8 )n a m e ( a 2 ,r e p l a c
/ / I fy o ur e q u i r et h eo r d e rt ob et h em e a nm p g / / t h ef o l l o w i n gc a nb ed o n e : e g e nm e a n 1=m e a n ( m p g ),b y ( r e p 7 8 )
e g e na x i s=a x i s 1 ( m e a n 1r e p 7 8 ) ,l a b e l ( r e p 7 8 )r
s c a t t e rm p gw e i g h t,b y ( a x i s )n a m e ( a 1 ,r e p l a c e
MC o x S t a t a l i s t2 1 / 0 3 / 1 1
www.survey-design.com.au/Stata Graphs.html
37/42
12/28/12
t w o w a y( s c a t t e rm p gw e i g h t ,y a x i s ( 12 )x l a b e l ( y s c a l e (a x i s ( 2 ,a l t ) ) y l a b e l ( 1 0" ` m 1 0 ' " 2 0" ` m 2 0 ' " 3 0" ` m 3 0 ' " 4 0" ` m 4 0 ' " , a x i s ( 2 ) ) ), b y ( f o r ,t i t l e (" k m / L " , o r i e n t a t i o n ( v e r t i c a l ) s i z e ( m e d )p o s i t i o n ( 3 ) )x r e s c a l ec o m p a c t )
/ / A d d i n gt i t l e st ot h el e f th a n ds i d eo ft h eg
s y s u s ea u t o ,c l e a r
f i l l i nr e p 7 8f o r e i g n t w o w a ys c a t t e rp r i c em p g ,b y ( f o r e i g nr e p 7 8 ,c o e g e ng r o u p=g r o u p ( f o r e i g nr e p 7 8 ) l a b e ld e f i n eg r o u p1" P o o r "/ / / 2" F a i r " 3" A v e r a g e " 4" G o o d " 5" E x c e l l e n t " 6 "" 7 "" 8 "" 9 "" 1 0"" / / / / / / / / / / / / / / / / / / / / / / / /
l a b e lv a l u eg r o u pg r o u p/ / /
g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 5 ] . s t y l e . e d i t s t y l e
g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 5 ] . a s _ t e x t b o x . s e t s g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 1 0 ] . a s _ t e x t b o x . s e t
g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 5 ] . t e x t={ } g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 5 ] . t e x t . A r r p u s hD o
g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 1 0 ] . t e x t={ } g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 1 0 ] . t e x t . A r r p u s hF
g r _ e d i tp l o t r e g i o n 1 . r 1 t i t l e [ 5 ] . _ s e t _ o r i e n t a t i o
F o ri n s t r u c t i o n so nh o wt od ot h i si nt h eS t a t ag r a p h i c se d i t o r s e eS t a t aJ o u r n a lV o l9N o4 F o ri n f o r m a t i o no nh o wt os u b s c r i b et ot h eS t a t aJ o u r n a l p l e a s ec o n t a c tu s .
www.survey-design.com.au/Stata Graphs.html
38/42
12/28/12
/ / C o m b i n i n gm a n yg r a p h so nap a g e s y s u s ea u t o ,c l e a r g r a p hd r o p_ a l l f o r e a c hio fv a r l i s t_ a l l{ c a p t u r ec o n f i r m n u m e r i cv a r i a b l e` i ' i f_ r c = = 0{ h i s t o g r a m` i ' ,n a m e ( ` i ' ) l o c a lz" ` z '` i ' " } } g r a p hc o m b i n e` z '
/ / P r o d u c i n gas i m i l a rp l o tt ot h ed e n s i t yp l o t h t t p : / / w w w . s u r v e y d e s i g n . c o m . a u / U s e r g r a p h s . h t m u s i n gS t a t a1 2 ' sn e wc o n t o u rp l o t
s u m m a r i z ep r i c em p g c l o n e v a rx=m p g c l o n e v a ry=p r i c e r e p l a c ex=( x 0 )/( 5 0 0 ) r e p l a c ey=( y 0 )/( 2 0 0 0 0 0 ) m y l a b e l s0 ( 1 0 ) 5 0 ,m y s c a l e ( ( @ 0 )/( 5 0 0 ) )l o c a m y l a b e l s0 ( 5 0 0 0 ) 2 0 0 0 0 ,m y s c a l e ( ( @ 0 )/( 2 0 0 0 0 k e e pxy s a v e" x y . d t a " ,r e p l a c e *2 .G e n e r a t ea1 0 0 x 1 0 0g r i d s p g r i d ,s h a p e ( h e x a g o n a l )x d i m ( 1 0 0 ) x r a n g e ( 01 )y r a n g e ( 01 ) d o t sr e p l a c e c e l l s ( " 2 D G r i d C e l l s . d t a " ) p o i n t s ( " 2 D G r i d P o i n t s . d t a " ) / / / / / / / / / / / /
www.survey-design.com.au/Stata Graphs.html
39/42
12/28/12
/ / M a n d e l b r o tS e t E x a m p l et a k e nf r o mS t a t aN e w s h t t p : / / w w w . s t a t a . c o m / s t a t a n e w s / s t a t a n e w s . 2 7 . 4 P l e a s en o t et h a to nm yc o m p u t e rt h i st o o k3h o
c l e a ra l l
/ / G e n e r a t i n gS u r v i v a lc u r v e sf r o mt w od a t as e t
u s eh t t p : / / w w w . s t a t a p r e s s . c o m / d a t a / r 1 1 / d r u g 2 . s t s e t s t s,n a m e ( a 1 ,r e p l a c e ) s t sg e n e r a t en e w v a r= s s e to b s` = _ N + 1 ' r e p l a c en e w v a r = 1i nl r e p l a c e_ t = 0i nl r e p l a c ed i e d = 0i nl r e p l a c es = 1i nl
www.survey-design.com.au/Stata Graphs.html
40/42
12/28/12
u s eh t t p : / / w w w . s t a t a p r e s s . c o m / d a t a / r 1 1 / s t a n 3 . r e p l a c et 1 = t 1 / 4 0 s t s e tt 1 ,i d ( i d )f a i l u r e ( d i e d ) s t s,n a m e ( a 1 a ,r e p l a c e ) s t sg e n e r a t en e w v a r 1= s s e to b s` = _ N + 1 ' r e p l a c en e w v a r 1 = 1i nl r e p l a c e_ t = 0i nl k e e pn e w v a r 1_ t r e n a m e_ t_ t 1 m e r g e1 : 1_ nu s i n gc : / s 1
t w o w a y( l i n e n e w v a r 1_ t 1 ,s o r tc ( s t a i r s t e p ) ) ( l i n e n e w v a r_ t ,s o r tc ( s t a i r s t e p ) ) , y l a b e l ( 0 ( . 2 ) 1 )n a m e ( a 2 ,r e p l a c e )y t i t l e ( " " ) l e g e n d ( l a b e l ( 1d a t as e t1 )l a b e l ( 2d a t as e t2 )
/ / G e n e r a t i n gS u r v i v a lc u r v ew i t hc o n f i d e n c ei n
u s eh t t p : / / w w w . s t a t a p r e s s . c o m / d a t a / r 1 1 / d r u g 2 . s t s e t s t sg e nk m = s s t sg e nu b = u b ( s ) s t sg e nl b = l b ( s )
/ / T h e ns o m e t h i n ga l o n gt h el i n e s : t w o w a y( r a r e au bl b_ t ,s o r t )( l i n ek m_ t ,s o r
F r o mS t a t a l i s t : F r i ,2 5F e b2 0 1 1 P h i lC l a y t o n
/ / G e n e r a t i n gS u r v i v a lc u r v ew i t hl i n e sa ts p e c
u s eh t t p : / / w w w . s t a t a p r e s s . c o m / d a t a / r 1 2 / d r u g 2 , s t sg e n e r a t en e w v a r 1= s s t sg e n e r a t el b=l b ( s ) s t sg e n e r a t eu b=u b ( s ) g e nk k = n e w v a r 1i fs t u d y t i m e = = 2 0 s o r tk k l o c a la 1 = k k [ 1 ]
www.survey-design.com.au/Stata Graphs.html
41/42
12/28/12
An Excellent book on Stata graphs is: A Visual Guide to Stata Graphics 3rd Edition Mitchell M. Price: $ 65 + Postage* More details Details (Now in stock)
www.survey-design.com.au/Stata Graphs.html
42/42