楼主: 匿名
20361 212

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

61
niuniuyiwan 在职认证  发表于 2015-8-30 23:07:59
  1. // Displaying part of a histogram

  2. sysuse auto, clear

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

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

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

  10. graph combine a1 a2 ,xcommon cols(1)
复制代码

Graph4.png



62
niuniuyiwan 在职认证  发表于 2015-8-30 23:09:05
  1. // Changing bar labels

  2. sysuse auto, clear

  3. graph bar  mpg  gear, showyvars ///
  4. yvaroptions(relabel(1 "MPG" 2"GEAR RATIO")) legend(off)

  5. exit
复制代码

Graph445.png



63
niuniuyiwan 在职认证  发表于 2015-8-30 23:10:43
  1. // Changing bar labels
  2. //It is sometimes more convenient to use value lables rather than a graph
  3. //label options to change graph bar labels. In the example it also allows the
  4. //legend to be spread over the width of the graph.

  5. clear all
  6. sysuse auto

  7. lab def origin 0 "Europe de l`=char(146)'Ouest" ///
  8. 1 "Asie de l`=char(146)'Est", modify

  9. graph hbar mpg trunk turn, over(foreign) ///
  10. legend(row(1) span) stack name(two,replace)

  11. exit

  12. *( Stata list,  25 Jun 2011)
复制代码

two.png



64
niuniuyiwan 在职认证  发表于 2015-8-30 23:12:09
  1. // Different color bars for the over option - Using twoway graph
  2. clear all

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

  16. label define w 1 "Women" 0 "Men"
  17. label value w w

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

  30. label value id  kk

  31. replace id=id+1 if id>5

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

  34. twoway (bar mark id if id==1, base(0) ylabel(0(10)90) ///
  35. xtitle("Men                                  Women")  ///
  36. xlabel(1(1)11,valuelabel angle(45))                   ///
  37. barwidth(.4)   color(cyan))                           ///
  38. (bar mark id if id==2,  barwidth(.4)   color(cyan))   ///
  39. (bar mark id if id==3,  barwidth(.4)   color(cyan))   ///
  40. (bar mark id if id==4,  barwidth(.4)   color(cyan))   ///
  41. (bar mark id if id==5,  barwidth(.4)   color(cyan))   ///
  42. (bar mark id if id==7,  barwidth(.4)   color(red))    ///
  43. (bar mark id if id==8,  barwidth(.4)   color(red))    ///
  44. (bar mark id if id==9,  barwidth(.4)   color(red))    ///
  45. (bar mark id if id==10, barwidth(.4)   color(red))    ///
  46. (bar mark id if id==11, barwidth(.4)   color(red))    ///
  47. (scatter mark id,  mlabel(mark) msymbol(none)         ///
  48. mlabposition(12)), legend(off)                        ///
  49. title("London Olympics 2012" "Javelin")               ///
  50. plotregion(margin(l=5 b=0))

  51. exit
复制代码

Graph44.png



65
niuniuyiwan 在职认证  发表于 2015-8-30 23:15:01
  1. // Difference from Average by levels of groups

  2. sysuse auto, clear

  3. graph drop _all

  4. bysort for rep78 : egen mpg1=mean(mpg)
  5. bysort  rep78: egen mpg2=mean(mpg)

  6. gen mpg3=mpg1-mpg2

  7. replace mpg2=0 if rep78==.

  8. twoway ///
  9. (bar mpg3 rep78 if for==0 & rep78==1 ,fcolor(green) ///
  10. lcolor(green) barwidth(.5)horiz  yscale(noline))  ///
  11. (bar  mpg3 rep78 if for==0 & rep78==2 ,fcolor(blue) ///
  12. lcolor(blue) barwidth(.5)horiz  yscale(noline))   ///
  13. (bar  mpg3 rep78 if for==0 & rep78==3 ,fcolor(red)  ///
  14. lcolor(red) barwidth(.5)horiz  yscale(noline))    ///
  15. (bar  mpg3 rep78 if for==0 & rep78==4 ,fcolor(pink) ///
  16. lcolor(pink) barwidth(.5)horiz  yscale(noline))   ///
  17. (bar  mpg3 rep78 if for==0 & rep78==5 ,fcolor(cyan) ///
  18. lcolor(cyan) barwidth(.5)horiz  yscale(noline)) , ///
  19. xtitle(MPG) xline(0)  ///
  20. title("Foreign made cars") legend(off) name(k1, replace)

  21. twoway ///
  22. (bar  mpg3 rep78 if for==1 & rep78==1 ,fcolor(green) ///
  23. lcolor(green) barwidth(.5)horiz  yscale(noline)) ///
  24. (bar  mpg3 rep78 if for==1 & rep78==2 ,fcolor(blue) ///
  25. lcolor(blue) barwidth(.5)horiz  yscale(noline))   ///
  26. (bar  mpg3 rep78 if for==1 & rep78==3 ,fcolor(red)  ///
  27. lcolor(red) barwidth(.5)horiz  yscale(noline))    ///
  28. (bar  mpg3 rep78 if for==1 & rep78==4 ,fcolor(pink) ///
  29. lcolor(pink) barwidth(.5)horiz  yscale(noline))    ///
  30. (bar  mpg3 rep78 if for==1 & rep78==5 ,fcolor(cyan) ///
  31. lcolor(cyan) barwidth(.5)horiz  yscale(noline)) , ///
  32. xtitle(MPG) xline(0) yscale(off) ///
  33. title("Domestically made cars") legend(off) name(k2, replace)

  34. graph combine k1 k2, row(1)  imargin(10 0 0 0) ycommon ///
  35. title("Difference from average of mpg for " ///
  36. "Foreign or Domestically produced cars" " " )
复制代码

Graph1.png



66
niuniuyiwan 在职认证  发表于 2015-8-30 23:19:04
  1. *修改后,添加第一行

  2. sysuse lifeexp.dta, clear
  3. replace lexp = 35 if country == "Haiti"
  4. egen median = median(lexp), by(region)
  5. egen upq = pctile(lexp), p(75) by(region)
  6. egen loq = pctile(lexp), p(25) by(region)
  7. egen iqr = iqr(lexp), by(region)
  8. egen upper = max(min(lexp, upq + 1.5 * iqr)), by(region)
  9. egen lower = min(max(lexp, loq - 1.5 * iqr)), by(region)
  10. twoway rbar med upq region, ///
  11. pstyle(p1) blc(gs15) bfc(gs8) barw(0.35) || ///
  12. rbar med loq region, pstyle(p1) blc(gs15) bfc(gs8) barw(0.35) || ///
  13. rspike upq upper region, pstyle(p1) || ///
  14. rspike loq lower region, pstyle(p1) || ///
  15. rcap upper upper region, pstyle(p1) msize(*2) || ///
  16. rcap lower lower region, pstyle(p1) msize(*2) || ///
  17. scatter lexp region if !inrange(lexp, lower, upper) & lexp > 50, ///
  18. ms(Oh)  mla(country) mlabcolor(gs8) xscale(off)  legend(off) ///
  19. yla(, ang(h)) ytitle(Life expectancy (years)) xtitle("") ///
  20. name(gr1,replace)

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

  27. graph combine gr1 gr2, col(1) xcommon imargin(zero)
复制代码

Graph11.png



67
niuniuyiwan 在职认证  发表于 2015-8-30 23:20:59
  1. // Combining graphs and including a common legend
  2. //this uses the user written command "grc1leg" which must be downloaded first

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

  4. clear        all

  5. input quest str25 q      a1 a2 a3 a4 a5 a6
  6. 1 "Question 1"           0  2  37 45 12 4
  7. 1 "Benchmark Q1"         2  5  25 47 17 4
  8. 2 "Question 2"           1  37  2 40 17 3
  9. 2 "Benchmark Q2"         2  5  25 47  4 17
  10. 3 "Question 3"           1  2  40 37 17 3
  11. 3 "Benchmark Q3"         2  5  25 47 17 4
  12. 4 "Question 4"           1  2  37  17 3 40
  13. 4 "Benchmark Q4"         2  5  47 25 17 4
  14. end       

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

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

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

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

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

  31. exit

  32. *(Also see the user written program catplot on:
  33. http://www.survey-design.com.au/Usergraphs.html)
复制代码

Graph1.png



68
niuniuyiwan 在职认证  发表于 2015-8-31 08:30:07
  1. // histogram - add x bar labels for the centre of each bar

  2. sysuse auto, clear

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

  4. count if !missing(x)

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

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

  7. }

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

Graph.png



69
niuniuyiwan 在职认证  发表于 2015-8-31 08:31:25
  1. // Changing bar labels using the Stata graph editor

  2. // The type of bar label is limited in Stata graphs but sometimes
  3. // additional information is required. This can be added with the
  4. // Stata graphics editor commands.
  5. // Below the number of observation for each region is added.

  6. sysuse citytemp, clear

  7. #delimit ;
  8. graph bar tempjuly tempjan, over(region) bargap(-30)
  9.   legend( label(1 "July") label(2 "January") )
  10.   ytitle("Degrees Fahrenheit")
  11.   title("Average July and January temperatures")
  12.   subtitle("by regions of the United States")
  13.   note("Source:  U.S. Census Bureau, U.S. Dept. of Commerce")
  14. blabel(bar, position(inside) format(%9.1f) color(white)) ;
  15. #delimit cr

  16. generate No=_n
  17. collapse (sum) N, by(region)
  18. list

  19. local i1=1

  20. forvalues i=1/`=_N' {

  21. gr_edit plotregion1.barlabels[`i1'].text = {}
  22. gr_edit plotregion1.barlabels[`i1'].text.Arrpush N=`=No[`i']'
  23. local ++i1

  24. gr_edit plotregion1.barlabels[`i1'].text = {}
  25. gr_edit plotregion1.barlabels[`i1'].text.Arrpush N=`=No[`i']'
  26. local ++i1

  27. }

  28. exit
复制代码

Graph.png



70
niuniuyiwan 在职认证  发表于 2015-8-31 08:32:40

Graph.png


  1. // Adding bar labels

  2. // The type of bar label is limited in Stata graphs but sometimes
  3. // additional information is required. This can be added with the
  4. // by adding another twoway plot.

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

  7. more

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

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

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




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

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