Sei sulla pagina 1di 42

12/28/12

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

Order form Contact us

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

c l e a r i n p u ti n p u tl o w h i g h m e a n 1 1 . 6 2 . 2 1 . 9 2 1 . 5 2 . 3 1 . 9 5 2 . 9 0 . 0 11 . 9 3 1 . 0 2 . 9 1 . 9 4 0 . 5 3 . 2 1 . 9 e n d l a b e lv a ri n p u t" I n p u t " l a b e lv a rl o w " L o wv a l u e "

www.survey-design.com.au/Stata Graphs.html

1/42

12/28/12

Stata Graphs - Examples

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

s c a t t e ryx , ` a x ' ` x ' ` y 's c h e m e ( s 2 m o n o )g r a p h r ( f

2/42

12/28/12

Stata Graphs - Examples

s c a t t e ryx , ` a x ' ` x ' ` y 's c h e m e ( s 2 m o n o )g r a p h r ( f

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

/ /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 u m m a r i z ep r i c e ,m e a n l o c a lm e a n=r ( m e a n ) k d e n s i t yp r i c e ,g e n ( xh ) l i n ehx ,| |a r e ahxi f x<` m e a n '

www.survey-design.com.au/Stata Graphs.html

3/42

12/28/12

Stata Graphs - Examples

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

t w o w a y/ / / ( a r e am a xx i fi n r a n g e ( x ,` = r ( p 1 ) ' ,` = r ( p 2 5 ) l c o l o r ( b g )f c o l o r ( g r e e n ) f i n t e n s i t y ( i n t e n 2 0 ) ) ( a r e a m a xx i fi n r a n g e ( x ,` = r ( p 7 5 ) ' ,` = r ( p 9 l c o l o r ( b g )f c o l o r ( g r e e n )f i n t e n s i t y ( i n t e n 2 0 ) ) ( l i n exm p g ) , l e g e n d ( o f f )p l o t r e g i o n ( m a r g i n ( z e r x t i t l e ( " M P G " ) x l a b e l ( 1 0` = r ( p 1 ) '` = r ( p 2 5 ) '` = r ( p 7 5 ) '` = r ( p 9 9 y s c a l e ( r a n g e ( 0` = _ N ' ) )

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

Stata Graphs - Examples

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

s t a t s b y" s um p g "m e a n = r ( m e a n )m a x = r ( m a x )m i n = r / /h e r ek=1 ;t u n et oc h o i c e g e nm e a n p s d=m e a n+s d

www.survey-design.com.au/Stata Graphs.html

5/42

12/28/12

Stata Graphs - Examples


g e nm e a n m s d=m e a n-s d

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

g e nd o u b l ed a t e = d o f c ( c l o c k ( t i m e , " D M Yh m " ) ) f o r m a td a t e% t d c o l l a p s e ( p 1 )p 1 = p r i c e( p 2 5 )p 2 5 = p r i c e/ / / ( p 5 0 )p 5 0 = p r i c e( p 7 5 )p 7 5 = p r i c e / / / ( p 9 9 )p 9 9 = p r i c e ,b y ( d a t e ) g e nu a v = p 7 5 -1 . 5 * ( p 2 5 p 7 5 ) g e nl a v = p 7 5 +1 . 5 * ( p 2 5 p 7 5 ) g e nc o l = 2 r e p l a c ec o l = ( p 5 0 p 5 0 [ _ n 1 ] ) > 0i f_ n ! = 1

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

Stata Graphs - Examples

/ /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=( l 4 [ _ N ] l 3 [ _ N ] ) / 2+l 3 [ _ N ] l o c a ly a x i s` " ` y a x i s '` m i d '" o t h e rs e r v i c e s " " '

l o c a lm i d=( l 5 [ _ N ] l 4 [ _ N ] ) / 2+l 4 [ _ N ] l o c a ly a x i s` " ` y a x i s '` m i d '" p r o f e s s i o n a ls e r v

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 )

/ /4 5d e g r e el i n e s y s u s ea u t o ,c l e a r s u mw e i g h t ,m e a n o n l y l o c a lm a x=r ( m a x ) s u m m a r i z ep r i c e ,m e a n o n l y i fr ( m a x )>` m a x '{ l o c a lm a x=r ( m a x ) }

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 )

#d e l i m i t; t w o w a ys c a t t e ryx| | f u n c t i o ny = 1 / 8 * ( 4 * s i n ( 1 * x ) 4 * s i n (7 * x ) ) ,r a n g f u n c t i o ny = 1 / 8 * ( 4 * s i n ( 1 * x ) 4 * s i n ( 1 * x ) ) ,r a n g l e g e n d ( c o l s ( 1 )l a b e l ( 1" s a m p l e dv a l u e s " ) l a b e l ( 2" s u mo fh i g hf r e q u e n c yc o m p o n e n t s " ) l a b e l ( 3" s u mo fl o wf r e q u e n c yc o m p o n e n t s " ) ) t i t l e ( " R e c o n s t r u c t e ds i g n a lf o ry = s i n ( x )s a m p #d e l i m i tc r

12/28/12

Stata Graphs - Examples

/ /h 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 yh i s t o g r a mm p gi f! f o r e i g n , s t a r t ( 1 0 )w i d t h ( 2 )f r e qb f c o l o r ( n o n e )b l c o l o r ( | |h i s t o g r a mm p gi ff o r e i g n ,f r e qs t a r t ( 1 0 )w i b f c o l o r ( n o n e )b l c o l o r ( b l u e ) 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 ( 2 )

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

l o c a ll a b e l` l a b e l ' ` = s t r i n g ( x [ ` i ' ] , " % 8 . 2 f " } h i s t o g r a mm p g,b i n ( 6 )x l a b e l ( ` l a b e l ' )

www.survey-design.com.au/Stata Graphs.html

9/42

12/28/12

Stata Graphs - Examples

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

t w o w a y( h i s t o g r a mm p g ,w i d t h ( ` w i d t h ' )f r e q )/ / ( f u n c t i o ny= ` w i d t h ' * ` = _ N ' *n o r m d e n ( x ,` r ( m e a n r a n g e ( 1 04 5 ) ) ,n a m e ( a 1 ,r e p l a c e )y t i t l e ( " f r e q u x t i t l e ( " m p g " )l e g e n d ( o f f )

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

Stata Graphs - Examples

t i t l e ( " S c a t t e rw i t hR e g r e s s i o nL i n ea n dC o n f i d , s i z e ( 4 . 8 )m a r g i n ( t = 0b = 1 . 5 )s p a n ) c a p t i o n ( " g r a p h 4 6 " ,b o xs i z e ( v s m a l l ) p o s i t i o n ( 5 ) ; # d e l i m i tc r

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

Stata Graphs - Examples


" 0 1 / 0 7 / 2 0 0 6 " " 0 1 / 0 8 / 2 0 0 6 " " 0 1 / 0 9 / 2 0 0 6 " " 0 1 / 1 0 / 2 0 0 6 " " 0 1 / 1 1 / 2 0 0 6 " " 0 1 / 1 2 / 2 0 0 6 " " 0 1 / 0 1 / 2 0 0 7 " " 0 1 / 0 2 / 2 0 0 7 " " 0 1 / 0 3 / 2 0 0 7 " " 0 1 / 0 4 / 2 0 0 7 " " 0 1 / 0 5 / 2 0 0 7 " " 0 1 / 0 6 / 2 0 0 7 " " 0 1 / 0 7 / 2 0 0 7 " " 0 1 / 0 8 / 2 0 0 7 " " 0 1 / 0 9 / 2 0 0 7 " " 0 1 / 1 0 / 2 0 0 7 " " 0 1 / 1 1 / 2 0 0 7 " " 0 1 / 1 2 / 2 0 0 7 " e n d 9 0 0 9 3 3 . 3 3 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 5 0 1 0 8 7 . 5 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 7 5 1 2 0 0 1 2 0 0 1 3 0 0 1 4 0 0 1 4 0 0

g e nd a t e 2 = d a t e ( d a t e , " D M Y " ) g e nm o n t h l a b e l=s u b s t r ( " J F M A M J J A S O N D " ,m o n t h ( d a t e

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 )

s c a t t e rxt i m e 1 ,x l a b ( ` t 1 ' ( ` t 2 ' ) ` t 3 ', f o r m a t ( % t c d d m o n c c y y _ h h : m m )a n g l e ( 4 5 ) )x t i t l e ( "


* ( N i c kC o x-S t a t al i s t , M o n d a y ,2 3J a n2 0 1 1 )

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

Stata Graphs - Examples

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

s y s u s ea u t o ,c l e a r / / c r e a t i n gs o m ed a t a g e nm p g 1 = m p g < 3 0 r e c o d em p g( 1 / 2 5 = 1 )( 2 6 / 3 0 = 2 )( 3 1 / 5 0 = 3 ) ,g e n ( m s a v ek k ,r e p l a c e l i s t c a p t u r ee r a s ek k 1 . d t a l o c a lz = 1 f o r e a c hio fv a r l i s tf o rm p g 1m p g 2{ u s ek k ,c l e a r k e e p` i ' c o n t r a c t` i ' e g e nt o t a l = t o t a l ( _ f r e q ) r e p l a c e_ f r e q = _ f r e q / t o t a l g e n e r a t et y p e = ` z ' c a p t u r ea p p e n du s i n gk k 1 s a v ek k 1 ,r e p l a c e l o c a l+ + z } d r o pm p *f o r b y s o r tt y p e:g e nt 1 = _ n l a b e ld e f i n ek k1c a t 12c a t 23c a t 3 l a b e lv a l u et y p ek k

g r a p hb a r( m e a n )_ f r e q,o v e r ( t 1 , s o r t ( _ f r e q )) a s y v a rl e g e n d ( o f f )n o f i l ly t i t l e ( " P r o p o r t i o n " )

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

Stata Graphs - Examples

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

Stata Graphs - Examples


s y s u s ea u t o ,c l e a r

g r a p hb a r m p g g e a r ,s h o w y v a r s/ / / y v a r o p t i o n s ( r e l a b e l ( 1" M P G "2 " G E A RR A T I O " ) )l e e x i t

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

l a bd e fo r i g i n0" E u r o p ed el ` = c h a r ( 1 4 6 ) ' O u e s t 1" A s i ed el ` = c h a r ( 1 4 6 ) ' E s t " ,m o d i f y g r a p hh b a rm p gt r u n kt u r n ,o v e r ( f o r e i g n )/ / / l e g e n d ( r o w ( 1 )s p a n )s t a c kn a m e ( t w o , r e p l a c e ) e x i t

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

Stata Graphs - Examples

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={ } 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 . A r r p u l o c a l+ + i 1

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={ } 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 . A r r p u l o c a l+ + i 1 } e x i t

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

s y s u s ec e n s u s ,c l e a r g r a p hb a r( s u m )m a r r id i v ,o v e r ( r e g i o n )s t a c k m o r e c o l l a p s e( s u m )m a r r id i v ,b y ( r e g i o n ) g e nr a t i o=d i v/( m a r r i+d i v ) g e ns r a t i o=s t r i n g ( r a t i o ," % 4 . 3 f " ) g e nm a r r i P d i v=m a r r i a+d i v

t w o w a yb a rm a r r i ar e g i o n ,b a r w ( 0 . 6 )b a s e ( 0 )/ / | |r b a rm a r r i am a r r i P d i vr e g i o n ,b a r w ( 0 . 6 )x l a | |s c a t t e rm a r r i P d i vr e g i o n ,m s ( n o n e )m l a ( s r a t l e g e n d ( o r d e r ( 1" m a r r i a g e s "2" d i v o r c e s " ) )/ / / y l a ( 04 e 5" 4 0 0 " 8 e 5" 8 0 0 "1 2 e 5" 1 2 0 0 ",a n g ( h y s c ( r ( .1 4 e 5 ) )

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

g e n e r a t ey e a r 2=y e a r+0 . 3 g e n e r a t ey e a r 3=y e a r-0 . 3

g e n e r a t ea v e r a g e=( i n v e s t+m v a l u e+k s t o c k ) t w o w a yb a ri n v e s ty e a r 3 ,b a r w ( 0 . 3 )| |/ / / b a rm v a l u ey e a r ,b a r w ( 0 . 3 ) | |/ / / b a rk s t o c ky e a r 2,b a r w ( 0 . 3 ) | |/ / / l i n ea v e r a g ey e a r,n a m e ( a 2 ,r e p l a c 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

Stata Graphs - Examples


s u m mp r i c ei ff o r = = 1&! m i s s i n g ( r e p 7 8 ) l o c a la v e 2 = r ( m e a n ) s e to b s` = _ N + 3 ' r e p l a c ef o r = 0i n3 r e p l a c er e p 7 8 = 6i n3 r e p l a c ef o r = 0i n2 r e p l a c er e p 7 8 = 7i n2 r e p l a c ep r i c e = ` a v e 1 'i n2 r e p l a c ef o r = 1i nl r e p l a c er e p 7 8 = 7i nl r e p l a c ep r i c e = ` a v e 2 'i nl

l a b e ld e f i n ek k11223344556" " 7" l a b e lv a l u er e p 7 8k k

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

g r a p hb a r( a s i s )m a r k , o v e r ( i d)o v e r ( p o s , l a b y l a b e l (0 ( 1 0 ) 9 0 ,a n g l e ( 4 5 ) ) 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 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 nO l y m p i c s2 0 1 2 "" J a v e l n o f i l ly t i t l e ( " M e t r e s " ) b a r ( 1 ,c o l o r ( g o l d ) ) / / / b a r ( 6 ,c o l o r ( g o l d ) ) / / / b a r ( 2 ,c o l o r ( g s 1 3 ) ) / / / b a r ( 7 ,c o l o r ( g s 1 3 ) ) / / / b a r ( 3 ,c o l o r ( s i e n n a ) ) / / / b a r ( 8 ,c o l o r ( s i e n n a ) ) / / / b a r ( 4 ,c o l o r ( c y a n ) ) b a r ( 9 ,c o l o r ( c y a n ) ) b a r ( 5 ,c o l o r ( p i n k ) ) b a r ( 1 0 ,c o l o r ( p i n k ) ) / / / / / / / / /

e x i t

www.survey-design.com.au/Stata Graphs.html

17/42

12/28/12

Stata Graphs - Examples

/ /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 n 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 ew1" W o m e n "0" M e n " l a b e lv a l u eww g e ng 1 = m a r ki f_ n < 6 g e ng 2 = m a r ki f_ n > 5 l i s t

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

Stata Graphs - Examples


l a b e lv a l u ei d k k r e p l a c ei d = i d + 1i fi d > 5 s e to b s` = _ N + 1 ' r e p l a c ei d = 6i nl

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

( b a rm a r ki di fi d = = 2 , b a r w i d t h ( . 4 ) c o l o r ( c ( b a rm a r ki di fi d = = 3 , b a r w i d t h ( . 4 ) c o l o r ( c ( b a rm a r ki di fi d = = 4 , b a r w i d t h ( . 4 ) c o l o r ( c ( b a rm a r ki di fi d = = 5 , b a r w i d t h ( . 4 ) c o l o r ( c ( b a rm a r ki di fi d = = 7 , b a r w i d t h ( . 4 ) c o l o r ( r ( b a rm a r ki di fi d = = 8 , b a r w i d t h ( . 4 ) c o l o r ( r ( b a rm a r ki di fi d = = 9 , b a r w i d t h ( . 4 ) c o l o r ( r ( b a rm a r ki di fi d = = 1 0 ,b a r w i d t h ( . 4 ) c o l o r ( r ( b a rm a r ki di fi d = = 1 1 ,b a r w i d t h ( . 4 ) c o l o r ( r ( s c a t t e rm a r ki d , m l a b e l ( m a r k )m s y m b o l ( n o n e ) m l a b p o s i t i o n ( 1 2 ) ) ,l e g e n d ( o f f ) t i t l e ( " L o n d o nO l y m p i c s2 0 1 2 "" J a v e l i n " ) p l o t r e g i o n ( m a r g i n ( l = 5b = 0 ) ) e x i t

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

g r a p hb a r ` v a r l i s t ' ,o v e r ( ` o v e r ' ) b l a b e l ( b a r ,p o s i t i o n ( b a s e )g a p ( * 2 2 . 5 )f o r m a t ( % s i z e ( l a r g e )c o l o r ( b l a c k ) ) l o c a lx = 1 f o r e a c hio fl o c a lk k{ s u m m` v a r l i s t 'i f` o v e r ' = = ` i ' l o c a la 1 = s t r i n g ( r ( m e a n ) , " % 8 . 0 f " )

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 [ ` x ' ] . t e x t={ } 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 [ ` x ' ] . t e x t . A r r p u s l o c a l+ + x } e n d

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 '

/ /m a p1 . . 5>3 . . 7t oe n s u r eo f f s e tf r o mf o r r e p l a c er e p 7 8=r e p 7 8+2

s t a c kp r i c ef o r e i g nw e i g h tr e p 7 8 ,i n t o ( d a t af o / /b r i n gb a c kl a b e l s l a b e ld e f_ s t a c k1" ` l 1 ' "2" ` l 2 ' " l a b e lv a l_ s t a c k_ s t a c k

www.survey-design.com.au/Stata Graphs.html

19/42

12/28/12

Stata Graphs - Examples

l a b e lv a l_ s t a c k_ s t a c k d o` m y l a b e l ' / /r e v e r s er e p 7 8 -o f f s e ti nl a b e l s l a b e ld e fo r i g i n3" 1 "4" 2 "5" 3 "6" 4 "7" 5 " l a b e lv a lf o r e i g no r i g i n / /n o w . . . g r a p hh b o xd a t a ,o v e r ( f o r e i g n )

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

g r a p hh b o xw a g e l o g 1 0 ,o v e r ( i n d ,s o r t ( 1 ) )n o o u t s i d e t i t l e ( " H o u r l yw a g e ,1 9 8 8 ,w o m a na g e d3 4 4 6 " ,s p a n ) n o t e ( " S o u r c e : 1 9 8 8d a t af r o mN L S ,U . S .D e p t .o fL a b o " B u r e a uo fL a b o rS t a t i s t i c s " , s p a n ) e x i t

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

i fr ( N ) > = 1{ r e g r e s sm p gw e i g h ti ff o r = = ` i '&r e p 7 8 = = ` i 1 p a r m e s t ,n o r e s t o r e 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 ) g e nf o r = ` i ' g e nr e p 7 8 = ` i 1 ' c a p t u r ea p p e n du s i n g` f i l e ' s a v e` f i l e ' ,r e p l a c e l o c a lk k = ` i 1 ' + 1 } } } / / e n dl o o p

www.survey-design.com.au/Stata Graphs.html

20/42

12/28/12

Stata Graphs - Examples

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

t w o w a y( s c a t t e ro r d e re s t,m s y m b o l ( n o n e ) ) / / ( s c a t t e ro r d e re s t) / / ( r c a pm i n 9 5m a x 9 5o r d e r ,h o r i z o n t a l) , / / y l a b e l ( 0 / 9 ,v a l u e l a b e la n g l e ( 0 ) )l e g e n d ( o f f )y x t i t l e ( " C o e f f i c i e n t sa n dc o n f i d e n c ei n t e r v a l " )

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

l o c a lf=f a c t o r [ ` a ' ] l o c a lt x t` " ` t x t 't e x t ( ` a '1" ` i ' " ,s i z e ( * ` f ' )

21/42

12/28/12

Stata Graphs - Examples

l o c a lt x t` " ` t x t 't e x t ( ` a '1" ` i ' " ,s i z e ( * ` f ' ) c a p t u r e` + + a ' }

s c a t t e rn u m b e rx ,m s y m b o l ( i ) / / / x t i t l e ( " " )x s c a l e ( o f f ) / / / y t i t l e ( " " )y s c a l e ( o f f ) / / / p l o t r e g i o n ( l s t y l e ( n o n e ) )/ / / ` t x t ' * -e n de x a m p l ee n d s / / c a l l sp r o g r a m" s "a b o v e

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

t w o w a y / / / ( l i n e m p gp r i c ei ff o r = = 0 )/ / / ( l i n e m p gp r i c ei ff o r = = 1 )/ / / , l e g e n d (l a b e l ( 2" f o r e i g n ")l a b e l ( 1" d o m e s t

www.survey-design.com.au/Stata Graphs.html

22/42

12/28/12

Stata Graphs - Examples

, l e g e n d (l a b e l ( 2" f o r e i g n ")l a b e l ( 1" d o m e s t

/ /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 rm p g 3r e p 7 8i ff o r = = 0&r e p 7 8 = = 1, f c o l o r ( g l c o l o r ( g r e e n )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ( b a r m p g 3r e p 7 8i ff o r = = 0&r e p 7 8 = = 2, f c o l o r ( l c o l o r ( b l u e )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ( b a r m p g 3r e p 7 8i ff o r = = 0&r e p 7 8 = = 3, f c o l o r ( l c o l o r ( r e d )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ) ) ( b a r m p g 3r e p 7 8i ff o r = = 0&r e p 7 8 = = 4, f c o l o r ( l c o l o r ( p i n k )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ) ( b a r m p g 3r e p 7 8i ff o r = = 0&r e p 7 8 = = 5, f c o l o r ( l c o l o r ( c y a n )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ) x t i t l e ( M P G )x l i n e ( 0 ) / / / t i t l e ( " F o r e i g nm a d ec a r s " )l e g e n d ( o f f )n a m e ( k 1

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 (

l c o l o r ( g r e e n )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ( b a r m p g 3r e p 7 8i ff o r = = 1&r e p 7 8 = = 2, f c o l o r ( l c o l o r ( b l u e )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ) ( b a r m p g 3r e p 7 8i ff o r = = 1&r e p 7 8 = = 3, f c o l o r ( l c o l o r ( r e d )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ) ) ( b a r m p g 3r e p 7 8i ff o r = = 1&r e p 7 8 = = 4, f c o l o r ( l c o l o r ( p i n k )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ) ( b a r m p g 3r e p 7 8i ff o r = = 1&r e p 7 8 = = 5, f c o l o r ( l c o l o r ( c y a n )b a r w i d t h ( . 5 ) h o r i z y s c a l e ( n o l i n e ) x t i t l e ( M P G )x l i n e ( 0 )y s c a l e ( o f f )/ / / t i t l e ( " D o m e s t i c a l l ym a d ec a r s " )l e g e n d ( o f f )n a

g r a p hc o m b i n ek 1k 2 ,r o w ( 1 ) i m a r g i n ( 1 0000 ) t i t l e ( " D i f f e r e n c ef r o ma v e r a g eo fm p gf o r"/ / " F o r e i g no rD o m e s t i c a l l yp r o d u c e dc a r s """)

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

c l e a r s e to b s1 0 0 0 e g e ni d=s e q ( ) ,b l o c k ( 1 0 ) e g e nt i m e=s e q ( ) ,t o ( 1 0 ) f o r m a tt i m e% t q g e nt i m e 2 = s t r i n g ( t i m e ," % t q " ) g e ns e c t o r=c e i l ( 3*r u n i f o r m ( ) ) b y s o r ti d :r e p l a c es e c t o r = s e c t o r [ 1 ] l a b e ld e f i n es e c t o r1" S E C T O R1 "2" S E C T O R2 " l a b e lv a l u e ss e c t o rs e c t o r g e ns a l e s = e x p ( r n o r m a l ( ) ) * 1 0 0 0 g e ns a l e s 2 = 2 * s a l e s

www.survey-design.com.au/Stata Graphs.html

23/42

12/28/12

Stata Graphs - Examples


/ / r e s h a p i n gt h ed a t a g e no b s=_ n r e n a m es a l e ss a l e s 1 r e s h a p el o n gs a l e s ,i ( o b s )j ( w h i c h )

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

/ /S t a c k e db a rg r a p h-p e r c e n t a g e s y s u s ea u t o ,c l e a r g e na = _ n c o l l a p s ea ,b y ( f o rr e p 7 8 ) d r o pi fm i s s i n g ( f o r ,r e p 7 8 ) b y s o r tf o rr e p 7 8 :g e ns u m=s u m ( a ) r e p l a c ea = 0i fa = = . l e v e l s o fr e p 7 8 ,l o c a l ( a a ) g e n e r a t ec s u m = . f o r e a c hio fl o c a la a{ s u m mai f` i ' = = r e p 7 8 r e p l a c ec s u m = a / ` r ( s u m ) 'i f` i ' = = r e p 7 8 }

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

Stata Graphs - Examples

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

l o c a lt o t a l" ` z 1 '` z 2 '` z 3 '` z 4 '` z 5 '` z 6 '` z 7 l o c a ls i z e = 4 0 l o c a lt 1 / / a d j u s tf o rl e n g t ho fl i n e

f o r v a l u e si = 1 / 1 2{ / / i n c r e a s et l o c a la ` i ':p i e c e` i '` s i z e 'o f" ` t o t a l ' " /

l o c a lt 1 ` " ` t 1 '` = c h a r ( 3 4 ) +" ` a ` i ' ' " + c h a r ( 3 4 ) }

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

# d e l i m i t l a b e ld e f i n eo r i g i n0` " g r o u p 0g r o u p 0g r o u p 0g g r o u p 0g r o u p 0g r o u p 0g r o u p 0g r o u p 0 " ' ,m o d i f y ;

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

l o c a lr e l a b e l s l o c a lr e l a b e l s 1 l e v e l s o ff o r ,l o c a l ( g r o u p s ) l o c a ls _ l e n = 2 0 / / C h a n g et or e q u i r e dl e n g t h f o r e a c hgo fl o c a lg r o u p s{ l o c a ll a b e l:l a b e lo r i g i n` g ' l o c a ll e n:l e n g t hl o c a ll a b e l

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

Stata Graphs - Examples

e x i t

/ /C o m b i n i n gg r a p h sa n di n c l u d i n gac o m m o nl e g e n d / / t h i su s e st h eu s e rw r i t t e nc o m m a n d" g r c 1 l e g "w h i

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 a p hh b a ra 1 a 6i fq u e s t = = 4 ,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 ( s p a nr o w s ( 1 )l a b e l ( 1" M i s s i n g " )l a b e l l a b e l ( 3" R a r e l y " )l a b e l ( 4" O c c a s i o n l y " )l a b e l ( 5 l a b e l ( 6" A l w a y s " )s i z e ( s m a l l ) ) y l i n e ( 2 04 06 08 0, l w i d t h ( 0 . 2 5)l c o l o r ( b l a c k ) )s a

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

t w o w a y/ / / ( s c a t t e rm p gr e p 7 8 ,y s c a l e ( r a n g e ( 05 0 ) ) ),y s i z e c o n t r a c tr e p 7 8f o r f i l l i nr e p 7 8f o r r e p l a c e_ f r e q = 0i fm i s s i n g ( _ f r e q ) s e to b s` = _ N + 2 ' t o s t r i n g_ f r e q ,r e p l a c e r e p l a c er e p 7 8 = 0i n` = _ N 1 ' r e p l a c er e p 7 8 = 0i n` = _ N ' r e p l a c ef o r = 1i n` = _ N 1 ' r e p l a c ef o r = 0i n` = _ N ' r e p l a c e_ f r e q = " f o r e i g nc a r s "i n` = _ N 1 '

www.survey-design.com.au/Stata Graphs.html

26/42

r e p l a c e_ f r e q = " f o r e i g nc a r s "i n` = _ N 1 ' r e p l a c e_ f r e q = " D o m e n s t i cc a r s "i n` = _ N '

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

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 2 + 8 '` g r _ e d i ta d d e d _ t e x t _ n e w=1 g r _ e d i ta d d e d _ t e x t _ r e c=1 g r _ e d i ta d d e d _ t e x t [ 1 ] . t e x t={ } g r _ e d i ta d d e d _ t e x t [ 1 ] . t e x t . A r r p u s h" M e a nm p gb

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 2 + 2 '` g r _ e d i ta d d e d _ t e x t _ n e w=2 g r _ e d i ta d d e d _ t e x t _ r e c=2 g r _ e d i ta d d e d _ t e x t [ 2 ] . t e x t={ } g r _ e d i ta d d e d _ t e x t [ 2 ] . t e x t . A r r p u s h" r e p 7 8 m p g c o l l a p s em p g ,b y ( r e p 7 8 ) l o c a lz = 2

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

t w o w a y/ / / s c a t t e rm p gr e p 7 8 ,/ / / t e x t ( 4 03" T h em e a nm p gi s : ` = s t r i n g ( ` = r ( m e a n ) ' , " % 8 t e x t ( 3 83" T h es dm p gi s :` = s t r i n g ( ` = r ( s d ) ' , " % 8 . 2 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

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

m e r g e1 : 1_ nu s i n g " C : \ P r o g r a mF i l e s \ s t a t a 1 1 \ l o c a ls i z e = " t i n y "

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

Stata Graphs - Examples

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

g e n e r a t e` n e w v ' = ( ( s t r p o s ( " ` = c ( a l p h a ) ' " , ` v a r l i r e p l a c e` n e w v ' = r e a l ( ` v a r l i s t ' )i f` n e w v ' = = 9 . 5 e n d

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

i n s h e e tu s i n g" c : / f a s tc a r . h t m l " ,c l e a r g e n e r a t er o w = . r e p l a c er o w = 1i fs t r p o s ( v 1 , " <T R > " ) r e p l a c er o w = s u m ( r o w ) k e e pi fs t r p o s ( v 1 ,"<T D B G C O L O R " ) s p l i tv 1 , p ( # ) s p l i tv 1 2 , p ( > ) k e e pr o wv 1 2 1 g e n e r a t eo b s = _ n b y s o r tr o w( o b s ) : g e nc o l = _ n

www.survey-design.com.au/Stata Graphs.html

29/42

12/28/12

Stata Graphs - Examples


b y s o r tr o w( o b s ) : g e nc o l = _ n r e n a m ev 1 2 1h e x g e n e r a t eh e x 1 = s u b s t r ( h e x , 1 , 1 ) g e n e r a t eh e x 1 a = s u b s t r ( h e x , 2 , 1 ) g e n e r a t eh e x 2 = s u b s t r ( h e x , 3 , 1 ) g e n e r a t eh e x 2 a = s u b s t r ( h e x , 4 , 1 ) g e n e r a t eh e x 3 = s u b s t r ( h e x , 5 , 1 ) g e n e r a t eh e x 3 a = s u b s t r ( h e x , 6 , 1 ) h e xh e x 1 ,n e w v ( d e c 1 ) h e xh e x 1 a ,n e w v ( d e c 1 a ) h e xh e x 2 ,n e w v ( d e c 2 ) h e xh e x 2 a ,n e w v ( d e c 2 a ) h e xh e x 3 ,n e w v ( d e c 3 ) h e xh e x 3 a ,n e w v ( d e c 3 a )

g e n e r a t ed e c 1 b = 1 6 * d e c 1 + d e c 1 a g e n e r a t ed e c 2 b = 1 6 * d e c 2 + d e c 2 a g e n e r a t ed e c 3 b = 1 6 * d e c 3 + d e c 3 a k e e pd e c 1 bd e c 2 bd e c 3 br o wc o l g e n e r a t ed e c 1 c = f l o o r ( d e c 1 b / ` d i v i d e ' ) * ` d i v i d e ' g e n e r a t ed e c 2 c = f l o o r ( d e c 2 b / ` d i v i d e ' ) * ` d i v i d e ' g e n e r a t ed e c 3 c = f l o o r ( d e c 3 b / ` d i v i d e ' ) * ` d i v i d e '

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

m e r g e1 : 1_ nu s i n g " C : \ P r o g r a mF i l e s \ s t a t a 1 1 \ a d o s u m m a r i z el e n g t h g e ny = 9 0 + ( ( ( _ N _ n + 1 ) 9 0 ) ) / ( _ N / r ( m a x ) ) / / 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 / 9 l o c a lz = 1 f o r e a c hio fl o c a la{

i f` z ' = = 1{ l o c a lz z= / / / ` " ( 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 e

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

Stata Graphs - Examples

/ /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 yb a r e s t i m a t ea x i s ,b a s e ( 1 )h o r i z o n t a l 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 l e g e n d ( o r d e r ( 1" p o i n te s t i m a t e " 2" 9 5 %c o n f .i n t . " )p x t i t l e ( " o d d sr a t i o " )y t i t l e ( " " )

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

t w o w a yb a r e s t i m a t ea x i s ,b a s e ( 1 )h o r i z o n t a l 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 l e g e n d ( o r d e r ( 1" p o i n te s t i m a t e " 2" 9 5 %c o n f .i n t . " )p o s ( 6 ) ) x t i t l e ( " o d d sr a t i o " )y t i t l e ( " " )s c h e m e ( c o l o r )


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

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

l e g e n d ( o r d e r ( 1" p o i n te s t i m a t e " 2" 9 5 %c o n f .i n t . " )p x t i t l e ( " o d d sr a t i o " )y t i t l e ( " " )

/ / P a i r e dP l o t s y s u s ea u t o ,c l e a r g s o r tm p g k e e pi n1 / 2 5 r e p l a c em p g=r o u n d ( m p g ) g e nr a n k 1=_ n e g e nr a n k 2=r a n k ( p r i c e ) g e nb y t eo n e=1 g e nb y t et w o=2 g e nl e f t=0 . 4

t w o w a y ( p c s p i k er a n k 1o n er a n k 2t w o , x l a b e l ( n o n e )x s c a l e ( n o l i n er a n g e ( 0 . 32 . 3 ) )x t i y s c a l e ( r ( 1. )r e v e r s eo f f )y l a b e l ( ,n o g r i d ) ) ( s c a t t e rr a n k 1o n e ,m l a b e l ( m p g )m l a b p o s ( 9 )m s ( ( s c a t t e rr a n k 2t w o ,m l a b e l ( p r i c e )m l a b p o s ( 3 )m (s c a t t e rr a n k 1l e f t ,m l a ( m a k e )m l a b p o s ( 3 )m s ( t e x t ( 0 . 51" m p g ,U Sg a l l o n s " ) t e x t ( 0 . 52" p r i c e,{ cS | } U Si n1 9 7 8 " ) l e g e n d ( o f f )g r a p h r e g i o n ( c o l o r ( w h i t e ) ) )

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

Stata Graphs - Examples

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

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

s c a t t e rw _ a f t e rw _ b e f o r ew _ b e f o r e , / / / m s y m b o l ( O hi )c ( .l )l c ( n o n eg s 1 2 ) / / / s o r t ( w _ b e f o r e )y l a ( ,a n g ( h ) ) / / / y t i t l e ( "W e i g h ta f t e rt r e a t m e n t ,l b " ) / / / b y ( t r e a t m e n t ,r o w ( 1 )n o t e ( " w e i g h t ,l b " )l e g e n d

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

t w o w a y/ / / p c s p i k ew _ b e f o r eo n ew _ a f t e rt w oi fw _ b e f o r e< l c o l o r ( g s 1 2 )| | p c s p i k ew _ b e f o r eo n ew _ a f t e rt w oi fw _ b e f o r e> l w ( * 1 . 2 )l c o l o r ( g s 2 )x l a ( 1"b e f o r e "2" a f t e r x t i t l e ( " " )y l a ( ,n o g r i da n g ( h ) )y t i t l e ( " w e i g h t b y ( t r e a t m e n t ,r o w ( 1 )n o t e ( " " )l e g e n d ( o f f )n o i x

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

Stata Graphs - Examples

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

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

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

t w o w a y p c a r r o ww _ b e f o r eo r d e r 1w _ a f t e ro r d e r 1 ,p s t y l e | |s c a t t e rw _ b e f o r eo r d e r 1 ,p s t y l e ( p 1 )m s ( o ) x l a ( n o n e )x t i t l e ( " " )y l a ( ,a n g ( h ) )y t i t l e ( " w e i b y ( t r e a t m e n t ,r o w ( 1 )n o t e ( " " )l e g e n d ( o f f ) )

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

Stata Graphs - Examples

( l i n e xt y p ei ff o r = = 0 ,l c o l o r ( b l u e )c ( L ) ) ,/ x l i n e ( 1 / 1 2 ) / ` a 'y s c a l e (o f fr a n g e ( 01 . 1 ) )x s c a l e ( o f f ) / t e x t ( 00" M I N " ) t e x t ( 10" M a x " ) / g r a p h r e g i o n ( m a r g i n ( t = 2 0b = 2 0 ) )l e g e n d ( o f f )

/ / U s i n gA r r o w s * * T h ee x a m p l eb e l o wu s e sd a t af r o m : h t t p : / / w w c d" C : \ " i n s h e e tu s i n g" t e m p e r a t u r ed a t a . c s v " ,c l e a r / s e tm o r eo f f k e e pi fi n l i s t ( y e a r , 1 9 4 0 ,1 9 5 0 ,1 9 6 0 ,1 9 7 0 , 2 0 1 c o l l a p s e( m e a n )m a x ,b y ( m o n t hy e a r ) s o r ty e a rm o n t h g e n e r a t ed a t e 1 = _ n

t s s e td a t e 1 / / s e tt ot i m es e r i e ss ol a g" l . " / /f o r w a r d" f . "o p e r a t o r sc a nb e / /s e eh e l pt s v a r l i s t

t w o w a yp c a r r o wm a xl . m a xf . m a xm a x ,m l a b ( d a t e 1 y t i t l e ( " T h i sm o n t hD e g .C " ) x t i t l e ( " P r e v i o u sm o n t hD e g .C " ) t i t l e ( " T e m p e r a t u r eC y c l eM e l b o u r n e-D e g .C " ) s u b t i t l e ( " Y e a r s1 9 4 01 9 5 01 9 6 01 9 7 02 0 1 0 " )

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

Stata Graphs - Examples


F r o mS t a t a l i s t : 2 2 / 1 1 / 2 0 1 05 : 4 7A M K i tB a u m

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

m a r g i n s ,a t ( k p r i c e = ( 3 / 1 5 ) )v s q u i s hp o s t r e t u r nl i s t e r e t u r nl i s t p a r m e s t ,n o r e s t o r e / / s a v e sp a r a m e t e r s l g e nk p r i c e = 3+_ n-1 l l a b e lv a rk p r i c e" P r i c e ( \ $ 1 0 0 0 ) "

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

/ /f i xt h eo t h e re x p l a n a t o r yv a r i a b l e s / /I nt h i sc a s ew eo n l yl o o ka tU S( " d o m e s t i c " r e p l a c ef o r e i g n=0

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

Stata Graphs - Examples

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

Stata Graphs - Examples

/ / U s i n gt h ec o m p a c to p t i o ni nt h eb y ( )o p t i o n s y s u s ea u t o ,c l e a r f o r v a l u e si = 1 0 ( 1 0 ) 4 0{ l o c a lm ` i ' = s t r i n g ( ` i ' * 0 . 4 2 5 1 4 3 7 , " % 6 . 2 f " ) }

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

t w o w a ys c a t t e rp r i c em p g ,/ / / b y ( g r o u p ,c o l s ( 5 ) / / / r 1 t i t l e ( " C a rt y p e " , / / / o r i e n t a t i o n ( r v e r t i c a l ) / / / s i z e ( m e d s m a l l ) ) / / / t 1 t i t l e ( " R e p a i rs t a t u s " , / / / s i z e ( m e d s m a l l ) ) / / / n o t e ( " " )c o m p a c 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 ] . 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

Stata Graphs - Examples

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

R e q u i r e s :s p g r i d ,s p k d e &m y l a b e l s t od o w n l o a dt h e s ep r o g r a m st y p et h ef o l l o w i n g o nt h eS t a t ac o m m a n dl i n e( i fn o ta l r e a d yl o a d s s ci n s t a l ls p g r i d a n dt h e n s s ci n s t a l ls p k d e a n dt h e n s s ci n s t a l lm y l a b e l s s y s u s e" a u t o . d t a " ,c l e a r s e tm o r eo f f

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

*3 .E s t i m a t et h eb i v a r i a t ep r o b a b i l i t yd e n s i t s p k d eu s i n g" 2 D G r i d P o i n t s . d t a " , x c o o r d ( x )y c o o r d ( y ) b a n d w i d t h ( f b w )f b w ( 0 . 1 )d o t s s a v i n g ( " 2 D K d e . d t a " ,r e p l a c e ) u s e" 2 D K d e . d t a " ,c l e a r m e r g e1 : 1_ nu s i n gx y . d t a / / / / / / / / /

t w o w a y( c o n t o u rp s p g r i d _ y c o o r ds p g r i d _ x c o o r d l e v e l s ( 1 5 ) ) ( s c a t t e ryx ,m c o l o r ( b l a c k )m s i z e ( s m a l l )) , x l a b ( ` X L A B ' ,n o g r i d )x t i t l e ( " M i l e a g e( m p g ) " ) y l a b ( ` Y L A B ' ,n o g r i d ) y t i t l e ( " P r i c e$ U S " )p l o t r e g i o n ( c o l o r ( b l u e ) )

www.survey-design.com.au/Stata Graphs.html

39/42

12/28/12

Stata Graphs - Examples

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

m a t a : r e a ls c a l a re s c a p e ( c o m p l e xs c a l a rZ ,C ,r e a ls { r e a ls c a l a ri f o r( i = 0 ;i<m a x i t e r ;i + + ){ i f( n o r m ( Z )< =R ){ Z=Z * Z + C } e l s er e t u r n ( i ) } r e t u r n ( i ) } e n d

m a t a : m a n d e l b r o t _ s e t=J ( 2 0 1 * 2 0 1 ,3 ,. ) c n t=1 C=0 . 8 + 0 . 1 5 6 i f o r ( i = 0 ;i < = 2 0 0 ;i + + ){ f o r ( j = 0 ;j < = 2 0 0 ;j + + ){ Z=C ( 2 + i * 4 / 2 0 0 ,2 + j * 4 / 2 0 0 ) n=e s c a p e ( Z ,C ,1 0 0 ,4 0 0 ) m a n d e l b r o t _ s e t [ c n t , . ] =( i ,j ,n ) c n t + + } } e n d

g e t m a t a( xye s c a p e ) =m a n d e l b r o t _ s e t q u i e ts u m m a r i z ee s c a p e l o c a lm i n=r ( m i n ) 1 l o c a lm a x=r ( m a x ) + 1 t w o w a yc o n t o u re s c a p eyx ,c c u t s ( ` m i n ' ( 1 ) ` m a x c l e g e n d ( o f f )g r a p h r ( m ( z e r o ) )x s c a l e ( o f f ) x l a b ( , n o g r i d ) y s c a l e ( o f f )y l a b ( , n o g r i d )s c h e x i t

Survivual Analysis Graphs

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

Stata Graphs - Examples


k e e pn e w v a r_ t s a v ec : / s 1 ,r e p l a c e

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 ]

t w o w a y ( r a r e au bl b_ t ,s o r t ) ( l i n e n e w v a r 1s t u d y t i m e ,s o r tc ( s t a i r s t e p ) ) ( p c i 02 0` a 1 '2 0 )( p c i ` a 1 '0 ` a 1 '2 0 ) , 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 ) s c h e m e ( s 1 c o l o r )l e g e n d ( o f f )x t i t l e ( " S t u d y t i m e " y l a b e l (. 1 ( . 2 ) 1 )y m l a b e l ( ` a 1 ' ,a n g l e ( 4 5 ))

www.survey-design.com.au/Stata Graphs.html

41/42

12/28/12

Stata Graphs - Examples

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

Potrebbero piacerti anche