Home

Products

  • Overview of Stata
  • Why buy Stata?
  • Stata Journal
  • Stat/Transfer
  • 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


  • Order form

    Contact us

    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 the code has been slightly modified.
    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.



    sysuse nlsw88, clear	
    
    generate g=max(grade,8)
    label  var g "Years of education"	
    rename  married m	
    collapse wage, by(g m)	
    generate  w=round(wage,.01)	
    summarize wage, meanonly	
    generate  c=round((wage-r(min))/(r(max)-r(min))*255)	
    quiet levelsof c, loc(cs)	
    local g		
    foreach c of loc cs {	
       local c1=120+round(`c'/2)
       local c2=255-round(`c'/2)	
       local m mc("`c2' `c2' `c1'")	
       local g `g'||sc m g if c==`c', ms(S) msize(ehuge) `m' 
     }	
     local g `g'||sc m g, ms(i) mlab(w) mlabp(0)	
     local g `g' leg(off) yla(-.75 " " 0 "N" 1 "Y" 1.75 " ", notick)             
     local g `g' xla(7 " " 8/18 19 " ", notick)		
     
    twoway `g'  scheme(s1mono) ti(Wage heat map) xsize(8) ysize(5)	
    
     clear		
     		
      input input low   high  mean	
             1     1.6   2.2  1.9	
             2     1.5   2.3  1.9
             5     2.9   0.01 1.9
    		 3     1.0   2.9  1.9
             4     0.5   3.2  1.9	
      end						
     										
      label var input "Input"	
      label var low   "Low value"	
      label var high  "High value"	
      label var mean  "Mean"	
     										
      label define inputlbl             ///	
      1 "Annual Donations in U.S."      ///
      2 "Seroprevalence"                ///
      3 "Exposures per Donation"	    ///
      4 "Probability of Transmission"   ///	
      5 "Sensitivity of Test"  			
      				
      label val input inputlbl	
     							
      gen sens = high-low	
      sort sens
     										
      local ml1 = (2.2+1.9)/2	
      local mh1 = (1.6+1.9)/2	
      local ml2 = (2.3+1.9)/2	
      local mh2 = (1.5+1.9)/2	
      local ml3 = (2.9+1.9)/2	
      local mh3 = (1.0+1.9)/2	
      local ml4 = (3.2+1.9)/2	
      local mh4 = (0.5+1.9)/2	
      local ml5 = (2.9+1.9)/2	
      local mh5 = (0  +1.9)/2	
     		
     		
    local format fcolor(white) lcolor(white) 
    										
      twoway                                                                   ///
      (rbar low mean input, horizontal)                                        ///
      (rbar mean high input, horizontal),                                      ///
      ylabel(, val angle(horizontal)) xsize(7.1) ysize(5.2) scheme(sj)         ///
      xtitle(" " "Predicted Cases of Transfusion-Transmitted T.   Cruzi")      ///
      title("Sensitivity of Results to Inputs") subtitle("Test Once Scenario") ///
      legend(label(1 "Low Input Values") label(2 "High Input Values") rows(2)) ///
      text(5 `ml5' "93.5%", box j(center)  margin(l+1 r+1 t+1 b+1)   `format') ///
      text(5 `mh5' "99.99%", box j(center) margin(l+1 r+1 t+1 b+1)   `format') ///
      text(4 `ml4' "5%", box j(center)     margin(l+1 r+1 t+1 b+1)   `format') ///
      text(4 `mh4' "0.8%", box j(center)   margin(l+1 r+1 t+1 b+1)   `format') ///
      text(3 `ml3' "3", box j(center)      margin(l+1 r+1 t+1 b+1)   `format') ///
      text(3 `mh3' "1", box j(center)      margin(l+1 r+1 t+1 b+1)   `format') ///
      text(2 1.4 "1:28,600", box           margin(l+1 r+1 t+1 b+1)   `format'  ///
      placement(9) j(right))                                                   ///
      text(2 2.4 "1:18,200", box           margin(l+1 r+1 t+1 b+1)   `format'  ///
      placement(3) j(left))                                                    ///
      text(1 1.5 "13.4 million", box       margin(l+1 r+1 t+1 b+1)   `format'  ///
      placement(9) j(right))                                                   ///
      text(1 2.4 "19 million", box         margin(l+1 r+1 t+1 b+1)   `format'  ///
      placement(3) j(left))							
    
    // Creating axis
    
    sysuse auto, clear	
    
    gen y=(price-5000)/1000	
    
    gen x=mpg-20	
    
    local x `"||scatteri 0 -10 "-10" 0 10 "10" 0 20 "20","'
    local x `"`x' mlabpos(6) mlabsize(*1.25)"'	
    local x `"`x' msymbol(none)  mlabcolor(black)"'
    local x `"`x'||scatteri 0 -10 "|" 0 10 "|" 0 20 "|" ,"'	
    local x `"`x' mlabpos(0) msymbol(none)  mlabcolor(black)"'	
    local y `"||scatteri -3 0 "-3" 3 0 "3" 6 0 "6" 9 0 "9","'	
    local y `"`y' mlabpos(9) mlabsize(*1.25)"'	
    local y `"`y' msymbol(none)  mlabcolor(black)"'
    local y `"`y'||scatteri -3 0 "_" 3 0 "_" 6 0 "_" 9 0 "_","'	
    local y `"`y' mlabpos(0) msymbol(none)  mlabcolor(black)"'
    local ax "yli(0,lc(black) lw(thin)) xli(0,lc(black) lw(thin))"	
    local ax "`ax' ysc(off) xsc(off) leg(off)"	
    
    scatter y x,`ax'`x'`y' scheme(s2mono) graphr(fc(white)) yla(,nogrid)
    
    
    
    // Creating axis in the middle of a graph
    
    
    sysuse auto, clear
    
    scatter price weight if weight<3000,  graphregion(margin(right(-10))) /// 
    xscale(range(2000 3010)) plotregion(margin(0)) /// 
    name(first, replace)  yscale(off)   nodraw
    
    
    scatter price weight if weight>3000  ,  graphregion(margin(left(-10)))  /// 
    xscale( range(3010 5000))  plotregion(margin(0))  /// 
    name(second, replace) xtitle(,color(bg) )    nodraw
    
    graph combine   first second 
    
    	
    
    
    
    
    
    // Controlling the side of the graph that the axis is on
    
    
    sysuse auto, clear
    
    twoway ///
    (histogram mpg, width(5) yscale(alt  axis(1)) ) ///
    (line  weight mpg, yaxis(2) yscale(alt axis(2)) sort) 
     
    	
    
    
    
    
    
    // Shade Part of a Graph
    
    sysuse auto, clear	
    
    summarize price, mean
    local mean = r(mean)
    kdensity price, gen(x h)
    
    line h x, || area h x if  x < `mean'
    
    
    
    
    
    
    // Grid on top of graph
    
    ssc install freduse, replace  //gets program online
    freduse MPRIME, clear
    generate ym = mofd(daten)
    tsset ym, monthly             //sets for time series
    twoway                                                                    ///
    function y=20.705,range(119 130) recast(area) color(gs12) base(4.7025) || ///
    function y=20.705,range(166 182) recast(area) color(gs12) base(4.7025) || ///
    function y=20.705,range(240 274) recast(area) color(gs12) base(4.7025) || ///
    function y= 5    ,range(119 274) lstyle(grid)                          || ///
    function y=10    ,range(119 274) lstyle(grid)                          || ///
    function y=15    ,range(119 274) lstyle(grid)                          || ///
    function y=20    ,range(119 274) lstyle(grid)                          || ///
    function y=ym(1980,11), range(4.7025 20.705) horizontal lstyle(grid)   || ///
    tsline MPRIME if tin(1970m1,1990m1), xlabel(,format(%tm)) lstyle(p1)      ///
    legend(order(5 1 "Recession")) tlabel(,grid) scheme(s2color)
    
    
    
    *(Maarten Buis - Stata list,  30 October 2011)
    
    
    
    
    // Shade Part of a Graph
    
    sysuse auto, clear
    sort mpg
    generate x=_n
    gen max=ceil(_N/10)*10
    
    summarize mpg, detail
    
    twoway ///
    (area max x   if inrange(x, `=r(p1)', `=r(p25)'),   ///
    lcolor(bg) fcolor(green)  fintensity(inten20))      ///
    (area  max x   if inrange(x, `=r(p75)', `=r(p99)'), ///
    lcolor(bg) fcolor(green) fintensity(inten20))       ///
    (line x mpg),legend(off) plotregion(margin(zero))   ///
    xtitle("MPG")                                       ///
    xlabel(10 `=r(p1)' `=r(p25)' `=r(p75)' `=r(p99)' )  ///
    yscale(range(0 `=_N'))
    
    
    
    
    
    
    
    // Graph - Slide
    
    twoway                                                                       ///
      (scatteri 6 0 "Kinds of graphs Stata can make", mlabsize(huge) pstyle(p1)) ///
      (scatteri 5 0 "Schemes for controlling look of graphs", mlabsize(huge)     ///
          pstyle(p1))                                                            ///
      (scatteri 4 0 "Structure of graph commands", mlabsize(huge) pstyle(p1))    ///
      (scatteri 3 0 "graph options", mlabsize(huge) pstyle(p1))                  ///
      (scatteri 2 0 "Building complex graphs", mlabsize(huge) pstyle(p1))        ///
      (scatteri 1 0 "Additional examples", mlabsize(huge) pstyle(p1)),           ///
          title("Overview")                                                      ///
       xlabel(-.05 1) ylabel(-1 7)                                               ///
       xscale(off) yscale(off)                                                   ///
       legend(off)                                                               ///
       note("Stata graphics are fun and easy" "Stata graphics are powerful",     ///
    	 ring(0))
    
    
    
    
    
    
    // Graph - Special Text
    
    twoway                                                                        ///
    (scatteri 6.5 0 "Other Symbols Include ",         mlabsize(small) pstyle(p1)) ///
    (scatteri 6.5 0.5 "{&trade} {©right} {&loz}", mlabsize(huge) pstyle(p1))  ///
    (scatteri 5.5 0 "Bold font",                      mlabsize(small) pstyle(p1)) ///
    (scatteri 5.5 0.5 "{bf: Bold font}",              mlabsize(huge) pstyle(p1))  ///
    (scatteri 4.5 0 "{it: Italics}",                  mlabsize(small) pstyle(p1)) ///
    (scatteri 4.5 0.5 "{it: Italics}",                mlabsize(huge)  pstyle(p1)) ///
    (scatteri 3.5 0 "Superscript",                    mlabsize(small) pstyle(p1)) ///
    (scatteri 3.5 0.5 "{superscript: Superscript}",   mlabsize(huge) pstyle(p1))  ///
    (scatteri 2.5 0 "Subscript",                      mlabsize(small) pstyle(p1)) ///
    (scatteri 2.5 0.5 "{subscript: Subscript}",       mlabsize(huge) pstyle(p1))  ///
    (scatteri 1.5 0 "Default Symbol font   ",         mlabsize(small) pstyle(p1)) ///
    (scatteri 1.5 0.5 " {stSymbol:  adcv}  ",         mlabsize(huge) pstyle(p1))  ///
    (scatteri 0.5 0 "Greek Symbols:  ",               mlabsize(small) pstyle(p1)) ///
    (scatteri 0.5 0.5 "{&Alpha} {&Pi} {&Sigma}",      mlabsize(huge) pstyle(p1)), ///
    title("Special Font that can be used on Stata Graphs", color(red))            ///
    xlabel(-.05 1) ylabel(-1 7)                                                   ///
    xscale(off) yscale(off)                                                       ///
    legend(off)                                                                   ///
    note("Stata special font."                                                    ///
    "There are many other options see {bf:help graph text}" ,                     ///
    ring(0))
    
    
    


    Using imported font in a graph

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

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

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

    Copy the font file to the disk file (temp directory suggested)
    Extract file from zip file to a suitable directory

    Go to the Control panel and click on font
    The File>install new Font ..

    The new font "Female and male symbols" has now been installed

    To see a graph with the new font
    
    
    
    //make up some data
    set seed 1
    clear
    set obs 50
    generate a=runiform()*1000
    generate b=runiform()*10
    generate s=runiform()<.5
    
    label define sexlab 1 `"{fontface "Female and male symbols":M }"' 0 ///
          `"{fontface "Female and male symbols":F }"'
    label values s sexlab
    
    scatter a b , ms(i)  mlab(s) mlabpos(c) ///
    mlabsize(*2) title( "Example" `"{fontface "Female and male symbols":M F}"')
    
    
    


    Using adjusted intensities with colour

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

    Producing a graph with different colours for levels of a variable
    this can be done a number of ways. The following are 3 way to do this:

    Options 1:
    Specifying the levels of the variable

    sysuse auto, clear
    
    twoway  ///
    (scatter weight mpg if rep78==1)            ///
    (scatter weight mpg if rep78==2)            ///
    (scatter weight mpg if rep78==3)            ///
    (scatter weight mpg if rep78==4)            ///
    (scatter weight mpg if rep78==5) , name(a1) ///
    legend(label(1 "weight, rep78 == 1"))       ///
    legend(label(2 "weight, rep78==2"))         ///
    legend(label(3 "weight, rep78==3"))         ///
    legend(label(4 "weight, rep78==4"))         ///
    legend(label(5 "weight, rep78==5")) 
    
    Options 2:
    Saving levels in a macro

    sysuse auto, clear
    
    forvalues i=1/5 {
     local a `a' (scatter weight mpg if rep78==`i')
     local b `b' legend(label(`i' "weight, rep78==`i'"))
    }
    
    twoway `a',`b' name(a2)
    
    Options 3:
    Using Stata's separate command

    sysuse auto, clear
    
    separate weight , by(rep78)
    twoway scatter  weight1-weight5   mpg , name(a3) ytitle(Weight (lbs.))
    
    
    
    // stacked bar graph
    
    sysuse auto, clear 
    twoway__histogram_gen mpg if foreign, gen(freq_foreign x) ///
    freq start(0) width(2)	
    drop if missing(x)
    keep freq_foreign x
    save a1, replace
    
    sysuse auto, clear
    twoway__histogram_gen mpg if !foreign, gen(freq_domestic x) ///
    freq start(0) width(2)	
    drop if missing(x)
    keep freq_domestic x mpg
    
    merge 1:1 	x using a1
    
    mvencode freq_domestic  freq_foreign , mv(.=0)
    	
    egen freq = rowtotal(freq_domestic  freq_foreign )
    
    twoway                                                                        ///
    (bar freq_domestic x if freq < ., barw(2) bcolor(blue) xsc(r(10 .)) )         ///
    rbar freq_domestic freq x if freq < ., barw(2) bcolor(red)                    ///
    legend(order(1 "Domestic" 2 "Foreign") pos(11) col(1) ring(0))                ///
    xtitle("`: var label mpg'") ytitle(Frequency) xla(10(5)45, alt) yla(, ang(h)) ///
    xsc(r(10 .)) 
    
    
    
    
    
    
    // box plot
    
    sysuse auto, clear	
    						
    statsby "su mpg" mean=r(mean) max=r(max) min=r(min) sd=r(sd), by(rep78)
    	
    // here k = 1; tune to choice
    gen meanpsd = mean + sd
    gen meanmsd = mean - sd
    						
    // use -line- or -connected- not -scatter-
    twoway scatter mean rep78 ||                            ///
    rbar mean max rep78, bcolor(none) barw(0.2) ||          ///
    rbar mean min rep78, bcolor(none) barw(0.2) ||          ///
    rbar meanmsd meanpsd rep78 , barw(0.1) bcolor(red) || , ///
    legend(off) ytitle(Miles per gallon) yla(, ang(h))      ///
    note(red bars: mean +/- sd; empty bars show range)
    
    
    
    
    
    
    // Candle Stick plot
    
    clear
    input ///
    str3 share         str20   time     price
          abc          "1/5/2011:10:34" 10.34
          abc          "1/5/2011:10:35" 10.25
          abc          "1/5/2011:10:36" 10.15
          abc          "1/5/2011:10:37" 10.73
          abc          "1/5/2011:10:39" 10.95
          abc          "1/5/2011:10:41" 10.53
          abc          "1/5/2011:10:45" 10.99
          abc          "1/5/2011:10:49" 11.05
          abc          "2/5/2011:10:34" 9.34
          abc          "2/5/2011:10:35" 9.25
          abc          "2/5/2011:10:36" 9.15
          abc          "2/5/2011:10:37" 9.73
          abc          "2/5/2011:10:39" 9.95
          abc          "2/5/2011:10:41" 9.53
          abc          "2/5/2011:10:45" 9.99
          abc          "2/5/2011:10:49" 9.05
          abc          "3/5/2011:10:34" 8.34
          abc          "3/5/2011:10:35" 8.25
          abc          "3/5/2011:10:36" 8.15
          abc          "3/5/2011:10:37" 8.73
          abc          "3/5/2011:10:39" 8.95
          abc          "3/5/2011:10:41" 8.53
          abc          "3/5/2011:10:45" 8.99
          abc          "3/5/2011:10:49" 12.05
          abc          "4/5/2011:10:34" 12.34
          abc          "4/5/2011:10:35" 12.25
          abc          "4/5/2011:10:36" 12.15
          abc          "4/5/2011:10:37" 12.73
          abc          "4/5/2011:10:39" 12.95
          abc          "4/5/2011:10:41" 12.53
          abc          "4/5/2011:10:45" 12.99
          abc          "4/5/2011:10:49" 17.05
    end
    l
    
    gen double date=dofc(clock(time,"DMY hm"))
    format date %td
    
    collapse  (p1) p1=price (p25) p25=price ///
    (p50) p50=price (p75) p75=price         ///
    (p99) p99=price, by(date)
    
    gen uav=p75- 1.5*(p25-p75)
    gen lav=p75+ 1.5*(p25-p75)
    		
    gen col=2		
    replace col=(p50-p50[_n-1])>0 if _n!=1	
    		
    twoway (rbar p75 p25 date if col==0, barwidth(.6) color(red))  ///
    (rbar p75 p25 date if col==1, barwidth(.6) color(green))       ///
    (rbar p75 p25 date if col==2, barwidth(.6) color(black))       ///
    (rspike uav lav date if col==2, lwidth(thick) color( black))   ///
    (rspike uav lav date  if col==1 , lwidth(thick) color( green)) ///
    (rspike uav lav date if col==0 , lwidth(thick) color(red)  ) , ///
    legend(off)
    
    
    
    
    
    
    // Extrapolate
    
    sysuse auto, clear
    	
    regress mpg weight
    			
    gen x= weight
    sort x
    twoway  (lfit mpg weight, lwidth(vthick) color(black))  ///
    (function  y = _b[weight]*x+_b[_cons], range(0 8000))	///
    (scatter mpg weight)
    	
    	
    //Alternatively
    
    sysuse auto, clear
    	
    regress mpg weight
    	
    set obs `=_N+2'
    replace weight=0 in l	
    replace weight=8000 in -2	
    	
    predict a
    	
    twoway (scatter mpg weight in 1/-2)  ///
    (line a weight)
    	
    
    
    
    
    
    // Area Plot
    
    // prepare the example data
    
    sysuse nlsw88, clear
    
    gen ind_gr = industry
    recode ind_gr 1/5=1 6=2 7=3 8/10=4 11=5 12=6
    label define ind_gr 1 "manual"                ///
                        2 "trade"                 ///
                        3 "finance"               ///
                        4 "other services"        ///
                        5 "professional services" ///
                        6 "public administration"	
    label value ind_gr ind_gr
    						
    // compute percentile ranks
    egen n = count(wage)
    egen i = rank(wage)
    gen hazen = (i - 0.5) / n * 100	
    label variable hazen "percentile rank of income"
    						
    // smooth the proportions
    mkspline s_w=hazen, cubic nknots(5)
    mlogit ind_gr s_w*
    predict pr*	
    					
    // create the graph
    gen zero = 0
    gen one = 100
    gen l1 = (pr1)*100	
    gen l2 = (pr1 + pr2)*100	
    gen l3 = (pr1 + pr2 + pr3)*100
    gen l4 = (pr1 + pr2 + pr3 + pr4)*100
    gen l5 = (pr1 + pr2 + pr3 + pr4 + pr5)*100
    			
    sort hazen	
    						
    // collect the labels for the second y-axis
    local mid = l1[_N]/2
    local yaxis `"`mid' "manual""'
    						
    local mid = (l2[_N]-l1[_N])/2 + l1[_N]
    local yaxis `"`yaxis' `mid' "trade""'
    						
    local mid = (l3[_N]-l2[_N])/2 + l2[_N]
    local yaxis `"`yaxis' `mid' "finance""'
    						
    local mid = (l4[_N]-l3[_N])/2 + l3[_N]
    local yaxis `"`yaxis' `mid' "other services""'
    						
    local mid = (l5[_N]-l4[_N])/2 + l4[_N]
    local yaxis `"`yaxis' `mid' "professional services""'	
    						
    local mid = (100-l5[_N])/2 + l5[_N]
    local yaxis `"`yaxis' `mid' "public administration""'
    						
    twoway rarea zero l1 hazen, yaxis(1) ||  ///
           rarea l1 l2 hazen, yaxis(2)   ||  ///
           rarea l2 l3 hazen   ||            ///	
           rarea l3 l4 hazen   ||            ///
           rarea l4 l5 hazen   ||            ///
           rarea l5 one hazen,               ///
           ytitle("percentage")              ///
           ylab(`yaxis', axis(2) angle(-45)) ///
           yscale(range(0 100) axis(1))      ///	
           yscale(range(0 100) axis(2))      ///
           ytitle("", axis(2))               ///
           plotregion(margin(zero))          ///
           aspect(1)                         ///	
           legend(off)
    
    
    
    
    
    
    // 45 degree line
    
     sysuse auto, clear
     
     sum weight, meanonly
     
     local max = r(max)
     
     summarize price, meanonly
      if r(max) > `max' {
        local max = r(max)
      }
     
    scatter weight price || scatteri 0 0 `max' `max', recast(line) name(a)
    				
    
    
    
    
    
    // Fourier plot
    
     clear
     set obs 8
     gen x=(_n-1)*(2*_pi)/8
     gen y=sin(x)
     mata: h=st_data(.,"y")
     mata: fft(h)
     
     # delimit ;
     twoway scatter y x ||	
     function y=1/8*(4*sin(1*x)-4*sin( 7*x)), range(x) ||	
     function y=1/8*(4*sin(1*x)-4*sin(-1*x)), range(x) ||,
     legend(cols(1) label(1 "sampled values")	
      label(2 "sum of high frequency components")	
      label(3 "sum of low frequency components"))	
     title("Reconstructed signal for y=sin(x) sampled at 8 points");	
     # delimit cr	
    			
    
    
    
    
    
    // Circle using function
    
    twoway (function y = sqrt(1 - (x)^2),           ///
    range(-1 1) lwidth(thick) lcolor(red))          ///
    function y = -sqrt(1 - (x)^2),                  ///
    range(-1 1) lwidth(thick)lcolor(red)), aspect(1)
    
    
    
    
    
    // normalden using function
    clear
    set obs 100
    
    generate x1 = rnormal(0,1)
    generate x2 = rnormal(0.5,1.3)
    
    summarize x1
    local M1 = r(mean)
    local SD1 = r(sd)
    
    summarize x2
    local M2 = r(mean)
    local SD2 = r(sd)
     
    twoway function x1 = normalden(x, `M1', `SD1') , range(-4 4) || ///
    function x2 = normalden(x, `M2', `SD2'), range(-4 5)
    
    
    
    
    
    // histogram
    
    sysuse auto, clear
    
    twoway histogram mpg if !foreign,                              ///
    start(10) width(2) freq bfcolor(none) blcolor(pink)            ///
    || histogram mpg if foreign, freq start(10) width(2) barw(1.8) ///
    bfcolor(none) blcolor(blue)                                    ///
    legend(order(1 "Domestic" 2 "Foreign") pos(2) ring(0) col(1))
    
    
    
    
    
    
    // Displaying part of a histogram
    
    sysuse auto, clear
    
    twoway__histogram_gen mpg , gen(h y, replace) freq bin(10)
    
    twoway ///
    (histogram y [fw=h] if inrange(y,19,35),discrete color(green)lcolor(red)freq) ///
    (histogram y [fw=h],color(none) lcolor(red) discrete freq name(a1, replace))
    
    twoway ///
    (histogram y [fw=h] if inrange(y,19,35),discrete color(green) lcolor(red)freq ///
    name(a2, replace))
    
    graph combine a1 a2 ,xcommon cols(1)
    
    
    
    
    
    // histogram - add x bar labels for the centre of each bar 
    
    sysuse auto, clear
    
    twoway__histogram_gen mpg  , bin(6) generate(h x , replace ) 
    
    count if !missing(x)
    
    forvalues i=1/`=r(N)' {
    
      local label `label'  `=string(x[`i'],"%8.2f")'
    
    } 
    
    histogram mpg , bin(6) xlabel(`label')
    
    
    
    
    
    // Including a distribution on top of a histogram.
    
    //This is easily done with Stata's: graph histogram 
    //and the normal options. However, this example
    //show this with the twoway histogram.  
    
    sysuse auto, clear
    
    summarize mpg
    
    local width=4  //<-----change width as required
    
    twoway (histogram mpg, width(`width') freq) ///
    (function y =`width'*`=_N'* normden(x, `r(mean)',`r(sd)'), ///
    range(10 45)), name(a1, replace) ytitle("frequency") ///
    xtitle("mpg") legend(off)
    
    
    
    
    
    // Including a distribution on top of a histogram.
    
    //The normal option for graph histogram uses the dataset's mean 
    //and standard deviation.
    //This method allows you to specify these.  
    
    sysuse auto, clear
    histogram mpg , addplot(function normalden(x,26,10), ra(10 42))
    
    
    
    *(Nick Cox - Stata list,  Fri, 03 June 2011)
    
    
    
    
    
    // distribution plot
    
    clear
    sysuse auto
    version 9.2
    keep if foreign
    sort weight
    
    gen weight2 = weight^2
    regress mpg weight weight2
    predict fit
    predict se , stdp
    
    #delimit ;
    twoway sc mpg weight , pstyle(p3) ms(o) ||
    fn weight[3]  - 1000 * normden(x, `=fit[3]' , `=se[3]') , 
    range(`=fit[3] -5' `=fit[3] +5') horiz pstyle(p1) ||
    fn `=fit[3]' , range(`=weight[3]' `=weight[3]-1000*normden(0, se[3])')
    pstyle(p1) ||
    fn weight[17] - 1000 * normden(x, `=fit[17]', `=se[17]') , 
    range(`=fit[17]-5' `=fit[17]+5') horiz pstyle(p1) ||
    fn `=fit[17]', range(`=weight[17]' `=weight[17]-1000*normden(0, se[17])')
    pstyle(p1) ||
    fn weight[21] - 1000 * normden(x, `=fit[21]' , `=se[21]') , 
    range(`=fit[21] -7' `=fit[21] +7') horiz pstyle(p1) ||
    fn `=fit[21]', range(`=weight[21]' `=weight[21]-1000*normden(0, se[21])')
    pstyle(p1) ||
    line fit weight
    ,clwidth(*2) legend(off) ytitle(Miles per gallon) xtitle(Weight)
    title("Scatter with Regression Line and Confidence Interval Densities"
    ,size(4.8) margin(t=0 b=1.5) span)
    caption("graph46", box size(vsmall)position(5) ring(10))
    ;
    #delimit cr		
    
    
    
    
    
    
    // Graph Title - Includes years
    
    clear
    
    inp str10 date ricepr
    "01/01/2006"   700
    "01/02/2006"   700
    "01/03/2006"   900
    "01/04/2006"   900
    "01/05/2006"   900
    "01/06/2006"   900
    "01/07/2006"   900
    "01/08/2006"   933.33
    "01/09/2006"   1000
    "01/10/2006"   1000
    "01/11/2006"   1000
    "01/12/2006"   1000
    "01/01/2007"   1050
    "01/02/2007"   1087.5
    "01/03/2007"   1100
    "01/04/2007"   1100
    "01/05/2007"   1100
    "01/06/2007"   1100
    "01/07/2007"   1175
    "01/08/2007"   1200
    "01/09/2007"   1200
    "01/10/2007"   1300
    "01/11/2007"   1400
    "01/12/2007"   1400
    end
    
    gen int year =yofd(date(date, "DMY"))
    gen byte aux=_n
    
    //from http://www.stata-journal.com/sjpdf.html?articlenum=pr0013
    //follow special sequences
    //generate label for months
    token `c(Mons)'
    
    forv i = 1/24{
    	if `i'==1 la def month `i' "`=year[`i']' ``i''" , modify
    	if inrange(`i',2,12) la def month `i' "``i''" , modify
    	if `i'==13 la def month `i' "`=year[`i-12']' ``=`i'-12''" , modify
    	if `i'>13 la def month `i' "``=`i'-12''" , modify
     }
    
    label value aux month
    
    twoway (line ricepr aux), xlabel(#24, labsize(small) ///
      angle(forty_five) valuel) xtitle("")
    
    
    
    *(Martin Weiss - Stata list,  Sun, 02 May 2010)
    
    
    
    
    
    // Graph Title - Includes years
    clear
    
    inp str10 date ricepr
    "01/01/2006"   700
    "01/02/2006"   700
    "01/03/2006"   900
    "01/04/2006"   900
    "01/05/2006"   900
    "01/06/2006"   900
    "01/07/2006"   900
    "01/08/2006"   933.33
    "01/09/2006"   1000
    "01/10/2006"   1000
    "01/11/2006"   1000
    "01/12/2006"   1000
    "01/01/2007"   1050
    "01/02/2007"   1087.5
    "01/03/2007"   1100
    "01/04/2007"   1100
    "01/05/2007"   1100
    "01/06/2007"   1100
    "01/07/2007"   1175
    "01/08/2007"   1200
    "01/09/2007"   1200
    "01/10/2007"   1300
    "01/11/2007"   1400
    "01/12/2007"   1400
    end
    
    gen date2=date(date,"DMY")
    gen monthlabel = substr("JFMAMJJASOND", month(date2), 1)
    
    replace monthlabel=monthlabel+ " " + string(year(date2),"%5.0f") ///
    if month(date2)==1
    
    labmask date2, values(monthlabel) //user written program, must be installed first
    levelsof date2, local(levels)
    line ricepr date2, xla(`levels', valuelabel angle(45))
    
    
    
    *(Nick Cox - Stata list,  Tue, 04 May 2010)
    
    
    
    
    
    // Controlling date labels 
    clear
    
    input str40 time       x
    "5jan2011 15:00:30"    1
    "1feb2011 17:30:32"    2
    "1march2011 18:10:30"  6
    "1april2011 05:05:01"  2
    "1march2011 15:10:30"  12
    end
    list
    
    generate double time1=clock(time,"DMY hms")
    
    local t1=tc(1jan2011 12:00:0)
    local t2=1000*60*60*24*10
    local t3=tc(1may2011 12:0:0)
    
    scatter x time1, xlab(`t1'(`t2')`t3' ,                ///
    format(%tcddmonccyy_hh:mm) angle(45)) xtitle("Time")
    
    
    *(Nick Cox - Stata list,  Monday, 23 Jan 2011)
    
    
    
    
    
    //2nd Row of axis labels
    
    webuse turksales, clear
    line sales t,                              ///
        xtick(119.5(4)159.5, tlength(*1.5))    ///
        xmlabel(120/159,                       ///
            noticks format(%tqq))              ///
        xlabel(121.5(4)157.5,                  ///
            noticks format(%tqCY) labgap(2.5)) ///
        xtitle("")
    
    
    *(Maarten Buis - Stata list,  Thu, 7 Nov 2013)
    
    
    
    
    
    
    
    // Gap between bars in bar graph
    
    
    sysuse auto, clear
    
    graph bar (mean) price turn mpg weight, over(rep78)
    
    set obs 75
    replace rep78=2.5 in 75
    
    graph bar (mean) price turn mpg weight, over(rep78,  relabel( 3 " "  )) 
    
    
    
    
    *(Nick Cox - Stata list,  Wed, 26 May 2010)
    
    
    
    
    
    
    // bar graph - categorical variables - proportions
    
    
    sysuse auto, clear
    //creating some data
    gen mpg1=mpg<30
    recode mpg (1/25=1) (26/30=2) (31/50=3), gen(mpg2)
    save kk, replace
    list
    
    capture erase kk1.dta
    local z=1
    
    foreach i of varlist for mpg1 mpg2 {
      use kk, clear
      keep `i'
      contract `i'
      egen total=total(_freq)
      replace _freq=_freq/total
      generate type=`z'
      capture append using kk1
      save kk1, replace
      local ++z
    }
    
    drop mp* for
    bysort type : gen t1=_n
    
    label define kk 1 cat1 2 cat2 3 cat3
    label value type kk
    
    
    graph bar (mean) _freq , over(t1,sort(_freq) ) over(type, ) showyvars ///
    asyvar legend(off) nofill ytitle("Proportion") scheme(s2color)
    
    // Including totals in bar graph
    
    sysuse auto, clear
    save auto1 , replace
    
    replace for=3
    label define  origin 3 Total, add
    
    append using auto1
    
    graph bar (mean) price , over(rep78, ) over(for) showyvars ///
    ascategory asyvar legend(off)
    
    
    exit
    
    
    
    // Stacking some levels of a variable and not others
    // Has the years 71 and 77 stacked and year 88 normaly displayed
    
    webuse union, clear
    keep idcode year age
    recode year (71 77=1) (88=2) (*=.), gen(which)
    graph bar age, over(year) over(which) asyvars stack
    
    
    exit
    
    Statalist
    Nick Cox 22/12/11
    
    
    
    
    
    
    
    // Coloring bars in order of magnitude. All bars not covered by the first and 
    // second places are considered (colored) as 3rd place)
    
    
    clear
    input country gdp 
    1	100	
    2	110	
    3	240	
    4	50	
    5	10	
    end
    
    list
    
    gsort -gdp
    
    gen gdpred=gdp if _n==1
    gen gdpgreen=gdp if _n==2
    gen gdpblue=gdp if _n>2
    
    list
    
    graph hbar gdpred gdpgreen gdpblue, over(country, sort(gdp)          ///
          descending) nofill  legend(label(1 "Best") label(2 "2nd best") ///
    	  label(3 "rest") rows(1)) bar(1, color(cranberry))              ///
    	  bar(2, color(dkgreen)) bar(3, color(dknavy))
    
    
    
    
    *(Sergiy Radyakin - Stata list,  Sat, 19 Feb 2011)
    
    
    
    
    
    
    
    // Changing bar labels
    
    
    sysuse auto, clear
    
    graph bar  mpg  gear, showyvars ///
    yvaroptions(relabel(1 "MPG" 2"GEAR RATIO")) legend(off)
    
    exit
    
    
    
    
    
    
    
    
    
    
    // Changing bar labels
    //It is sometimes more convenient to use value lables rather than a graph
    //label options to change graph bar labels. In the example it also allows the
    //legend to be spread over the width of the graph.
    
    clear all
    sysuse auto
    
    lab def origin 0 "Europe de l`=char(146)'Ouest" ///
     1 "Asie de l`=char(146)'Est", modify
    
    graph hbar mpg trunk turn, over(foreign) ///
    legend(row(1) span) stack name(two,replace)
    
    exit
    
    
    
    
    
    *( Stata list,  25 Jun 2011)
    
    
    
    
    
    
    
    // Changing bar labels using the Stata graph editor
    
    // The type of bar label is limited in Stata graphs but sometimes
    // additional information is required. This can be added with the 
    // Stata graphics editor commands.
    // Below the number of observation for each region is added.
    
    sysuse citytemp, clear
     
     #delimit ;
     graph bar tempjuly tempjan, over(region) bargap(-30)
      legend( label(1 "July") label(2 "January") )
      ytitle("Degrees Fahrenheit")
      title("Average July and January temperatures")
      subtitle("by regions of the United States")
      note("Source:  U.S. Census Bureau, U.S. Dept. of Commerce")
     blabel(bar, position(inside) format(%9.1f) color(white)) ;
     #delimit cr
     
     generate No=_n
     collapse (sum) N, by(region)
     list
     
    local i1=1
    
    forvalues i=1/`=_N' {
    
    gr_edit plotregion1.barlabels[`i1'].text = {}
    gr_edit plotregion1.barlabels[`i1'].text.Arrpush N=`=No[`i']'
    local ++i1
    
    gr_edit plotregion1.barlabels[`i1'].text = {}
    gr_edit plotregion1.barlabels[`i1'].text.Arrpush N=`=No[`i']'
    local ++i1
     
    }
    
    exit
    
    
    
    
    
    
    
    // Adding bar labels 
    
    // The type of bar label is limited in Stata graphs but sometimes
    // additional information is required. This can be added with the 
    // by adding another twoway plot.
    
    
    sysuse census, clear
    graph bar (sum) marri div, over(region) stack
    
    more
    
    collapse (sum) marri div, by(region)
    gen ratio = div / (marri + div)
    gen sratio = string(ratio, "%4.3f")
    gen marriPdiv = marria + div
    
    twoway bar marria region, barw(0.6) base(0) ///
    || rbar marria marriPdiv region, barw(0.6) xla(1/4, noticks valuelabel) ///
    || scatter marriPdiv region, ms(none) mla(sratio) mlabpos(12)  ///
    legend(order(1 "marriages" 2 "divorces")) ///
    yla(0 4e5 "400"  8e5 "800" 12e5 "1200" , ang(h)) ytitle(thousands) ///
    ysc(r(. 14e5))
    
    
    
    *(Nick Cox - Stata list,  Thu, 17 Feb 2011)
    
    
    
    
    // Adding average line to bar graph 
    
    webuse grunfeld, clear   //getting data online
    keep if company == 1
    keep if year < 1945
    
    generate year2 = year + 0.3
    generate year3 = year - 0.3
    
    generate average = (invest + mvalue + kstock) / 3
    
    twoway bar invest year3, barw(0.3) || ///
    bar mvalue year, barw(0.3)         || ///
    bar kstock year2 , barw(0.3)       || /// 
    line average year , name(a2, replace)
    
    
    
    *(Nick Cox - Stata list,  13/4/11)
    
    
    
    
    // Adding average bar to group 
    
    sysuse auto, clear
    
    summ price if for==0 & !missing(rep78)
    local ave1=r(mean)
    
    summ price if for==1 & !missing(rep78)
    local ave2=r(mean)
    
    set obs `=_N+3'
    
    replace for=0 in -3
    replace rep78=6 in -3
    
    replace for=0 in -2
    replace rep78=7 in -2
    replace price=`ave1' in -2
    
    replace for=1 in l
    replace rep78=7 in l
    replace price=`ave2' in l
    
    label define kk 1 1 2 2 3 3 4 4 5 5 6 "-"  7 "AVE"
    label value rep78 kk
    
    graph bar (mean) price , over(rep78 ) over(for) showyvars ///
      asyvar legend(off)  scheme(color) bar(7, color(black))
    
    
    
    
    
    
    // Different color bars 
    
    clear
    
    input ///
    id w pos mark
    1  1 1  69.55
    2  1 2  65.16
    3  1 3  64.91
    4  1 4  64.53
    5  1 5  63.70
    6  0 6  84.58
    7  0 7  84.51
    8  0 8  84.12
    9  0 9  83.34
    10 0 10 82.8
    end
    
    label define kk        ///
    1  "Barbora - CZE "    ///
    2  "Christina - GER "  ///
    3  "Linda - GER "      ///
    4  "Sunette - RSA"     ///
    5  "Huihui - CHN"      ///
    6  "Keshorn - TRI "    ///
    7  "Oleksandr - UKR "  ///
    8  "Antti - FIN "      ///
    9  "Vitezslav - CZE"   ///
    10 "Tero - FIN"
    
    label value pos kk
    
    label define w 1 "Women" 0 "Men"
    label value w w
    
    
    graph bar (asis) mark,  over(id ) over(pos,label(angle(45))) over(w) ///
    ylabel( 0(10)90, angle(45))                                         ///
     blabel(bar, position(inside) format(%9.1f) color(black))            ///
     scheme(s2color)  legend(off)                                        ///
     bargap(5) title("London Olympics 2012" "Javelin")                   ///
     nofill ytitle("Metres")                                             ///
     bar(1, color(gold))      ///
     bar(6, color(gold))      ///
     bar(2, color(gs13))      ///
     bar(7, color(gs13))      ///
     bar(3, color(sienna))    ///
     bar(8, color(sienna))    ///
     bar(4, color(cyan))      ///
     bar(9, color(cyan))      ///
     bar(5, color(pink))      ///
     bar(10, color(pink)) 
     
     
    exit
    
    
    
    
    
    
    // Different color bars for the over option
    clear
    
    input ///
    id w pos mark
    1  1 1  69.55
    2  1 2  65.16
    3  1 3  64.91
    4  1 4  64.53
    5  1 5  63.70
    6  0 6  84.58
    7  0 7  84.51
    8  0 8  84.12
    9  0 9  83.34
    10 0 10 82.8
    end
    
    label define w 1 "Women" 0 "Men"
    label value w w
    
    gen g1=mark if _n<6
    gen g2=mark if _n>5
    
    list
    
    graph bar (asis) g1 g2, ascat over(mark,sort(mark) descending ///
    relabel(  ///
    5  "Barbora - CZE "    ///
    4  "Christina - GER "  ///
    3  "Linda - GER "      ///
    2  "Sunette - RSA"     ///
    1  "Huihui - CHN"      ///
    10 "Keshorn - TRI "    ///
    9  "Oleksandr - UKR "  ///
    8  "Antti - FIN "      ///
    7  "Vitezslav - CZE"   ///
    6  "Tero - FIN"        ///
    ) label( angle(45)))   over(w) ylabel(0(10)90)                                 ///
    ytitle("Metres") blabel(bar, position(inside) format(%9.1f) color(white))      ///
    scheme(s2color)  legend(off) bargap(5) title("London Olympics 2012" "Javelin") ///
    nofill  bar(1, color(dknavy)) bar(2, color(red)) 
    
    exit
    
    
    
    
    
    
    // Different color bars for the over option - Using twoway graph
    clear all
    
    input ///
    id w pos mark
    1  0 6  84.58
    2  0 7  84.51
    3  0 8  84.12
    4  0 9  83.34
    5  0 10 82.8
    6  1 1  69.55
    7  1 2  65.16
    8  1 3  64.91
    9  1 4  64.53
    10 1 5  63.70
    end
    
    label define w 1 "Women" 0 "Men"
    label value w w
    
    label define kk        ///
    1  "Keshorn - TRI "    ///
    2  "Oleksandr - UKR "  ///
    3  "Antti - FIN "      ///
    4  "Vitezslav - CZE"   ///
    5  "Tero - FIN"        ///
    6  " "                 ///
    7  "Barbora - CZE "    ///
    8  "Christina - GER "  ///
    9  "Linda - GER "      ///
    10  "Sunette - RSA"    ///
    11 "Huihui - CHN"      
    
    label value id  kk
    
    replace id=id+1 if id>5
    
    set obs `=_N+1'
    replace id=6 in l
    
    twoway (bar mark id if id==1, base(0) ylabel(0(10)90) ///
    xtitle("Men                                  Women")  ///
    xlabel(1(1)11,valuelabel angle(45))                   ///
    barwidth(.4)   color(cyan))                           ///
    (bar mark id if id==2,  barwidth(.4)   color(cyan))   ///
    (bar mark id if id==3,  barwidth(.4)   color(cyan))   ///
    (bar mark id if id==4,  barwidth(.4)   color(cyan))   ///
    (bar mark id if id==5,  barwidth(.4)   color(cyan))   ///
    (bar mark id if id==7,  barwidth(.4)   color(red))    ///
    (bar mark id if id==8,  barwidth(.4)   color(red))    ///
    (bar mark id if id==9,  barwidth(.4)   color(red))    ///
    (bar mark id if id==10, barwidth(.4)   color(red))    ///
    (bar mark id if id==11, barwidth(.4)   color(red))    ///
    (scatter mark id,  mlabel(mark) msymbol(none)         ///
    mlabposition(12)), legend(off)                        ///
    title("London Olympics 2012" "Javelin")               ///
    plotregion(margin(l=5 b=0)) 
     
    exit
    
    
    
    
    
    
    
    
    // Adding bar labels in bold font
    
    // Adding bold font using the Stata editor commands. This is produced
    // using a program. 
    
    program g1
    syntax varname, over(varname)
    
    levelsof `over', local(kk)
    
    graph bar  `varlist'  , over(`over')                 ///
    blabel(bar, position(base) gap(*22.5) format(%9.1f)  ///
    size(large) color(black)) 
     
    local x=1
    foreach i of local kk {
      summ `varlist' if `over'==`i'
      local a1=string(r(mean),"%8.0f")
     
    gr_edit plotregion1.barlabels[`x'].text = {}
    gr_edit plotregion1.barlabels[`x'].text.Arrpush `"{bf: `a1'   }"'
    local ++x
    }
     
    end 
     
     
    sysuse auto, clear
    g1 price , over(rep78)
    
    
    
    
    
    
    // Adding frequency count bar labels in bold font
    
    // Adding this using the Stata editor commands. This is produced
    // using a program. 
    
    program g2
    syntax varname, over(varname)
    
    levelsof `over', local(kk)
    
    graph bar  `varlist'  , over(`over')                 ///
    blabel(bar, position(base) gap(*22.5) format(%9.1f)  ///
    size(large) color(black)) 
     
    local x=1
    foreach i of local kk {
      summ `varlist' if `over'==`i'
      local a1="N= "+string(r(N),"%8.0f")
     
    gr_edit plotregion1.barlabels[`x'].text = {}
    gr_edit plotregion1.barlabels[`x']._set_orientation vertical
    gr_edit plotregion1.barlabels[`x'].text.Arrpush `"{bf:  `a1'   }"'
    local ++x
    }
     
    end 
     
    sysuse auto, clear
    egen pr2=cut(price), group(10)
    
    g2 weight , over(pr2)
    
    
    
    
    
    // Using stack to produce a graph
    
    
    use http://www.stata-press.com/data/r10/auto, clear
    preserve
    
    tempfile mylabel
    label save origin using `mylabel'
    
    // map 1..5 -> 3..7 to ensure offset from -foreign-'s values of 0 1 
    replace rep78 = rep78 + 2 
    
    stack price foreign weight rep78, into(data foreign) clear
    
    // bring back labels
    label def _stack 1 "`l1'" 2 "`l2'"
    label val _stack _stack
    do `mylabel'
    // reverse -rep78- offset in labels 
    label def origin 3 "1" 4 "2" 5 "3" 6 "4" 7 "5", add
    label val foreign origin
    
    // now...
    graph hbox data, over(foreign) 
    
    
    
    *(Nick Cox - Stata list,  Thu, 8 Nov 2007)
    
    
    
    
    
    // Detail and Summary box plots together
    
    
    sysuse auto, clear
    generate order = _n
    expand 3
    bysort order : generate which = _n
    drop if which == 1 & price > 5000
    drop if which == 2 & price > 10000
    label def which 1 "<= $5000" 2 "<= $10000" 3 "all"
    label val which which
    graph box mpg, over(which) over(foreign)
    
    
    
    
    *(Nick Cox - Stata list,  December 06, 2013)
    
    
    


      // Log scale - not working as expected - fix
      // Also see: How can I best get box plots on logarithmic scales? .

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

    sysuse nlsw88, clear
    
    clonevar wagelog10=wage
    replace wagelog10=log10(wagelog10)
    
    mylabels 0(10)40 , myscale(log10(@)) local(labels)
    
    graph hbox wagelog10, over(ind, sort(1)) nooutside ytitle("") ylabel(`labels') ///
    title("Hourly wage, 1988, woman aged 34-46", span) subtitle(" ") ///
    note("Source:1988 data from NLS, U.S. Dept. of Labor, "          ///
    "Bureau of Labor Statistics",span) 
    exit
    
    
    
    


      // Breaking the y scale

    replace lexp = 35 if country == "Haiti"
    egen median = median(lexp), by(region)
    egen upq = pctile(lexp), p(75) by(region)
    egen loq = pctile(lexp), p(25) by(region)
    egen iqr = iqr(lexp), by(region)
    egen upper = max(min(lexp, upq + 1.5 * iqr)), by(region)
    egen lower = min(max(lexp, loq - 1.5 * iqr)), by(region)
    twoway rbar med upq region, ///
     pstyle(p1) blc(gs15) bfc(gs8) barw(0.35) || ///
     rbar med loq region, pstyle(p1) blc(gs15) bfc(gs8) barw(0.35) || ///
     rspike upq upper region, pstyle(p1) || ///
     rspike loq lower region, pstyle(p1) || ///
     rcap upper upper region, pstyle(p1) msize(*2) || ///
     rcap lower lower region, pstyle(p1) msize(*2) || ///
     scatter lexp region if !inrange(lexp, lower, upper) & lexp > 50, ///
     ms(Oh)  mla(country) mlabcolor(gs8) xscale(off)  legend(off) ///
     yla(, ang(h)) ytitle(Life expectancy (years)) xtitle("") ///
     name(gr1,replace)
    
    
     scatter lexp region if !inrange(lexp, lower, upper) & lexp < 50, ///
     ylabel(35)   fysize(20) ytitle("") ms(Oh) mlabcolor(blue) ///
     mcolor(blue)  mla(country)  xlabel(1 `" "Europe and" "Central Asia" "' ///
     2 "North America" 3 "South America", noticks) ///
    ytitle(Life expectancy (years), color(white))  yla(, ang(h))  ///
      xtitle("")  name(gr2,replace)
    
    graph combine gr1 gr2, col(1) xcommon imargin(zero)
    
    
    
    
    *(Scott Merryman - Statalist,  17/12/2013)
    
    
    
    // Labelling the over variable
    
    
    sysuse auto, clear
    
    levelsof for, local(a)
    levelsof rep78, local(a1)
    
    tempfile file
    
    
    foreach i of local a {     //for
    foreach i1 of local a1 {   //rep78
    
    sysuse auto, clear
    
    count if for==`i' & rep78==`i1'  //making sure there are obs to regress
    
     if r(N)>=1 {
       regress mpg weight if for==`i' & rep78==`i1'
       parmest, norestore 
       egen axis= axis(estimate), label(parm)
       gen for=`i'
       gen rep78=`i1'
       capture append using `file'
       save `file', replace
       local kk=`i1'+1
     }
    }
    }  //end loop
    
    
    summarize rep78
    foreach i of local a {  //for
      set obs `=_N+1'
      replace for=`i' if for==.
      local max1=`r(max)'
      replace rep78=`max1'  +1 if rep78==.
    }
    
    gsort for -rep78
    
    drop if parm=="_cons"
    gen order=_N-_n
    
    label define jj 9 "foreign=0       "  ///
                    3 "foreign=1       "  ///
    				0 "rep78_3"        ///
    				1 "rep78_4"        ///
    				2 "rep78_5"        ///
    				4 "rep78_1"        ///
    				5 "rep78_2"        ///
                    6 "rep78_3"        ///
    				7 "rep78_4"        /// 
    				8 "rep78_5"
    
    label value order  jj 
    
    twoway (scatter order est , msymbol(none))  ///
    (scatter order est )                        ///
    (rcap min95 max95 order, horizontal ),      ///
    ylabel(0/9, valuelabel angle(0)) legend(off) ytitle("rep78 over foreign") ///
    xtitle("Coefficients and confidence interval")
    
    
    exit
    
    
    
    
    
    
    // Text that varies in size due to a calculated factor
    
    
    clear programs
    
    *--------------- begin example ---------------------
    program s
    
    sysuse auto, clear
    tempname hdle
    tempfile info
    postfile `hdle' str20  variable missings using `info'
    
    qui ds, has(type numeric)
    
    qui foreach var in `r(varlist)'{
        /*want more missings
        to make this interesting */
        replace `var'=. if runiform()<0.1
        qui count if `var'==.
        post `hdle' ("`var'") (r(N))
      }
    
    postclose `hdle'
    
    use `info', clear
    gen number=_n
    list, noobs
    gen x = 1
    
    sum missings, meanonly
    gen factor = missings/r(mean)
    levelsof variable, local(aa)
    
    local a=1	
    foreach i of local aa { local f = factor[`a'] local txt `"`txt' text(`a' 1 "`i'", size(*`f'))"' capture `++a' } scatter number x, msymbol(i) /// xtitle("") xscale(off) /// ytitle("") yscale(off) /// plotregion(lstyle(none)) /// `txt' *----------------- end example ----------------------------- end s //calls program "s" above
    
    
    // Using xtset to produce a panel data graph 
    
    //produce the panel dataset
    
    clear
    set more off
    
    set obs 3
    gen id=_n
    expand 10
    bysort id: gen year=2000+_n-1
    gen data=runiform()*100
    list, sepby(id)
    
    
    // producing the graph
    
    xtset id year
    
    bysort year :egen ave=mean(data)
    replace ave=. if id!=1
    
    gen mean_lab="mean" if id==1 & year==2009
    
    sort id year
    xtline data, overlay addplot(                    ///
    (line ave year,lwidth(thick))                    ///
    (scatter data year if year==2009,mlab(id))       ///
    (scatter ave year if year==2009,mlab(mean_lab)), ///
    legend(off))
    
    //alternative to xtset, producing the same graph 
    
    twoway (line data year,c(L))                     ///
    (line ave year,lwidth(thick))                    ///
    (scatter data year if year==2009,mlab(id))       ///
    (scatter ave year if year==2009,mlab(mean_lab)), ///
    legend(off) ytitle("data")
    
    
    
    
    
    // Double graph - if there is too much detail for one 
    // graph then 2 graphs can be used
    
    
    clear
    set more off
    use "http://www.stata-press.com/data/r11/nlswork2.dta"
    describe
    
    replace year=1900+year
    
    gen age11= yofd(date(c(current_date)),"DMY") -(1900+birth_yr)
    
    recode age11 (56/58=1) (59/61=2) (62/64=3) (65/67=4) (68/70=5)
    
    collapse (count) union  (count) idcode ,by(year age11)
    gen per=(union/idcode)*100
    
    twoway                                                                   ///
    (line  per year   if age11==1 , lpattern(dash)  connect(l))              ///
    (line  per year   if age11==2 , lpattern(dash_dot)  connect(l))          ///
    (line   per year   if age11==3 , lpattern(dot) connect(l)) ,             ///
    name(a1,replace) legend(  bmargin(l=40)     stack    region(style(none)) ///
    label( 1 "56-58")                                                        ///
    label( 2 "59-61")                                                        ///
    label( 3 "62-64")                                                        ///
    rows(1)   ) xtitle("")                                                   ///
    xlabel(1968 "            1968"  1978 "1978             ", noticks)
     
    twoway                                                                      ///
    (line  per year   if age11==4 ,  yaxis(1)  lpattern(solid)  connect(l))     ///
    (line  per year   if age11==5 ,  yaxis(1)  lpattern(longdash)  connect(l)), ///
    name(a2,replace) legend(  bmargin(r=40)  stack    region(style(none))       ///
    label(1 "65-67")                                                            ///
    label( 2 "68-70")                                                           ///
    rows(1)   ) xtitle("")                                                      ///
    xlabel(1968 "            1968"  1978 "1978             ", noticks) ///
    yscale(alt) xline(1968)
    
    graph combine a1 a2 , imargins(zero) ycommon 
    
    
    
    
    
    
    
    //Graphing the mean of a variable by other variables 
    
    sysuse auto, clear
    collapse (mean) mpg , by(for price)
    
    twoway                      ///
    (line  mpg price if for==0) ///
    (line  mpg price if for==1) ///
    ,legend( label(2 "foreign" ) label(  1 "domestic" )) 
    
    
    
    
    
    
    // Difference from Average by levels of groups
    
    sysuse auto, clear
    
    graph drop _all
    
    bysort for rep78 : egen mpg1=mean(mpg)
    bysort  rep78: egen mpg2=mean(mpg)
    
    gen mpg3=mpg1-mpg2
    
    replace mpg2=0 if rep78==.
    
    twoway ///
    (bar mpg3 rep78 if for==0 & rep78==1 ,fcolor(green) ///
    lcolor(green) barwidth(.5)horiz  yscale(noline))  ///
    (bar  mpg3 rep78 if for==0 & rep78==2 ,fcolor(blue) ///
     lcolor(blue) barwidth(.5)horiz  yscale(noline))   ///
    (bar  mpg3 rep78 if for==0 & rep78==3 ,fcolor(red)  ///
    lcolor(red) barwidth(.5)horiz  yscale(noline))    ///
    (bar  mpg3 rep78 if for==0 & rep78==4 ,fcolor(pink) ///
    lcolor(pink) barwidth(.5)horiz  yscale(noline))   ///
    (bar  mpg3 rep78 if for==0 & rep78==5 ,fcolor(cyan) ///
    lcolor(cyan) barwidth(.5)horiz  yscale(noline)) , ///
    xtitle(MPG) xline(0)  ///
    title("Foreign made cars") legend(off) name(k1, replace)
    
    twoway ///
    (bar  mpg3 rep78 if for==1 & rep78==1 ,fcolor(green) ///
    lcolor(green) barwidth(.5)horiz  yscale(noline)) ///
    (bar  mpg3 rep78 if for==1 & rep78==2 ,fcolor(blue) ///
    lcolor(blue) barwidth(.5)horiz  yscale(noline))   ///
    (bar  mpg3 rep78 if for==1 & rep78==3 ,fcolor(red)  ///
    lcolor(red) barwidth(.5)horiz  yscale(noline))    ///
    (bar  mpg3 rep78 if for==1 & rep78==4 ,fcolor(pink) ///
    lcolor(pink) barwidth(.5)horiz  yscale(noline))    ///
    (bar  mpg3 rep78 if for==1 & rep78==5 ,fcolor(cyan) ///
    lcolor(cyan) barwidth(.5)horiz  yscale(noline)) , ///
    xtitle(MPG) xline(0) yscale(off) ///
    title("Domestically made cars") legend(off) name(k2, replace)
    
    graph combine k1 k2, row(1)  imargin(10 0 0 0) ycommon ///
    title("Difference from average of mpg for " ///
    "Foreign or Domestically produced cars" " " )
    
    
    
    
    
    
    // Stacked bar graph - reshaping the data
    
    
    //creating the data
    
    clear
    set obs 1000
    egen id = seq(), block(10)
    egen time = seq(), to(10)
    format time %tq
    gen time2=string(time, "%tq")
    gen sector = ceil(3 * runiform())
    bysort id: replace sector=sector[1]
    label define sector 1 "SECTOR 1" 2 "SECTOR 2" 3 "SECTOR 3"
    label values sector sector
    gen sales=exp(rnormal())*1000
    gen sales2=2*sales
    
    
    
    //reshaping the data
    gen obs = _n
    rename sales sales1
    reshape long sales, i(obs) j(which)
    
    graph bar (sum) sales ,  over(sector) over(which) ///
    over(time2, label(labsize(vsmall))) asyvars stack legend(size(vsmall))
    
    
    
    
    
    
    // Stacked bar graph - percentage
    
    sysuse auto, clear
    
    gen a=_n
    collapse a, by(for rep78)
    drop if missing(for, rep78)
    
    bysort for rep78: gen sum = sum(a)
    replace a=0 if a==.
    
    levelsof rep78, local(aa)
    
    generate csum=.
    
    foreach i of local aa {
      summ a if `i'==rep78
    replace csum=a/`r(sum)' if `i'==rep78
    }
    
    graph bar (sum)csum ,  over(for) over(rep78)        ///
    asyvars stack legend(size(vsmall)) per  blabel(bar, ///
    position(center) format(%3.1f))
     
     
    *(Also see the user written program catplot on:
    http://www.survey-design.com.au/Usergraphs.html
    )  
    
    
    
    
    
    // Editing a graph with the gr_edit command
    
    
    //The graph with the y axis line
    
    sysuse auto, clear
    graph hbar   weight price, yscale(off)
    
    
    //The graph with the y axis line removed
    
    sysuse auto, clear
    graph hbar   weight price, ysc(off)
    gr_edit .varaxis.style.editstyle linestyle(color(none)) editcopy
    
    
    
    //The above can also be produced with:
    
    graph hbar weight price ,showyvar  yvaroptions(axis(lc(none)))
    
    
    From the Statalist:
    Kieran McCaul
    11/6/2010
    
    David Airey
    11/6/2010
    
    
    
    
    
    // Breaking up text automatically in a long note
    
    webuse auto, clear
    set more off
    
    //input the text to place in the graph - can be on one line if you wish
    
    local z1 "The development of Linux is one of the most prominent" 
    local z2 " examples of free  and  open source software" 
    local z3 "local z3 collaboration; typically all the underlying source code" 
    local z4 "can beused, freely  modified, and redistributed, both commercially"
    local z5 "and  non-commercially, by anyone under licenses such as the GNU"
    local z6 "General Public License."
    local z7 "Typically Linux is packaged in a format known as a Linux"
    local z8 "distribution for desktop and server use."
    
    local total "`z1' `z2' `z3' `z4' `z5' `z6' `z7' `z8'"
    
    local size=40   //adjust for length of line
    
    local t1
    
    forvalues i=1/12 {                //increase the 12 if more lines required
    local a`i' : piece `i' `size' of "`total'"   //extended macro function
    
    local t1  `"`t1' `=char(34)+ "`a`i''"+char(34)' "'
    }
    
    graph bar price,bargap(-30) over(foreign)         ///
    note( `t1' ,   margin(large) justification(left))
    
    exit
    
    
    
    
    
    // Breaking up long labels
    
    sysuse auto, clear
     
     #delimit
    label define origin 0 `"group0 group0 group0 group0 group0
    group0 group0 group0 group0 group0"', modify;
    
    label define origin 1 "group1 group1 group1 group1 group1
    group1 group1 group1 group1 group1 ", modify;
    #delimit cr
     
     
    local relabels 
    local relabels1 
     
    levelsof for, local(groups)
      
    local s_len=20  //Change to required length
    
    foreach g of local groups {
    
      local label : label origin `g'
      local len : length local label
    
      if `len'>`s_len' {
    	forvalues i=1/  `=`len'/`s_len'+1' {
    	    local p1 : piece `i' `s_len' of `"`label'"', nobreak
    	    local relabels `"`relabels' `=char(34)+ "`p1'" +char(34)' "'
    	}
    
    	local relabels1  `relabels1'   `=`g'+1'    `"`relabels'"'
    local relabels 
    	
       }
    }
    
    graph hbar mpg, over(for, relabel(`relabels1' ))
    exit
    
    
    



    Breaking up long labels - with splitvallabels

    Requires the user written command: splitvallabels
    To download splitvallabels type the following on the Stata command line:
    ssc install splitvallabels
    
    
    
    sysuse auto, clear
        
    label define rep78 1 "Very low rating in 1978" ///
    2 "Mediocre - Low Rating in 1978"              ///
    3 "Medium Rating in 1978"                      ///
    4 "Not bad rating in 1978, considering"        ///
    5 "Excellent in 1978--the best of the year!"
            
    label values rep78 rep78
            
    splitvallabels rep78 if rep78>=3, length(11) recode
            
    graph bar mpg if rep78>=3, over(rep78, relabel(`r(relabel)') ) scheme(s1)
    
    exit
    
    
    
    
    
    // Combining graphs and including a common legend
    //this uses the user written command "grc1leg" which must be downloaded first
    
    net install grc1leg,from( http://www.stata.com/users/vwiggins/)
    
    clear	all
    
    
    input quest str25 q      a1 a2 a3 a4 a5 a6
    1 "Question 1"           0  2  37 45 12 4
    1 "Benchmark Q1"         2  5  25 47 17 4
    2 "Question 2"           1  37  2 40 17 3
    2 "Benchmark Q2"         2  5  25 47  4 17
    3 "Question 3"           1  2  40 37 17 3
    3 "Benchmark Q3"         2  5  25 47 17 4
    4 "Question 4"           1  2  37  17 3 40
    4 "Benchmark Q4"         2  5  47 25 17 4
    end	
    
    graph hbar a1-a6 if quest==1, percent over(quest, gap(1)) over(q, gap(10))   ///
    stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
    saving(a1, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)
    
    graph hbar a1-a6 if quest==2, percent over(quest, gap(1)) over(q, gap(10))   ///
    stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)lcolor(black))  ///
    saving(a1a, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)
    
    graph hbar a1-a6 if quest==3, percent over(quest, gap(1)) over(q, gap(10))    ///
    stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)  lcolor(black)) ///
    saving(a1b, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)
    
    
    graph hbar a1-a6 if quest==4, percent over(quest, gap(1)) over(q, gap(20))    ///
    stack legend(span rows(1) label(1 "Missing") label(2 "Never")                 ///
    label(3 "Rarely") label(4 "Occasionly  ") label(5 "Mostly")                   ///
    label(6 "Always    ") size(small))                                            ///
    yline(20 40 60 80 ,lwidth(0.25 ) lcolor(black)) saving(a2, replace)
    
    
    grc1leg a1.gph a1a.gph a1b.gph a2.gph, cols(1) ///
    imargin(0 0 0 0) ycommon xcommon legendfrom(a2.gph)
    
    exit
    
    
    *(Also see the user written program catplot on:
    http://www.survey-design.com.au/Usergraphs.html)
    
    
    
    
    
    // Table under x axis 
    
    clear all
    sysuse auto
     
    twoway ///
    (scatter mpg rep78, yscale(range(0 50))) , ysize(2) name(a1)  xlabel( 1(1)5 )
    
    contract rep78 for
    fillin rep78 for
    replace _freq=0 if missing(_freq)
    
    set obs `=_N+2'
    
    tostring _freq, replace
    replace rep78=0 in `=_N-1'
    replace rep78=0 in `=_N'
    
    replace for=1 in `=_N-1'
    replace for=0 in `=_N'
    
    replace _freq="foreign cars  " in `=_N-1'
    replace _freq="Domenstic cars" in `=_N'
    
    
    twoway ///
    (scatter    for rep78 if for==1, mlabel(_freq)  msymbol(none) mlabgap(-2)   ///
    mlabposition(12))                                                           ///
    (scatter for rep78 if for==0, mlabel(_freq) msymbol(none) mlabgap(-2)       ///
    mlabposition(6))                                                            ///
    , name(a2, replace) legend(off) ytitle("b")  fysize(5)  xscale( off)        ///
    yscale( off fill)  graphregion(color(ltblue ))  plotregion(color(ltblue ))  ///
    ylabel(,nogrid) xlabel( 1(1)5 )
      
    graph combine a1 a2, cols(1) xcommon imargin(0 0 0 0) 
    
    exit
    
    
    
    
    
    
    // Table on the side of a graph 
    
    sysuse auto, clear
    twoway (scatter mpg rep78) , graphregion( margin(r+30) )
    
    gr_edit AddTextBox added_text editor `=82+8' `=101'
    gr_edit added_text_new = 1
    gr_edit added_text_rec = 1
    gr_edit added_text[1].text = {}
    gr_edit added_text[1].text.Arrpush "Mean mpg by rep78"
    
    
    gr_edit AddTextBox added_text editor `=82+2' `=112'
    gr_edit added_text_new = 2
    gr_edit added_text_rec = 2
    gr_edit added_text[2].text = {}
    gr_edit added_text[2].text.Arrpush "rep78  mpg"
    
    collapse mpg, by(rep78)
    
    local z=2
    
    forvalues i=0/4 {  //rows
      local ++z
      gr_edit AddTextBox added_text editor `=80-(`i'*4)' `=114'
      gr_edit added_text_new = `z'
      gr_edit added_text_rec = `z'
      gr_edit added_text[`z'].text = {}
      gr_edit added_text[`z'].text.Arrpush ///
      "`=rep78[`z'-2]'      `=string(mpg[`z'-2],"%8.2f")' "
    }
    exit
    
    
    
    
    
    // Table on the side of a graph 
    
    sysuse auto, clear
    
    forval n = 1/20 {
    	loc sidetable `" `sidetable'  " `=make[`n']'  "  "'
    	}
    
    forval n = 1/20 {
    	loc sidecap `" `sidecap'  "      $`=price[`n']'"  "'
    	}
    	
    sysuse auto, clear
    twoway (scatter mpg rep78) ,        ///
    note("{bf: MAKE    }"               ///
    "-----------------"    `sidetable'  ///
    , size(medsmall) color(green)       ///
    position(2) margin(small)           /// 
    justification(left))                ///
    caption(" {bf:   Price }"           ///
    "---------"    `sidecap'            ///
    , size(medsmall) color(midgreen)    ///
    position(2) margin(vsmall)          /// 
    justification(right))
    exit
    
    
    
    //Based statalist Jun 9, 2011
    //Eric Booth
    
    
    
    
    
    // Putting Data set results in a graph
    
    clear all
    sysuse auto
     
    quiet: summarize mpg 
     
    twoway ///
    scatter mpg rep78, ///
    text(40 3 "The mean mpg is:`=string(`=r(mean)',"%8.2f")'", placement(right)) /// 
    text(38 3 "The sd mpg is: `=string(`=r(sd)',"%8.2f")'" ,   placement(right))
    
    exit
    
    
    
    
    
    
    // Adding a picture as background to a graph 
    
    //your picture is converted by:  http://www.text-image.com/convert/ascii.html
    //set width to 500
    
    set more off
    infix str1 a1-a500 1-500 using "car ascii to text big.txt",  clear
    
    //hexdump may be needed to look for unexpected characters
    //hexdump "c:/car ascii to text big.txt", tabulate
    
    //scaling y axis picture data and reversing
    generate y=90+((_N-_n+1)*1)
    
    reshape long a, i(y) j(col)
    
    //replacing characters with numbers
    replace a="1" if a=="+"
    replace a="2" if a=="-"
    replace a="3" if a=="."
    replace a="4" if a=="/"
    replace a="5" if a==":"
    replace a="6" if a=="-"
    replace a="7" if a=="M"
    replace a="8" if a=="N"
    replace a="9" if a=="`"
    replace a="10" if a=="d"
    replace a="11" if a=="h"
    replace a="12" if a=="m"
    replace a="13" if a=="o"
    replace a="14" if a=="s"
    replace a="15" if a=="y"
    
    
    //scaling x axis picture data
    replace col=col/10
    
    merge 1:1 _n using  "C:\Program Files\stata11\ado\base/a/auto.dta"
    
    local size="tiny"
    
    twoway ///
    (scatter y col if a=="1" ,msymbol(circle) ///
    mcolor("0 0 0*.20") msize(`size') )       /// 
    (scatter y col if a=="2" ,msymbol(circle) ///
    mcolor("255 255 255*.2") msize(`size'))   ///
    (scatter y col if a=="3" ,msymbol(circle) ///
    mcolor("255 0 0*.2") msize(`size') )      ///
    (scatter y col if a=="4" ,msymbol(circle) ///
    mcolor("0 0 255*.2") msize(`size') )      ///
    (scatter y col if a=="5" ,msymbol(circle) ///
    mcolor("0 0 255*.2") msize(`size') )      ///
    (scatter y col if a=="6" ,msymbol(circle) ///
    mcolor("255 255 0*.2") msize(`size') )    ///
    (scatter y col if a=="7" ,msymbol(circle) ///
    mcolor("0 255 255*.2") msize(`size') )    ///
    (scatter y col if a=="8" ,msymbol(circle) ///
    mcolor("255 0 255*.2") msize(`size') )    ///
    (scatter y col if a=="9" ,msymbol(circle) ///
    mcolor(white) msize(`size') )             ///
    (scatter y col if a=="10",msymbol(circle) ///
    mcolor(orange*.2) msize(`size') )         ///
    (scatter y col if a=="11",msymbol(circle) ///
    mcolor(brown*.2) msize(`size') )          ///
    (scatter y col if a=="12",msymbol(circle) ///
    mcolor(sand*.2) msize(`size') )           ///
    (scatter y col if a=="13",msymbol(circle) ///
    mcolor(black*.2) msize(`size') )          ///
    (scatter y col if a=="14",msymbol(circle) ///
    mcolor(black*.2) msize(`size') )          ///
    (scatter y col if a=="15",msymbol(circle) ///
    mcolor(black*.2) msize(`size') )          ///
    (scatter  length mpg)                     ///
    (lfit  length mpg)                        ///
    ,yscale( range(100 250))                  ///
    ytitle(Length ins.) xtitle(mpg)           ///
    title(Mpg v Length) legend(off)
    
    exit
    
    
    
    
    
    
    
    // Adding a picture as background to a graph
    
    //The above picture can be improved on, if required, by converting a picture
    //into html and then cleaning this file up in Stata.
    
    //The picture is first loaded into GIMP (free software program) and then saved
    //as html (Remmember the larger the picture the more computer resources are
    //required and the longer it will take to process the code)
    
    
    clear all
    set more off
    set memory 900m
    
    
    program hex
    
      syntax varname , Newv(name)
    
        generate `newv'=((strpos("`=c(alpha)'",`varlist')-1)/2)+10
        replace `newv'=real(`varlist') if `newv'==9.5
    
    end
    
    
    //values that can be changed
    local divide=8     //sample size
    
    
    
    
    insheet using "c:/fast car.html", clear
    
    generate row=.
    replace row=1 if strpos(v1,"< TR>")
    replace row=sum(row)
    
    keep if strpos(v1, " < TD  BGCOLOR")
    
    split v1,p(#)
    split v12,p(>)
    keep row v121
    
    generate obs=_n
    
    bysort row (obs):gen col=_n
    
    rename v121 hex
    generate hex1=substr(hex,1,1)
    generate hex1a=substr(hex,2,1)
    
    generate hex2=substr(hex,3,1)
    generate hex2a=substr(hex,4,1)
    
    generate hex3=substr(hex,5,1)
    generate hex3a=substr(hex,6,1)
    
    hex hex1, newv(dec1)
    hex hex1a, newv(dec1a)
    
    hex hex2, newv(dec2)
    hex hex2a, newv(dec2a)
    
    hex hex3, newv(dec3)
    hex hex3a, newv(dec3a)
    
    
    generate dec1b=16*dec1+dec1a
    generate dec2b=16*dec2+dec2a
    generate dec3b=16*dec3+dec3a
    
    keep dec1b dec2b dec3b row col
    
    generate dec1c=floor(dec1b/`divide')*`divide'
    generate dec2c=floor(dec2b/`divide')*`divide'
    generate dec3c=floor(dec3b/`divide')*`divide'
    
    gen str20 color=char(34)+string(dec1c,"%3.0f")+" "+string(dec2c,"%3.0f") ///
    +" "+string(dec3c,"%3.0f")+char(34)
    
    egen group_col=group(color)
    
    compress
    save pict_graph, replace
    
    use pict_graph, clear
    quiet summarize col
    
    local size="*`=33/`r(max)''"     //marker size, can be adjusted
    
    levelsof color, local(a) 
    
    merge 1:1 _n using  "C:\Program Files\stata11\ado\base/a/auto.dta"
    summarize length
    
    gen y=90+(((_N-_n+1)-90))/(_N/r(max))
    
    //scaling x axis picture data
    replace col=col/9
    
    local z=1
    
    foreach i of local a {
    
      if `z'==1 {
    local zz =  ///
    `"(scatter y col if group_col==`z',msymbol(square) mcolor(`i') msize(`size'))"'
    
        local z=2
       }
          else {
    local zz1 = ///
    `"(scatter y col if group_col==`z' ,msymbol(square) mcolor(`i') msize(`size'))"'
    
    local zz "`zz' `zz1'"
    local ++z
          }
    }
    
    twoway `zz'                                      ///
    (scatter  length mpg, mcolor(red) msize(large))  ///
    (lfit  length mpg)                               ///
    ,yscale( range(100 325))                         ///
    ytitle(Length ins.) xtitle(mpg)                  ///
    title(Mpg v Length) legend(off)
    
    exit
    
    
    


     Adding confidence bars using data obtained from Stata's ci command.
    
    
    sysuse auto, clear
    
    egen mpg1=cut(mpg), group(5) icode
    
    levelsof mpg1, local(kk)
    
     gen mean=.
     gen ub=.
     gen lb=.
     gen loc=.
     local z=1
     
    foreach i of local kk {
     ci for if mpg1==`i', wilson binomial
     
      replace mean=r(mean) in `z'
      replace ub=r(ub) in `z'
      replace lb=r(lb) in `z'
      replace loc=`i' in `z'
      local ++z
     }
     
     label define a 0 "very poor"  1 poor  2  OK 3 good  4 vgood
     label value  loc a
     
    twoway (bar mean loc) (rcap ub lb loc), ///
    xlab(, valuelabel angle(45))            ///
    xtitle("Fuel Consumption")              ///
    ytitle(mean) legend(off)
    
    
    


     Confidence-interval plots ci
    
    
    webuse nlswork, clear
    
    local ytitle "`: var label ln_wage'"
    
    set seed 2803
    generate rnd = runiform()
    bysort idcode (rnd): generate byte select = _n == 1
    statsby mean=r(mean) ub=r(ub) lb=r(lb) N=r(N) if select, ///
    by(grade) clear: ci ln_wage
    
    levelsof grade, local(levels)
    
    twoway rcap ub lb grade || ///
    scatter mean grade, ytitle(`ytitle') legend(off) ///
    subtitle("95% confidence intervals for mean", place(w)) xlabel(`levels')
    
    
    
    // Odds ratio graph
    
    ** requires 2 user written graphs: parmest and egenmore
    ** if not already installed:
    
    ssc install parmest
    ssc install egenmore
    
    
    
    sysuse nlsw88, clear
    logit union married never_married collgrad south
    
    parmest, norestore eform
    egen axis= axis(estimate), label(parm)
    
    twoway bar  estimate axis, base(1) horizontal barw(.5) ///
                xline(1) ylab(, valuelabel angle(0)) ||    ///
           rcap min95 max95 axis, horizontal               ///
                legend(order(1 "point estimate"            ///
                             2 "95% conf. int.") pos(6))   ///
                xtitle("odds ratio") ytitle("")
    
    
    *(Maarten L. Buis - Statalist,  14/09/2010)
    
    // Adding labels - standard error bar chart
    
    webuse assembly, clear
    
    generate upper=mean+2*std
    
    serrbar mean std date, scale(2) yline(195)              ///
    addplot( scatter  up date, msymbol(none) mlabel(upper))
    
    
    // Odds ratio graph - selecting order of bars
    
    ** requires 2 user written graphs: parmest and egenmore
    ** if not already installed:
    
    ssc install parmest
    ssc install egenmore
    
    
    sysuse nlsw88, clear
    logit union married never_married collgrad south
    
    parmest, norestore eform
    
    tab estimate, gen(kk)
    
    //order of bars
    input a 
    1
    2
    3
    4
    5
     
    egen axis= axis( a estimate), label(parm)
    
    twoway bar  estimate axis, base(1) horizontal barw(.5) ///
    xline(1) ylab(, valuelabel angle(0)) ||                ///
    rcap min95 max95 axis, horizontal                      ///
    legend(order(1 "point estimate"                        ///
    2 "95% conf. int.") pos(6))                            ///
    xtitle("odds ratio") ytitle("") scheme(color)
    
    *(Maarten L. Buis - Statalist,  14/09/2010)
    
    
    
    // Odds ratio graph
    
    ** requires 2 user written graphs: parmest and egenmore
    ** if not already installed:
    
    ssc install parmest
    ssc install egenmore
    
    
    
    sysuse nlsw88, clear
    logit union married never_married collgrad south
    
    parmest, norestore eform
    egen axis= axis(estimate), label(parm)
    
    twoway scatter   axis estimate, ///
                xline(1) ylab(, valuelabel angle(0)) ||    ///
           rcap min95 max95 axis, horizontal               ///
                legend(order(1 "point estimate"            ///
                             2 "95% conf. int.") pos(6))   ///
                xtitle("odds ratio") ytitle("")
    
    
    //Paired Plot
    
    sysuse auto, clear
    
    gsort -mpg
    keep in 1/25
    
    replace mpg = round(mpg)
    
    gen rank1 = _n
    egen rank2 = rank(-price)
    
    gen byte one = 1
    gen byte two = 2
    gen left = 0.4
    
    twoway                                                  ///
    (pcspike rank1 one rank2 two,                           ///
    xlabel(none) xscale(noline range(0.3 2.3)) xtitle("")   ///
    yscale(r(-1 .) reverse off) ylabel(, nogrid))           ///
    (scatter rank1 one, mlabel(mpg) mlabpos(9) ms(none))    ///
    (scatter rank2 two, mlabel(price) mlabpos(3) ms(none))  ///
    ( scatter rank1 left, mla(make) mlabpos(3) ms(none)     ///
    text(-0.5 1 "mpg, US gallons")                          ///
    text(-0.5 2 "price , {c S|}US in 1978")                 ///
    legend(off) graphregion(color(white)))
    
    
    //Paired Plot 1
    //Looking at the effect of treatment on the weight of anorexic patients
    //those under the line have lost weight.
    
    //Making up the data; based on the data found at:
    //http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt
    
    
    clear
    set obs 76
    gen treatment=mod(_n,3)+1
    
    generate w_before1=rnormal(82, 4.8) 
    replace w_before1=rnormal(81, 5.7)  if treatment==2
    replace w_before1=rnormal(83, 5)    if treatment==3
    
    generate w_after1=rnormal(85.6, 8.3)
    replace w_after1=rnormal(81.1, 4.7) if treatment==2
    replace w_after1=rnormal(90, 5.4)   if treatment==3
    //the end of making up the data
    
    label var w_before " Weight before treatment, lb"
    label var w_after  " Weight after treatment, lb"
    label define treat ///
            1  "Cognitive behavioural"     ///
            2  "Control"                   ///
            3  "Family therapy"
    		
    label values treatment treat
    	
    		
    scatter w_after w_before w_before,      ///
    msymbol(Oh i) c(. l) lc(none gs12)      ///
    sort(w_before) yla(, ang(h))            ///
    ytitle(" Weight after treatment, lb")   ///
    by(treatment, row(1) note("weight, lb") legend(off))
    
    
    
    //From Stata Journal Article Vol 9 No 4
    //Speaking Stata: Paired, parallel, or profile plots
    //for changes, correlations, and other comparisons
    //Nicholas J. Cox
    //Contact us to subscribe to the Stata Journal
    
    
    //Paired Plot 2
    //Looking at the effect of treatment on the weight of anorexic patients.
    
    //Making up the data; based on the data found at:
    //http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt
    
    clear
    set obs 76
    gen treatment=mod(_n,3)+1
    
    gen w_before1=rnormal(82, 4.8) 
    replace w_before1=rnormal(81, 5.7)  if treatment==2
    replace w_before1=rnormal(83, 5)    if treatment==3
    
    gen w_after1=rnormal(85.6, 8.3)
    replace w_after1=rnormal(81.1, 4.7) if treatment==2
    replace w_after1=rnormal(90, 5.4)   if treatment==3
    //end of making up the data
    
    label var w_before " Weight before treatment, lb"
    label var w_after  " Weight after treatment, lb"
    label define treat ///
            1  "Cognitive behavioural" ///
            2  "Control"               ///
            3  "Family therapy"
    		
    label values treatment treat
    	
    gen byte one = 1
    gen byte two = 2	
    	
    twoway ///
    pcspike w_before one w_after two if w_before <= w_after, ///
    lcolor(gs12) ||                                          ///
    pcspike w_before one w_after two if w_before > w_after,  ///
    lw(*1.2) lcolor(gs2) xla(1 " before" 2 "after ")         ///
    xtitle("") yla(, nogrid ang(h)) ytitle("weight, lb")     ///
    by(treatment, row(1) note("") legend(off) noixtick)	
    
    
    
    //From Stata Journal Article Vol 9 No 4
    //Speaking Stata: Paired, parallel, or profile plots
    //for changes, correlations, and other comparisons
    //Nicholas J. Cox
    //Contact us to subscribe to the Stata Journal.
    
    
    //Paired Plot 3
    //Looking at the effect of treatment on the weight of anorexic patients.
    
    //Making up the data; based on the data found at:
    //http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt
    
    clear
    set obs 76
    gen treatment=mod(_n,3)+1
    
    gen w_before1=rnormal(82, 4.8) 
    replace w_before1=rnormal(81, 5.7)  if treatment==2
    replace w_before1=rnormal(83, 5)    if treatment==3
    
    gen w_after1=rnormal(85.6, 8.3)
    replace w_after1=rnormal(81.1, 4.7) if treatment==2
    replace w_after1=rnormal(90, 5.4)   if treatment==3
    //end of making up the data
    
    label var w_before " Weight before treatment, lb"
    label var w_after  " Weight after treatment, lb"
    label define treat ///
            1  "Cognitive behavioural" ///
            2  "Control"               ///
            3  "Family therapy"
    		
    label values treatment treat
    	
    
    bysort treatment (w_before w_after) : gen order1 = _n - _N/2
    	
    twoway                                                   ///
    pcarrow w_before order1 w_after order1, pstyle(p1)       ///
    || scatter w_before order1, pstyle(p1) ms(o)             ///
    xla(none) xtitle("") yla(, ang(h)) ytitle("weight, lb")  ///
    by(treatment, row(1) note("") legend(off))
    	
    
    
    //From Stata Journal Article Vol 9 No 4
    //Speaking Stata: Paired, parallel, or profile plots
    //for changes, correlations, and other comparisons
    //Nicholas J. Cox
    //Contact us to subscribe to the Stata Journal.
    
    
    //Parallel Coordinate Plot
    //Rescaling variables and plotting
    
    
    sysuse auto, clear
    drop make
    set more off
    
    local z=1
    foreach i of varlist _all {
    
    summarize `i', detail
    
    #delimit ;
      local a `"`a' text( 1.2 `z' `=char(34)'`i'`=char(34)' 
      `=char(34)'`=string(`r(max)',"%8.2f")'`=char(34)' ,  
      orientation(vertical) placement(n) size(3) justification(left) ) 
      text( -0.1 `z' `=char(34)' `i'`=char(34)'
      `=char(34)'`=string(`r(min)',"%8.2f")'`=char(34)' , 
      orientation(vertical) placement(south) size(3) justification(right) ) "'; 
     #delimit cr
    
    //scaling variables  
    generate x`z'=(`i' -r(min))/(r(max)-r(min))
    local ++z
    }
    drop if rep78==.
    generate id=_n
    reshape long x, i(id) j(type)
    
    drop if x>100
    
    twoway                                       ///
    (line  x type if for==1, lcolor(red) c(L))   ///
    (line  x type if for==0, lcolor(blue) c(L)), ///
    xline(1/12)                                  /// 
    `a' yscale( off range(0 1.1)) xscale(off)    ///
    text(0 0 "MIN")  text(1 0 "Max")             ///
    graphregion(margin(t=20 b=20)) legend(off)
    
    //Transparent graphs
    //Using IrfanView - free image software
    //http://www.irfanview.com/
    
    histogram mpg if for==0, percent color(red) ///
    xscale(range(10(5)45)) barwidth(3) yscale(range(0(10)60))
    
    graph export "c:/a1.png", height(376)  width(506) replace
    shell "C:\Program Files\IrfanView\i_view32.exe" "c:\a1.png" 
    
    histogram mpg if for==1, percent color(blue) ///
    xscale(range(10(5)45)) barwidth(3)  yscale(range(0(10)60))
    
    graph export "c:/a2.png", height(376) width(506) replace
    
    //Then in Irfanview
    //Edit pulldown menu
    //Insert overlay/watermark image
    //select graph  eg. a2.png
    //The "overlay transparency" setting used was 80%
    
    
    //Using Arrows
    **The example below uses data from:  http://www.bom.gov.au/climate/data/
    cd "C:\"
    insheet using "temperature data.csv", clear  //or whatever data you have
    set more off
    keep if inlist(year,1940, 1950, 1960, 1970,2010)
    collapse (mean) max, by(month year)
    sort year month
    generate date1=_n
    
    tsset date1  //set to time series so lag "l." and 
                 // forward "f." operators can be used
    			 // see help tsvarlist
    			 
    twoway pcarrow max l.max f.max max, mlab(date1) ///
    ytitle("This month Deg. C")                            ///
    xtitle("Previous month Deg. C")                        ///
    title("Temperature Cycle Melbourne - Deg. C")   ///
    subtitle("Years 1940 1950 1960 1970 2010")
    //Also see: Stata Journal Article Vol 5 No 2 //The arrows of outrageous fortune by Nicholas J. Cox //http://www.stata-journal.com/sjpdf.html?articlenum=gr0015 //Contact us to subscribe to the Stata Journal.

    
    //tsline with confidence intervals - using margins
    
    
    Requires: parmest
    to download this program type the following on the Stata command
    line (if not already loaded):
    ssc install parmest
    
    Requires: tsmktim
    to download this program type the following on the Stata command
    line (if not already loaded):
    ssc install tsmktim
    
    To run the example: copy the following into a do file and run
    
    
    
    webuse grunfeld, clear
    qui reg invest i.year i.company
    margins i.year, post
    // Roger Newson's parmest from SSC
    parmest, saving(yeareff, replace)
    use yeareff, clear
    // Kit Baum's tsmktim from SSC
    tsmktim year, start(1935)
    tsline estimate || tsrline min95 max95, legend(col(1)) scheme(s2mono)
    
    
    From Statalist:
    22/11/2010 5:47 AM
    Kit Baum 
    
    
    
    //Predictive margins with 95% confidence interval - using margins
    
    
    Requires: parmest
    to download this program type the following on the Stata command
    line (if not already loaded):
    ssc install parmest
    
    
    
    
    sysuse auto, clear
    gen kprice=price/1000
    regress  mpg c.kprice##c.kprice i.foreign i.rep78 turn trunk headroom
    
    
    margins, at(kprice=(3/15)) vsquish post
    
    return list
    ereturn list
    
    parmest, norestore  //saves parameters
    l
    gen kprice=3 + _n - 1
    l
    label var kprice "Price(\$1000)"
    
    twoway ///
    (rarea max95 min95 kprice, pstyle(ci)) ///
    (line estimate kprice), ///
    ytitle(Miles per gallon) ///
    title(Predictive margins with 95% confidence interval) ///
    xlabel(3(3)15 10.4) legend(off)
    
    From Stata News Vol 25 No 3
    
    In Stata 12 see marginsplot
    
    
    //Generating predictions and graphing them
    
    
    sysuse auto, clear
    
    // estimate the model
    reg price c.mpg##c.weight##c.weight i.foreign
    
    // collect some information about mpg
    sum mpg
    local m = r(mean)
    local sd = r(sd)
    
    // we are going to change the data, so we preserve it first
    preserve
    
    // fix the other explanatory variables
    // In this case we only look at US ("domestic") cars
    replace foreign = 0
    
    // predict the price while fixing mpg at different values
    replace mpg = `m'
    predict yhatm
    
    replace mpg = `m' - `sd'
    predict yhatl
    
    replace mpg = `m' - 2*`sd'
    predict yhatll
    
    replace mpg = `m' + `sd'
    predict yhath
    
    replace mpg = `m' + 2*`sd'
    predict yhathh
    
    // nicer display of large numbers 1,000 instead of 1000
    format yhat* %8.0gc
    
    // create graph
    sort weight
    twoway line yhatll yhatl yhatm yhath yhathh weight, ///
           title("predicted price for US cars")         ///
           ytitle("predicted price (US {c S|})")        /// 
           lpattern(solid solid solid solid solid)      ///
           lcolor("255 255 204"                         ///
                  "161 218 180"                         ///
                  " 65 182 196"                         /// 
                  " 44 127 184"                         ///   
                  " 37  52 148" )                       ///
           legend( order( - "mpg"                       ///
                          1 "mean-2*sd"                 ///
                          2 "mean-1*sd"                 ///
                          3 "mean"                      ///
                          4 "mean+1*sd"                 ///
                          5 "mean+2*sd" ))
    
    // get our original data back
    restore
    
    
    martin buis
    Wed, 23 Feb 2011 10:57:36
    Statalist
    
    
    
    //Labelling in terms of another variable
    
    
    Requires: labmask
    to download this program type the following on the Stata command
    line (if not already loaded):
    ssc install labmask
    
    
    
    sysuse auto, clear
    sort price
    gen order = _n
    labmask order, values(make)
    twoway bar price order, horizontal ylabel(8 44 71, valve angle(h)) ytitle("")
    
    M Cox
    Statalist 21/12/10
    
    
    
    //Changing the order of twoway graphs where the by() option is used
    
    
    Requires: egenmore
    to download this program type the following on the Stata command
    line (if not already loaded):
    ssc install egenmore
    
    
    
    //the following is an example of order that the graphs are in when 
    //determined by Stata 
    // ie. lowest rep78 value to highest
     
    scatter mpg weight , by(rep78) name(a2, replace)
    
    
    //If you require the order to be the mean mpg of the levels of rep78,
    //the following can be done:  
      
    egen mean1 = mean(mpg) , by(rep78)
    
    egen axis = axis1(mean1 rep78), label(rep78) reverse  // egenmore command
    
    scatter mpg weight , by(axis) name(a1, replace)
    
    
    M Cox
    Statalist 21/03/11
    
    
    
    //Using the compact option in the by() option
    
    sysuse auto, clear
    
    forvalues i=10(10)40 {
      local m`i'=string(`i'*0.4251437,"%6.2f")
    }
    
    twoway (scatter mpg weight, yaxis(1 2) xlabel(,angle(45)) ///
    yscale( axis(2, alt))                                     ///
    ylabel(                                                   ///
    10 "`m10'"                                                ///
    20 "`m20'"                                                ///
    30 "`m30'"                                                ///
    40 "`m40'"                                                ///
    ,axis(2))) ,                                              ///
     by(for, title( "km/L",  orientation(vertical)            ///
     size(med) position(3)) xrescale compact)  
    
    
    
    //Adding titles to the left hand side of the graph
    
    
    sysuse auto, clear
    
    fillin rep78 foreign
    twoway scatter price mpg, by(foreign rep78, cols(5) compact)
    
    egen group = group(foreign rep78)
    
    label define group 1 "Poor" ///
    2 "Fair"         ///
    3 "Average"      /// 
    4 "Good"         ///
    5 "Excellent"    ///
    6" "             ///
    7" "             ///
    8" "             /// 
    9" "             ///
    10 " "  
     
     
    label value group group ///
     
     
     twoway scatter price mpg, ///
     by(group, cols(5)         ///
     r1title("Car type",       ///
     orientation(rvertical)    ///
     size(medsmall))           ///
     t1title("Repair status",  ///
     size(medsmall))           ///
     note("") compact)  
     
    
    gr_edit plotregion1.r1title[5].style.editstyle drawbox(yes) editcopy
    
    gr_edit plotregion1.r1title[5].as_textbox.setstyle, style(yes)
    gr_edit plotregion1.r1title[10].as_textbox.setstyle, style(yes)
    
    gr_edit plotregion1.r1title[5].text = {}
    gr_edit plotregion1.r1title[5].text.Arrpush Domestic
    
    gr_edit plotregion1.r1title[10].text = {}
    gr_edit plotregion1.r1title[10].text.Arrpush Foreign
    
    gr_edit plotregion1.r1title[5]._set_orientation rvertical
    
    
    
    
    For instructions on how to do this in the Stata graphics editor 
    see Stata Journal Vol 9 No 4
    
    For information on how to subscribe to the Stata Journal 
    please contact us.
    
    
    
    //Combining many graphs on a page
    
    sysuse auto, clear
    
    graph drop _all
    
    foreach i of varlist _all {
    capture confirm  numeric variable `i'
      if _rc==0 {
        histogram `i', name(`i')
        local z "`z' `i'"
      }
    }
    graph combine `z'
    
    
    
    
    
    
    //Producing a similar plot to the density plot at:
    http://www.survey-design.com.au/Usergraphs.html
    using Stata 12's new contour plot
    
    Requires: spgrid, spkde  & mylabels 
    to download these programs type the following 
    on the Stata command line (if not already loaded):
    
    ssc install spgrid
    and then
    ssc install spkde
    and then
    ssc install mylabels
    
    sysuse "auto.dta", clear
    set more off
    
    summarize price mpg
    clonevar x = mpg 
    clonevar y = price
    replace x = (x-0) / (50-0)
    replace y = (y-0) / (20000-0)
    mylabels 0(10)50, myscale((@-0) / (50-0)) local(XLAB)
    mylabels 0(5000)20000, myscale((@-0) / (20000-0)) local(YLAB)
    keep x y
    save "xy.dta", replace
    
    * 2. Generate a 100x100 grid
    
    spgrid, shape(hexagonal) xdim(100)    ///
    xrange(0 1) yrange(0 1)               ///
    dots replace                          ///
    cells("2D-GridCells.dta")             ///
    points("2D-GridPoints.dta")
    
    * 3. Estimate the bivariate probability density function
    
    spkde using "2D-GridPoints.dta",    ///
    xcoord(x) ycoord(y)                 ///
    bandwidth(fbw) fbw(0.1) dots        ///
    saving("2D-Kde.dta", replace)
    
    use "2D-Kde.dta", clear
    
    merge 1:1 _n using xy.dta
    
    twoway (contour p  spgrid_ycoord spgrid_xcoord  if p!=0 , ///
    levels(15))                                               ///
    (scatter y x, mcolor(black) msize(small) )                ///
    ,xlab(`XLAB', nogrid) xtitle("Mileage (mpg)")             ///
    ylab(`YLAB', nogrid)                                      ///
    ytitle("Price $US") plotregion(color(blue))
    
    
    
    
    //Mandelbrot Set
    Example taken from Stata News 
    http://www.stata.com/stata-news/statanews.27.4.pdf
    Please note that on my computer this took 3 hours to run.
    
    
    clear all
    
    mata:
    real scalar escape(complex scalar Z, C, real scalar R, maxiter)
    {
    real scalar i
    for (i=0; i < maxiter; i++) {
      if (norm(Z) <= R) {
       Z = Z*Z+C
    }
      else return(i)
      }
      return(i)
    }
    end
    
    
    mata:
    mandelbrot_set = J(201*201, 3, .)
    cnt = 1
    C = -0.8+0.156i
    for(i=0; i<=200; i++) {
     for(j=0; j<=200; j++) {
      Z = C(-2+i*4/200, -2+j*4/200)
      n = escape(Z, C, 100, 400)
      mandelbrot_set[cnt,.]= (i, j, n)
      cnt++
     }
    }
    end
    
    
    getmata (x y escape)= mandelbrot_set
     quiet summarize escape
     local min = r(min)-1
     local max = r(max)+1
     twoway contour escape y x, ccuts(`min'(1)`max')  ///
      clegend(off) graphr(m(zero)) xscale(off)        ///
      xlab(,nogrid)  yscale(off) ylab(,nogrid) scheme(s2color)
    
    exit
    
    
    
    Survivual Analysis Graphs
    
    //Generating Survival curves from two data sets
    
    
    
    
    
    use http://www.stata-press.com/data/r11/drug2.dta, clear
    stset 
    sts , name(a1, replace)
    
    sts generate newvar =s
    
    set obs `=_N+1'
    replace newvar=1 in l
    replace _t=0 in l
    replace died=0 in l
    replace s=1 in l
    keep newvar _t
    
    save c:/s1, replace
    
    
    use http://www.stata-press.com/data/r11/stan3.dta, clear
    replace t1=t1/40
    stset t1, id(id) failure(died)
    sts , name(a1a, replace)
    sts generate newvar1 =s
    set obs `=_N+1'
    replace newvar1=1 in l
    replace _t=0 in l
    keep newvar1 _t
    rename _t _t1
    
    merge 1:1 _n using c:/s1
    
    
    twoway (line  newvar1 _t1, sort c(stairstep)) ///
    (line  newvar _t, sort c(stairstep)),         ///
    ylabel(0(.2)1) name(a2, replace) ytitle("")   ///
    legend(label(1 data set 1) label(2 data set 2))
    
    
    
    
    //Generating Survival curve with confidence interval
    
    
    
    
    
    use http://www.stata-press.com/data/r11/drug2.dta, clear
    stset 
    
    sts gen km=s
    sts gen ub=ub(s)
    sts gen lb=lb(s)
    
    //Then something along the lines:
    twoway (rarea ub lb _t, sort) (line km _t, sort)
    
    
    From Statalist:
    Fri, 25 Feb 2011
    Phil Clayton
    
    
    //Generating Survival curve with lines at specific points in time
    
    
    
    
    
    use http://www.stata-press.com/data/r12/drug2, clear
    
    sts generate newvar1 =s
    sts generate lb = lb(s)
    sts generate ub = ub(s)
    
    gen kk=newvar1 if studytime==20
    sort kk
    local a1=kk[1]
    
    twoway  (rarea ub lb _t, sort  )                ///
    (line  newvar1 studytime, sort c(stairstep))    ///
    (pci  0 20 `a1' 20) (pci  `a1' 0  `a1' 20),     ///
    ylabel(0(.2)1) name(a2, replace) ytitle("")     ///
    legend(label(1 data set 1) label(2 data set 2)) ///
    scheme(s1color) legend(off) xtitle("Studytime") ///
    ylabel( .1(.2)1  ) ymlabel(  `a1', angle(45) )
    
    



    An Excellent book on Stata graphs is: