楼主: 匿名
20353 212

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

91
niuniuyiwan 在职认证  发表于 2015-8-31 12:18:53
  1. //在图像中添加背景

  2. //转化地址:  http://www.text-image.com/convert/ascii.html
  3. //set width to 500  转化时特别注意的地方
  4. // 下载后保存到当前工作路径(输入cd/pwd),本例命名为car ascii to text big.txt


  5. set more off
  6. infix str1 a1-a500 1-500 using "car ascii to text big.txt",  clear

  7. //hexdump may be needed to look for unexpected characters
  8. //hexdump "D:\StataMP\ado\personal\data\car ascii to text big.txt", tabulate

  9. //scaling y axis picture data and reversing
  10. generate y=90+((_N-_n+1)*1)

  11. reshape long a, i(y) j(col)

  12. //replacing characters with numbers
  13. replace a="1" if a=="+"
  14. replace a="2" if a=="-"
  15. replace a="3" if a=="."
  16. replace a="4" if a=="/"
  17. replace a="5" if a==":"
  18. replace a="6" if a=="-"
  19. replace a="7" if a=="M"
  20. replace a="8" if a=="N"
  21. replace a="9" if a=="`"
  22. replace a="10" if a=="d"
  23. replace a="11" if a=="h"
  24. replace a="12" if a=="m"
  25. replace a="13" if a=="o"
  26. replace a="14" if a=="s"
  27. replace a="15" if a=="y"

  28. //scaling x axis picture data
  29. replace col=col/10

  30. merge 1:1 _n using  "D:\StataMP\ado\base/a/auto.dta"  //<--------根据需要改变

  31. local size="tiny"

  32. twoway ///
  33. (scatter y col if a=="1" ,msymbol(circle) ///
  34. mcolor("0 0 0*.20") msize(`size') )       ///
  35. (scatter y col if a=="2" ,msymbol(circle) ///
  36. mcolor("255 255 255*.2") msize(`size'))   ///
  37. (scatter y col if a=="3" ,msymbol(circle) ///
  38. mcolor("255 0 0*.2") msize(`size') )      ///
  39. (scatter y col if a=="4" ,msymbol(circle) ///
  40. mcolor("0 0 255*.2") msize(`size') )      ///
  41. (scatter y col if a=="5" ,msymbol(circle) ///
  42. mcolor("0 0 255*.2") msize(`size') )      ///
  43. (scatter y col if a=="6" ,msymbol(circle) ///
  44. mcolor("255 255 0*.2") msize(`size') )    ///
  45. (scatter y col if a=="7" ,msymbol(circle) ///
  46. mcolor("0 255 255*.2") msize(`size') )    ///
  47. (scatter y col if a=="8" ,msymbol(circle) ///
  48. mcolor("255 0 255*.2") msize(`size') )    ///
  49. (scatter y col if a=="9" ,msymbol(circle) ///
  50. mcolor(white) msize(`size') )             ///
  51. (scatter y col if a=="10",msymbol(circle) ///
  52. mcolor(orange*.2) msize(`size') )         ///
  53. (scatter y col if a=="11",msymbol(circle) ///
  54. mcolor(brown*.2) msize(`size') )          ///
  55. (scatter y col if a=="12",msymbol(circle) ///
  56. mcolor(sand*.2) msize(`size') )           ///
  57. (scatter y col if a=="13",msymbol(circle) ///
  58. mcolor(black*.2) msize(`size') )          ///
  59. (scatter y col if a=="14",msymbol(circle) ///
  60. mcolor(black*.2) msize(`size') )          ///
  61. (scatter y col if a=="15",msymbol(circle) ///
  62. mcolor(black*.2) msize(`size') )          ///
  63. (scatter  length mpg)                     ///
  64. (lfit  length mpg)                        ///
  65. ,yscale( range(100 250))                  ///
  66. ytitle(Length ins.) xtitle(mpg)           ///
  67. title(Mpg v Length) legend(off)

  68. exit
复制代码

捕获.PNG


car.jpg



Graph.png


92
niuniuyiwan 在职认证  发表于 2015-8-31 16:59:19
  1. //透明图像叠加
  2. //使用IrfanView - free image software
  3. //http://www.irfanview.com/  <---------百度:irfanview最新官方版下载_百度软件中心


  4. sysuse auto
  5. histogram mpg if for==0, percent color(red) ///
  6. xscale(range(10(5)45)) barwidth(3) yscale(range(0(10)60))  xtitle("") ytitle("")

  7. graph export "a1.png", height(376)  width(506) replace
  8. //此处做了修改,存放在当前工作路径


  9. histogram mpg if for==1, percent color(blue) ///
  10. xscale(range(10(5)45)) barwidth(3)  yscale(range(0(10)60)) xtitle("") ytitle("")

  11. graph export "a2.png", height(376) width(506) replace
  12. //此处做了修改,存放在当前工作路径

  13. *输入cdout (ssc install cdout) 找到刚才两幅图像
  14. //然后打开 Irfanview
  15. //菜单栏Edit-->Insert overlay/watermark image----->select graph  eg. a2.png
  16. //在"overlay transparency" 设定为80% 并且Start Comer选择center
复制代码

捕获.PNG



3.jpg


93
niuniuyiwan 在职认证  发表于 2015-8-31 17:06:03
  1. //tsline with confidence intervals - using margins

  2. /*Requires: parmest
  3. to download this program type the following on the Stata command
  4. line (if not already loaded):*/
  5. ssc install parmest

  6. /*Requires: tsmktim
  7. to download this program type the following on the Stata command
  8. line (if not already loaded):*/
  9. ssc install tsmktim

  10. /*To run the example: copy the following into a do file and run*/

  11. webuse grunfeld, clear
  12. qui reg invest i.year i.company
  13. margins i.year, post
  14. // Roger Newson's parmest from SSC
  15. parmest, saving(yeareff, replace)
  16. use yeareff, clear
  17. // Kit Baum's tsmktim from SSC
  18. tsmktim year, start(1935)
  19. tsline estimate || tsrline min95 max95, legend(col(1)) scheme(s2mono)
复制代码

Graph.png



94
niuniuyiwan 在职认证  发表于 2015-8-31 21:19:07
  1. //Labelling in terms of another variable Requires: labmask
  2. //修改后--------->添加Y轴刻度

  3. ssc install labutil --------->根据需要安装或者findit labmask

  4. sysuse auto, clear
  5. sort price
  6. gen order = _n
  7. labmask order, values(make)


  8. twoway bar price order, horizontal  ///
  9. ylabel(8 "Renault Le Car"  44 "VW Diesel" 71 "Linc.Versailles", angle(h)) ytitle("")
复制代码

Graph.png



95
niuniuyiwan 在职认证  发表于 2015-9-1 09:02:50
  1. sysuse gnp96, clear
  2. #delimit ;

  3. twoway area d.gnp96 date, xlabel(36(8)164, angle(90))
  4.   ylabel(-100(50)200, angle(0))
  5.   ytitle("Billions of 1996 Dollars")
  6.   xtitle("")
  7.   subtitle("Change in U.S. GNP", position(11))
  8.   note("Source: U.S. Department of Commerce, Bureau of Economic Analysis")
  9.   ;
  10. #delimit cr
复制代码

Graph.png



96
niuniuyiwan 在职认证  发表于 2015-9-1 09:04:38
  1. sysuse lifeexp, clear
  2. generate lgnppc = ln(gnppc)
  3. label var lgnppc "ln GNP per capita"
  4. #delimit ;
  5. graph matrix popgr lgnp safe lexp,
  6.   maxes(ylab(#4, grid) xlab(#4, grid))
  7.   subtitle("Summary of 1998 life-expectancy data")
  8.   note("Source:  The World Bank Group")
  9.   ;
  10. #delimit cr
复制代码

Graph.png



97
niuniuyiwan 在职认证  发表于 2015-9-1 09:07:29
  1. sysuse census, clear
  2. gen drate = divorce / pop18p
  3. label var drate "Divorce rate"
  4. scatter drate medage [w=pop18p] if state!="Nevada", msymbol(Oh) ///
  5.   note("State data excluding Nevada" ///
  6.        "Area of symbol proportional to state's population aged 18+")
复制代码

Graph.png



98
niuniuyiwan 在职认证  发表于 2015-9-1 09:58:49
  1. sysuse sp500, clear
  2. #delimit ;
  3. twoway
  4. line close date, yaxis(1)
  5. ||
  6. bar change date, yaxis(2)
  7. ||
  8. in 1/52,
  9. ysca(axis(1) r(1000 1400)) ylab(1200(50)1400, axis(1))
  10. ysca(axis(2) r(-50 300)) ylab(-50 0 50, axis(2))
  11. ytick(-50(25)50, axis(2) grid)
  12. legend(off)
  13. title("S&P 500")
  14. subtitle("January - March 2001")
  15. note("Source:  Yahoo!Finance and Commodity Systems, Inc.")
  16. yline(1150, axis(1) lstyle(foreground))
  17. ;
  18. #delimit cr
复制代码
Graph.png

99
niuniuyiwan 在职认证  发表于 2015-9-1 10:23:03
  1. //在图像中添加背景

  2. //转化地址:  http://www.text-image.com/convert/ascii.html
  3. //set width to 500  转化时特别注意的地方
  4. // 下载后保存到当前工作路径(输入cd/pwd),本例命名为car ascii to text big.txt


  5. set more off
  6. infix str1 a1-a500 1-500 using "car ascii to text big1.txt",  clear

  7. //hexdump may be needed to look for unexpected characters
  8. //hexdump "D:\StataMP\ado\personal\data\car ascii to text big.txt", tabulate

  9. //scaling y axis picture data and reversing
  10. generate y=90+((_N-_n+1)*1)

  11. reshape long a, i(y) j(col)

  12. //replacing characters with numbers
  13. replace a="1" if a=="+"
  14. replace a="2" if a=="-"
  15. replace a="3" if a=="."
  16. replace a="4" if a=="/"
  17. replace a="5" if a==":"
  18. replace a="6" if a=="-"
  19. replace a="7" if a=="M"
  20. replace a="8" if a=="N"
  21. replace a="9" if a=="`"
  22. replace a="10" if a=="d"
  23. replace a="11" if a=="h"
  24. replace a="12" if a=="m"
  25. replace a="13" if a=="o"
  26. replace a="14" if a=="s"
  27. replace a="15" if a=="y"

  28. //scaling x axis picture data
  29. replace col=col/10

  30. merge 1:1 _n using  "D:\StataMP\ado\base/a/auto.dta"  //<--------根据需要改变

  31. local size="tiny"

  32. twoway ///
  33. (scatter y col if a=="1" ,msymbol(circle) ///
  34. mcolor("0 0 0*.20") msize(`size') )       ///
  35. (scatter y col if a=="2" ,msymbol(circle) ///
  36. mcolor("255 255 255*.2") msize(`size'))   ///
  37. (scatter y col if a=="3" ,msymbol(circle) ///
  38. mcolor("255 0 0*.2") msize(`size') )      ///
  39. (scatter y col if a=="4" ,msymbol(circle) ///
  40. mcolor("0 0 255*.2") msize(`size') )      ///
  41. (scatter y col if a=="5" ,msymbol(circle) ///
  42. mcolor("0 0 255*.2") msize(`size') )      ///
  43. (scatter y col if a=="6" ,msymbol(circle) ///
  44. mcolor("255 255 0*.2") msize(`size') )    ///
  45. (scatter y col if a=="7" ,msymbol(circle) ///
  46. mcolor("0 255 255*.2") msize(`size') )    ///
  47. (scatter y col if a=="8" ,msymbol(circle) ///
  48. mcolor("255 0 255*.2") msize(`size') )    ///
  49. (scatter y col if a=="9" ,msymbol(circle) ///
  50. mcolor(white) msize(`size') )             ///
  51. (scatter y col if a=="10",msymbol(circle) ///
  52. mcolor(orange*.2) msize(`size') )         ///
  53. (scatter y col if a=="11",msymbol(circle) ///
  54. mcolor(brown*.2) msize(`size') )          ///
  55. (scatter y col if a=="12",msymbol(circle) ///
  56. mcolor(sand*.2) msize(`size') )           ///
  57. (scatter y col if a=="13",msymbol(circle) ///
  58. mcolor(black*.2) msize(`size') )          ///
  59. (scatter y col if a=="14",msymbol(circle) ///
  60. mcolor(black*.2) msize(`size') )          ///
  61. (scatter y col if a=="15",msymbol(circle) ///
  62. mcolor(black*.2) msize(`size') )          ///
  63. (scatter  length mpg)                     ///
  64. (lfit  length mpg)                        ///
  65. ,yscale( range(100 250))                  ///
  66. ytitle(Length ins.) xtitle(mpg)           ///
  67. title(Mpg v Length) legend(off)

  68. exit
复制代码

2008-volkswagen-golf-r36-6w.jpg



Graph.png



100
niuniuyiwan 在职认证  发表于 2015-9-1 12:36:45
  1. // Adding a picture as background to a graph

  2. //下载GIMP(与PS类似)

  3. //修改1:删除set momeory 900m
  4. //修改2:将图片宽度修改为200 (压低像素,仅作事例,待改进),保存为fastcar.html
  5. //修改3:路径及数据修改

  6. clear all
  7. set more off

  8. program hex

  9.   syntax varname , Newv(name)

  10.     generate `newv'=((strpos("`=c(alpha)'",`varlist')-1)/2)+10
  11.     replace `newv'=real(`varlist') if `newv'==9.5

  12. end

  13. //values that can be changed
  14. local divide=8     //sample size

  15. insheet using "d:\StataMP\ado\personal\data\fastcar.html", clear  //------>根据需要改变

  16. generate row=.
  17. replace row=1 if strpos(v1,"<TR>")  
  18. replace row=sum(row)

  19. keep if strpos(v1, "<TD  BGCOLOR")   //------>此处进行了修改


  20. split v1,p(#)
  21. split v12,p(>)
  22. keep row v121

  23. generate obs=_n

  24. bysort row (obs):gen col=_n

  25. rename v121 hex
  26. generate hex1=substr(hex,1,1)
  27. generate hex1a=substr(hex,2,1)

  28. generate hex2=substr(hex,3,1)
  29. generate hex2a=substr(hex,4,1)

  30. generate hex3=substr(hex,5,1)
  31. generate hex3a=substr(hex,6,1)

  32. hex hex1, newv(dec1)
  33. hex hex1a, newv(dec1a)

  34. hex hex2, newv(dec2)
  35. hex hex2a, newv(dec2a)

  36. hex hex3, newv(dec3)
  37. hex hex3a, newv(dec3a)

  38. generate dec1b=16*dec1+dec1a
  39. generate dec2b=16*dec2+dec2a
  40. generate dec3b=16*dec3+dec3a

  41. keep dec1b dec2b dec3b row col

  42. generate dec1c=floor(dec1b/`divide')*`divide'
  43. generate dec2c=floor(dec2b/`divide')*`divide'
  44. generate dec3c=floor(dec3b/`divide')*`divide'

  45. gen str20 color=char(34)+string(dec1c,"%3.0f")+" "+string(dec2c,"%3.0f") ///
  46. +" "+string(dec3c,"%3.0f")+char(34)

  47. egen group_col=group(color)

  48. compress
  49. save pict_graph, replace

  50. use pict_graph, clear
  51. quiet summarize col

  52. local size="*`=33/`r(max)''"     //marker size, can be adjusted

  53. levelsof color, local(a)

  54. merge 1:1 _n using  "d:\StataMP\ado\base\a\auto.dta"  //-------->根据需要改变
  55. summarize length

  56. gen y=90+(((_N-_n+1)-90))/(_N/r(max))

  57. //scaling x axis picture data
  58. replace col=col/9

  59. local z=1

  60. foreach i of local a {

  61.   if `z'==1 {
  62. local zz =  ///
  63. `"(scatter y col if group_col==`z',msymbol(square) mcolor(`i') msize(`size'))"'

  64.     local z=2
  65.    }
  66.       else {
  67. local zz1 = ///
  68. `"(scatter y col if group_col==`z' ,msymbol(square) mcolor(`i') msize(`size'))"'

  69. local zz "`zz' `zz1'"
  70. local ++z
  71.       }
  72. }

  73. twoway `zz'                                      ///
  74. (scatter  length mpg, mcolor(red) msize(large))  ///
  75. (lfit  length mpg)                               ///
  76. ,yscale( range(100 325))                         ///
  77. ytitle(Length ins.) xtitle(mpg)                  ///
  78. title(Mpg v Length) legend(off)
复制代码

待完善

宽度为200.png

第二次修改:

  1. clear all
  2. set more off

  3. program hex

  4.   syntax varname , Newv(name)

  5.     generate `newv'=((strpos("`=c(alpha)'",`varlist')-1)/2)+10
  6.     replace `newv'=real(`varlist') if `newv'==9.5

  7. end

  8. //values that can be changed
  9. local divide=30     //sample size

  10. insheet using "d:\StataMP\ado\personal\data\fastcar.html", clear  //------>根据需要改变

  11. generate row=.
  12. replace row=1 if strpos(v1,"<TR>")  
  13. replace row=sum(row)

  14. keep if strpos(v1, "<TD  BGCOLOR")   //------>此处进行了修改


  15. split v1,p(#)
  16. split v12,p(>)
  17. keep row v121

  18. generate obs=_n

  19. bysort row (obs):gen col=_n

  20. rename v121 hex
  21. generate hex1=substr(hex,1,1)
  22. generate hex1a=substr(hex,2,1)

  23. generate hex2=substr(hex,3,1)
  24. generate hex2a=substr(hex,4,1)

  25. generate hex3=substr(hex,5,1)
  26. generate hex3a=substr(hex,6,1)

  27. hex hex1, newv(dec1)
  28. hex hex1a, newv(dec1a)

  29. hex hex2, newv(dec2)
  30. hex hex2a, newv(dec2a)

  31. hex hex3, newv(dec3)
  32. hex hex3a, newv(dec3a)

  33. generate dec1b=16*dec1+dec1a
  34. generate dec2b=16*dec2+dec2a
  35. generate dec3b=16*dec3+dec3a

  36. keep dec1b dec2b dec3b row col

  37. generate dec1c=floor(dec1b/`divide')*`divide'
  38. generate dec2c=floor(dec2b/`divide')*`divide'
  39. generate dec3c=floor(dec3b/`divide')*`divide'

  40. gen str20 color=char(34)+string(dec1c,"%3.0f")+" "+string(dec2c,"%3.0f") ///
  41. +" "+string(dec3c,"%3.0f")+char(34)

  42. egen group_col=group(color)

  43. compress
  44. save pict_graph, replace

  45. use pict_graph, clear
  46. quiet summarize col

  47. local size="*`=33/`r(max)''"     //marker size, can be adjusted

  48. levelsof color, local(a)

  49. merge 1:1 _n using  "d:\StataMP\ado\base\a\auto.dta"  //-------->根据需要改变
  50. summarize length

  51. gen y=90+(((_N-_n+1)-90))/(_N/r(max))

  52. //scaling x axis picture data
  53. replace col=col/9

  54. local z=1

  55. foreach i of local a {

  56.   if `z'==1 {
  57. local zz =  ///
  58. `"(scatter y col if group_col==`z',msymbol(square) mcolor(`i') msize(`size'))"'

  59.     local z=2
  60.    }
  61.       else {
  62. local zz1 = ///
  63. `"(scatter y col if group_col==`z' ,msymbol(square) mcolor(`i') msize(`size'))"'

  64. local zz "`zz' `zz1'"
  65. local ++z
  66.       }
  67. }

  68. twoway `zz'                                      ///
  69. (scatter  length mpg, mcolor(red) msize(large))  ///
  70. (lfit  length mpg)                               ///
  71. ,yscale( range(100 325))                         ///
  72. ytitle(Length ins.) xtitle(mpg)                  ///
  73. title(Mpg v Length) legend(off)
复制代码

Graph.png


重要之处在于local divided,而不是图片输出设置

1.PNG






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

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