楼主: 匿名
20348 212

[Stata] [Stata] 如何添加直角坐标系及极坐标系及SDAS_绘图实例 [推广有奖]

41
niuniuyiwan 在职认证  发表于 2015-8-27 23:17:29
  1. /*Producing a similar plot to the density plot at:
  2. http://www.survey-design.com.au/Usergraphs.html
  3. using Stata 12's new contour plot

  4. Requires: spgrid, spkde  & mylabels
  5. to download these programs type the following
  6. on the Stata command line (if not already loaded):
  7. 耗时约3分钟
  8. */

  9. ssc install spgrid
  10. ssc install spkde
  11. ssc install mylabels

  12. sysuse "auto.dta", clear
  13. set more off

  14. summarize price mpg
  15. clonevar x = mpg
  16. clonevar y = price
  17. replace x = (x-0) / (50-0)
  18. replace y = (y-0) / (20000-0)
  19. mylabels 0(10)50, myscale((@-0) / (50-0)) local(XLAB)
  20. mylabels 0(5000)20000, myscale((@-0) / (20000-0)) local(YLAB)
  21. keep x y
  22. save "xy.dta", replace

  23. * 2. Generate a 100x100 grid

  24. spgrid, shape(hexagonal) xdim(100)    ///
  25. xrange(0 1) yrange(0 1)               ///
  26. dots replace                          ///
  27. cells("2D-GridCells.dta")             ///
  28. points("2D-GridPoints.dta")

  29. * 3. Estimate the bivariate probability density function

  30. spkde using "2D-GridPoints.dta",    ///
  31. xcoord(x) ycoord(y)                 ///
  32. bandwidth(fbw) fbw(0.1) dots        ///
  33. saving("2D-Kde.dta", replace)

  34. use "2D-Kde.dta", clear

  35. merge 1:1 _n using xy.dta

  36. twoway (contour p  spgrid_ycoord spgrid_xcoord  if p!=0 , ///
  37. levels(15))                                               ///
  38. (scatter y x, mcolor(black) msize(small) )                ///
  39. ,xlab(`XLAB', nogrid) xtitle("Mileage (mpg)")             ///
  40. ylab(`YLAB', nogrid)                                      ///
  41. ytitle("Price $US") plotregion(color(blue))
复制代码

Graph.png



42
niuniuyiwan 在职认证  发表于 2015-8-28 09:35:33
  1. sysuse auto, clear

  2. twoway__histogram_gen mpg  , bin(6) generate(h x , replace )

  3. count if !missing(x)

  4. forvalues i=1/`=r(N)' {

  5.   local label `label'  `=string(x[`i'],"%8.2f")'

  6. }

  7. histogram mpg , bin(6) xlabel(`label')
复制代码

Graph.png



43
niuniuyiwan 在职认证  发表于 2015-8-28 18:15:56
  1. // Different color bars

  2. clear

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

  16. label define kk        ///
  17. 1  "Barbora - CZE "    ///
  18. 2  "Christina - GER "  ///
  19. 3  "Linda - GER "      ///
  20. 4  "Sunette - RSA"     ///
  21. 5  "Huihui - CHN"      ///
  22. 6  "Keshorn - TRI "    ///
  23. 7  "Oleksandr - UKR "  ///
  24. 8  "Antti - FIN "      ///
  25. 9  "Vitezslav - CZE"   ///
  26. 10 "Tero - FIN"

  27. label value pos kk

  28. label define w 1 "Women" 0 "Men"
  29. label value w w

  30. graph bar (asis) mark,  over(id ) over(pos,label(angle(45))) over(w) ///
  31. ylabel( 0(10)90, angle(45))                                         ///
  32. blabel(bar, position(inside) format(%9.1f) color(black))            ///
  33. scheme(s2color)  legend(off)                                        ///
  34. bargap(5) title("London Olympics 2012" "Javelin")                   ///
  35. nofill ytitle("Metres")                                             ///
  36. bar(1, color(gold))      ///
  37. bar(6, color(gold))      ///
  38. bar(2, color(gs13))      ///
  39. bar(7, color(gs13))      ///
  40. bar(3, color(sienna))    ///
  41. bar(8, color(sienna))    ///
  42. bar(4, color(cyan))      ///
  43. bar(9, color(cyan))      ///
  44. bar(5, color(pink))      ///
  45. bar(10, color(pink))


  46. exit
复制代码

Graph.png



44
niuniuyiwan 在职认证  发表于 2015-8-29 09:26:17
  1. // Stacking some levels of a variable and not others
  2. // Has the years 71 and 77 stacked and year 88 normaly displayed

  3. webuse union, clear
  4. keep idcode year age
  5. recode year (71 77=1) (88=2) (*=.), gen(which)
  6. graph bar age, over(year) over(which) asyvars stack

  7. exit

  8. Statalist
  9. Nick Cox 22/12/11
复制代码

Graph.png



45
niuniuyiwan 在职认证  发表于 2015-8-29 09:27:31
  1. // bar graph - categorical variables - proportions

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

  8. capture erase kk1.dta
  9. local z=1

  10. foreach i of varlist for mpg1 mpg2 {
  11.   use kk, clear
  12.   keep `i'
  13.   contract `i'
  14.   egen total=total(_freq)
  15.   replace _freq=_freq/total
  16.   generate type=`z'
  17.   capture append using kk1
  18.   save kk1, replace
  19.   local ++z
  20. }

  21. drop mp* for
  22. bysort type : gen t1=_n

  23. label define kk 1 cat1 2 cat2 3 cat3
  24. label value type kk

  25. graph bar (mean) _freq , over(t1,sort(_freq) ) over(type, ) showyvars ///
  26. asyvar legend(off) nofill ytitle("Proportion") scheme(s2color)
复制代码

Graph.png



46
niuniuyiwan 在职认证  发表于 2015-8-29 09:30:34
  1. // The type of bar label is limited in Stata graphs but sometimes
  2. // additional information is required. This can be added with the
  3. // by adding another twoway plot.

  4. sysuse census, clear
  5. graph bar (sum) marri div, over(region) stack

  6. more

  7. collapse (sum) marri div, by(region)
  8. gen ratio = div / (marri + div)
  9. gen sratio = string(ratio, "%4.3f")
  10. gen marriPdiv = marria + div

  11. twoway bar marria region, barw(0.6) base(0) ///
  12. || rbar marria marriPdiv region, barw(0.6) xla(1/4, noticks valuelabel) ///
  13. || scatter marriPdiv region, ms(none) mla(sratio) mlabpos(12)  ///
  14. legend(order(1 "marriages" 2 "divorces")) ///
  15. yla(0 4e5 "400"  8e5 "800" 12e5 "1200" , ang(h)) ytitle(thousands) ///
  16. ysc(r(. 14e5))

  17. *(Nick Cox - Stata list,  Thu, 17 Feb 2011)
复制代码


Graph.png


Graph.png (11.96 KB)

Graph.png

47
niuniuyiwan 在职认证  发表于 2015-8-29 23:23:58
  1. // Controlling date labels
  2. clear

  3. input str40 time       x
  4. "5jan2011 15:00:30"    1
  5. "1feb2011 17:30:32"    2
  6. "1march2011 18:10:30"  6
  7. "1april2011 05:05:01"  2
  8. "1march2011 15:10:30"  12
  9. end
  10. list

  11. generate double time1=clock(time,"DMY hms")

  12. local t1=tc(1jan2011 12:00:0)
  13. local t2=1000*60*60*24*10
  14. local t3=tc(1may2011 12:0:0)

  15. scatter x time1, xlab(`t1'(`t2')`t3' ,                ///
  16. format(%tcddmonccyy_hh:mm) angle(45)) xtitle("Time")

  17. *(Nick Cox - Stata list,  Monday, 23 Jan 2011)
复制代码

Graph.png



48
niuniuyiwan 在职认证  发表于 2015-8-29 23:25:17
  1. //2nd Row of axis labels

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

  10. *(Maarten Buis - Stata list,  Thu, 7 Nov 2013)
复制代码

Graph.png



49
niuniuyiwan 在职认证  发表于 2015-8-29 23:26:40
  1. // Adding frequency count bar labels in bold font

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

  4. program g2
  5. syntax varname, over(varname)

  6. levelsof `over', local(kk)

  7. graph bar  `varlist'  , over(`over')                 ///
  8. blabel(bar, position(base) gap(*22.5) format(%9.1f)  ///
  9. size(large) color(black))

  10. local x=1
  11. foreach i of local kk {
  12.   summ `varlist' if `over'==`i'
  13.   local a1="N= "+string(r(N),"%8.0f")

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

  19. end

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

  22. g2 weight , over(pr2)
复制代码

Graph.png



50
niuniuyiwan 在职认证  发表于 2015-8-30 10:15:52
  1. // Coloring bars in order of magnitude. All bars not covered by the first and
  2. // second places are considered (colored) as 3rd place)

  3. clear
  4. input country gdp
  5. 1        100       
  6. 2        110       
  7. 3        240       
  8. 4        50       
  9. 5        10       
  10. end

  11. list

  12. gsort -gdp

  13. gen gdpred=gdp if _n==1
  14. gen gdpgreen=gdp if _n==2
  15. gen gdpblue=gdp if _n>2

  16. list

  17. graph hbar gdpred gdpgreen gdpblue, over(country, sort(gdp)          ///
  18.       descending) nofill  legend(label(1 "Best") label(2 "2nd best") ///
  19.           label(3 "rest") rows(1)) bar(1, color(cranberry))              ///
  20.           bar(2, color(dkgreen)) bar(3, color(dknavy))

  21. *(Sergiy Radyakin - Stata list,  Sat, 19 Feb 2011)
复制代码

Graph.png



您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 16:35