楼主: 匿名
20361 212

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

21
niuniuyiwan 在职认证  发表于 2015-8-24 13:02:34
  1 sysuse auto, clear
  2
  3 twoway  ///
  4 (scatter weight mpg if rep78==1)            ///
  5 (scatter weight mpg if rep78==2)            ///
  6 (scatter weight mpg if rep78==3)            ///
  7 (scatter weight mpg if rep78==4)            ///
  8 (scatter weight mpg if rep78==5) , name(a1) ///
  9 legend(label(1 "weight, rep78 == 1"))       ///
10 legend(label(2 "weight, rep78==2"))         ///
11 legend(label(3 "weight, rep78==3"))         ///
12 legend(label(4 "weight, rep78==4"))         ///
13 legend(label(5 "weight, rep78==5"))           

a1.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

22
niuniuyiwan 在职认证  发表于 2015-8-25 08:18:33
  1. // Graph - Special Text

  2. twoway                                                                        ///
  3. (scatteri 6.5 0 "Other Symbols Include ",         mlabsize(small) pstyle(p1)) ///
  4. (scatteri 6.5 0.5 "{&trade} {©right} {&loz}", mlabsize(huge) pstyle(p1))  ///
  5. (scatteri 5.5 0 "Bold font",                      mlabsize(small) pstyle(p1)) ///
  6. (scatteri 5.5 0.5 "{bf: Bold font}",              mlabsize(huge) pstyle(p1))  ///
  7. (scatteri 4.5 0 "{it: Italics}",                  mlabsize(small) pstyle(p1)) ///
  8. (scatteri 4.5 0.5 "{it: Italics}",                mlabsize(huge)  pstyle(p1)) ///
  9. (scatteri 3.5 0 "Superscript",                    mlabsize(small) pstyle(p1)) ///
  10. (scatteri 3.5 0.5 "{superscript: Superscript}",   mlabsize(huge) pstyle(p1))  ///
  11. (scatteri 2.5 0 "Subscript",                      mlabsize(small) pstyle(p1)) ///
  12. (scatteri 2.5 0.5 "{subscript: Subscript}",       mlabsize(huge) pstyle(p1))  ///
  13. (scatteri 1.5 0 "Default Symbol font   ",         mlabsize(small) pstyle(p1)) ///
  14. (scatteri 1.5 0.5 " {stSymbol:  adcv}  ",         mlabsize(huge) pstyle(p1))  ///
  15. (scatteri 0.5 0 "Greek Symbols:  ",               mlabsize(small) pstyle(p1)) ///
  16. (scatteri 0.5 0.5 "{&Alpha} {&Pi} {&Sigma}",      mlabsize(huge) pstyle(p1)), ///
  17. title("Special Font that can be used on Stata Graphs", color(red))            ///
  18. xlabel(-.05 1) ylabel(-1 7)                                                   ///
  19. xscale(off) yscale(off)                                                       ///
  20. legend(off)                                                                   ///
  21. note("Stata special font."                                                    ///
  22. "There are many other options see {bf:help graph text}" ,                     ///
  23. ring(0))
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

23
niuniuyiwan 在职认证  发表于 2015-8-25 08:21:02
  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)
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

24
niuniuyiwan 在职认证  发表于 2015-8-25 08:28:53
  1. //Generating Survival curve with lines at specific points in time

  2. use http://www.stata-press.com/data/r12/drug2, clear

  3. sts generate newvar1 =s
  4. sts generate lb = lb(s)
  5. sts generate ub = ub(s)

  6. gen kk=newvar1 if studytime==20
  7. sort kk
  8. local a1=kk[1]

  9. twoway  (rarea ub lb _t, sort  )                ///
  10. (line  newvar1 studytime, sort c(stairstep))    ///
  11. (pci  0 20 `a1' 20) (pci  `a1' 0  `a1' 20),     ///
  12. ylabel(0(.2)1) name(a2, replace) ytitle("")     ///
  13. legend(label(1 data set 1) label(2 data set 2)) ///
  14. scheme(s1color) legend(off) xtitle("Studytime") ///
  15. ylabel( .1(.2)1  ) ymlabel(  `a1', angle(45) )
复制代码

a2.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

25
niuniuyiwan 在职认证  发表于 2015-8-25 08:29:59
  1. //Generating Survival curve with confidence interval

  2. use http://www.stata-press.com/data/r11/drug2.dta, clear
  3. stset

  4. sts gen km=s
  5. sts gen ub=ub(s)
  6. sts gen lb=lb(s)

  7. //Then something along the lines:
  8. twoway (rarea ub lb _t, sort) (line km _t, sort)
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

26
niuniuyiwan 在职认证  发表于 2015-8-25 08:35:20
  1. sysuse auto, clear

  2. // estimate the model
  3. reg price c.mpg##c.weight##c.weight i.foreign

  4. // collect some information about mpg
  5. sum mpg
  6. local m = r(mean)
  7. local sd = r(sd)

  8. // we are going to change the data, so we preserve it first
  9. preserve

  10. // fix the other explanatory variables
  11. // In this case we only look at US ("domestic") cars
  12. replace foreign = 0

  13. // predict the price while fixing mpg at different values
  14. replace mpg = `m'
  15. predict yhatm

  16. replace mpg = `m' - `sd'
  17. predict yhatl

  18. replace mpg = `m' - 2*`sd'
  19. predict yhatll

  20. replace mpg = `m' + `sd'
  21. predict yhath

  22. replace mpg = `m' + 2*`sd'
  23. predict yhathh

  24. // nicer display of large numbers 1,000 instead of 1000
  25. format yhat* %8.0gc

  26. // create graph
  27. sort weight
  28. twoway line yhatll yhatl yhatm yhath yhathh weight, ///
  29.        title("predicted price for US cars")         ///
  30.        ytitle("predicted price (US {c S|})")        ///
  31.        lpattern(solid solid solid solid solid)      ///
  32.        lcolor("255 255 204"                         ///
  33.               "161 218 180"                         ///
  34.               " 65 182 196"                         ///
  35.               " 44 127 184"                         ///   
  36.               " 37  52 148" )                       ///
  37.        legend( order( - "mpg"                       ///
  38.                       1 "mean-2*sd"                 ///
  39.                       2 "mean-1*sd"                 ///
  40.                       3 "mean"                      ///
  41.                       4 "mean+1*sd"                 ///
  42.                       5 "mean+2*sd" ))

  43. // get our original data back
  44. restore
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

27
niuniuyiwan 在职认证  发表于 2015-8-25 08:36:47
  1. sysuse auto, clear

  2. gsort -mpg
  3. keep in 1/25

  4. replace mpg = round(mpg)

  5. gen rank1 = _n
  6. egen rank2 = rank(-price)

  7. gen byte one = 1
  8. gen byte two = 2
  9. gen left = 0.4

  10. twoway                                                  ///
  11. (pcspike rank1 one rank2 two,                           ///
  12. xlabel(none) xscale(noline range(0.3 2.3)) xtitle("")   ///
  13. yscale(r(-1 .) reverse off) ylabel(, nogrid))           ///
  14. (scatter rank1 one, mlabel(mpg) mlabpos(9) ms(none))    ///
  15. (scatter rank2 two, mlabel(price) mlabpos(3) ms(none))  ///
  16. ( scatter rank1 left, mla(make) mlabpos(3) ms(none)     ///
  17. text(-0.5 1 "mpg, US gallons")                          ///
  18. text(-0.5 2 "price , {c S|}US in 1978")                 ///
  19. legend(off) graphregion(color(white)))
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

28
niuniuyiwan 在职认证  发表于 2015-8-25 08:41:17
  1. //Parallel Coordinate Plot
  2. //Rescaling variables and plotting

  3. sysuse auto, clear
  4. drop make
  5. set more off

  6. local z=1
  7. foreach i of varlist _all{

  8. summarize `i', detail

  9. #delimit ;
  10.   local a `"`a' text( 1.2 `z' `=char(34)'`i'`=char(34)'
  11.   `=char(34)'`=string(`r(max)',"%8.2f")'`=char(34)' ,  
  12.   orientation(vertical) placement(n) size(3) justification(left) )
  13.   text( -0.1 `z' `=char(34)' `i'`=char(34)'
  14.   `=char(34)'`=string(`r(min)',"%8.2f")'`=char(34)' ,
  15.   orientation(vertical) placement(south) size(3) justification(right) ) "';
  16. #delimit cr

  17. //scaling variables  
  18. generate x`z'=(`i' -r(min))/(r(max)-r(min))
  19. local ++z
  20. }
  21. drop if rep78==.
  22. generate id=_n
  23. reshape long x, i(id) j(type)

  24. drop if x>100

  25. twoway                                       ///
  26. (line  x type if for==1, lcolor(red) c(L))   ///
  27. (line  x type if for==0, lcolor(blue) c(L)), ///
  28. xline(1/12)                                  ///
  29. `a' yscale( off range(0 1.1)) xscale(off)    ///
  30. text(0 0 "MIN")  text(1 0 "Max")             ///
  31. graphregion(margin(t=20 b=20)) legend(off)
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

29
niuniuyiwan 在职认证  发表于 2015-8-25 14:48:28
  1. // Area Plot

  2. // prepare the example data

  3. sysuse nlsw88, clear

  4. gen ind_gr = industry
  5. recode ind_gr 1/5=1 6=2 7=3 8/10=4 11=5 12=6
  6. label define ind_gr 1 "manual"                ///
  7.                     2 "trade"                 ///
  8.                     3 "finance"               ///
  9.                     4 "other services"        ///
  10.                     5 "professional services" ///
  11.                     6 "public administration"       
  12. label value ind_gr ind_gr
  13.                                                
  14. // compute percentile ranks
  15. egen n = count(wage)
  16. egen i = rank(wage)
  17. gen hazen = (i - 0.5) / n * 100       
  18. label variable hazen "percentile rank of income"
  19.                                                
  20. // smooth the proportions
  21. mkspline s_w=hazen, cubic nknots(5)
  22. mlogit ind_gr s_w*
  23. predict pr*       
  24.                                        
  25. // create the graph
  26. gen zero = 0
  27. gen one = 100
  28. gen l1 = (pr1)*100       
  29. gen l2 = (pr1 + pr2)*100       
  30. gen l3 = (pr1 + pr2 + pr3)*100
  31. gen l4 = (pr1 + pr2 + pr3 + pr4)*100
  32. gen l5 = (pr1 + pr2 + pr3 + pr4 + pr5)*100
  33.                        
  34. sort hazen       
  35.                                                
  36. // collect the labels for the second y-axis
  37. local mid = l1[_N]/2
  38. local yaxis `"`mid' "manual""'
  39.                                                
  40. local mid = (l2[_N]-l1[_N])/2 + l1[_N]
  41. local yaxis `"`yaxis' `mid' "trade""'
  42.                                                
  43. local mid = (l3[_N]-l2[_N])/2 + l2[_N]
  44. local yaxis `"`yaxis' `mid' "finance""'
  45.                                                
  46. local mid = (l4[_N]-l3[_N])/2 + l3[_N]
  47. local yaxis `"`yaxis' `mid' "other services""'
  48.                                                
  49. local mid = (l5[_N]-l4[_N])/2 + l4[_N]
  50. local yaxis `"`yaxis' `mid' "professional services""'       
  51.                                                
  52. local mid = (100-l5[_N])/2 + l5[_N]
  53. local yaxis `"`yaxis' `mid' "public administration""'
  54.                                                
  55. twoway rarea zero l1 hazen, yaxis(1) ||  ///
  56.        rarea l1 l2 hazen, yaxis(2)   ||  ///
  57.        rarea l2 l3 hazen   ||            ///       
  58.        rarea l3 l4 hazen   ||            ///
  59.        rarea l4 l5 hazen   ||            ///
  60.        rarea l5 one hazen,               ///
  61.        ytitle("percentage")              ///
  62.        ylab(`yaxis', axis(2) angle(-45)) ///
  63.        yscale(range(0 100) axis(1))      ///       
  64.        yscale(range(0 100) axis(2))      ///
  65.        ytitle("", axis(2))               ///
  66.        plotregion(margin(zero))          ///
  67.        aspect(1)                         ///       
  68.        legend(off)
复制代码

Graph.png


已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

30
niuniuyiwan 在职认证  发表于 2015-8-26 15:26:38
  1. // Including totals in bar graph

  2. sysuse auto, clear
  3. save auto1 , replace  //保存为auto1.dta

  4. replace for=3   //foreign 替换为3
  5. label define  origin 3 Total, add

  6. append using auto1

  7. graph bar (mean) price , over(rep78, ) over(for) showyvars ///
  8. ascategory asyvar legend(off)

  9. exit
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 信用等级 收起 理由
xddlovejiao1314 + 50 + 20 + 1 + 1 精彩帖子

总评分: 经验 + 50  论坛币 + 20  学术水平 + 1  信用等级 + 1   查看全部评分

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

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