楼主: 匿名
20354 212

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

51
niuniuyiwan 在职认证  发表于 2015-8-30 10:17:18
  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



52
niuniuyiwan 在职认证  发表于 2015-8-30 10:19:29
  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



53
niuniuyiwan 在职认证  发表于 2015-8-30 10:20:38
  1. // Different color bars for the over option
  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 w 1 "Women" 0 "Men"
  17. label value w w

  18. gen g1=mark if _n<6
  19. gen g2=mark if _n>5

  20. list

  21. graph bar (asis) g1 g2, ascat over(mark,sort(mark) descending ///
  22. relabel(  ///
  23. 5  "Barbora - CZE "    ///
  24. 4  "Christina - GER "  ///
  25. 3  "Linda - GER "      ///
  26. 2  "Sunette - RSA"     ///
  27. 1  "Huihui - CHN"      ///
  28. 10 "Keshorn - TRI "    ///
  29. 9  "Oleksandr - UKR "  ///
  30. 8  "Antti - FIN "      ///
  31. 7  "Vitezslav - CZE"   ///
  32. 6  "Tero - FIN"        ///
  33. ) label( angle(45)))   over(w) ylabel(0(10)90)                                 ///
  34. ytitle("Metres") blabel(bar, position(inside) format(%9.1f) color(white))      ///
  35. scheme(s2color)  legend(off) bargap(5) title("London Olympics 2012" "Javelin") ///
  36. nofill  bar(1, color(dknavy)) bar(2, color(red))

  37. exit
复制代码

Graph.png



54
niuniuyiwan 在职认证  发表于 2015-8-30 18:08:13
  1. // Adding average bar to group

  2. sysuse auto, clear

  3. summ price if for==0 & !missing(rep78)
  4. local ave1=r(mean)

  5. summ price if for==1 & !missing(rep78)
  6. local ave2=r(mean)

  7. set obs `=_N+3'

  8. replace for=0 in -3
  9. replace rep78=6 in -3

  10. replace for=0 in -2
  11. replace rep78=7 in -2
  12. replace price=`ave1' in -2

  13. replace for=1 in l
  14. replace rep78=7 in l
  15. replace price=`ave2' in l

  16. label define kk 1 1 2 2 3 3 4 4 5 5 6 "-"  7 "AVE"
  17. label value rep78 kk

  18. graph bar (mean) price , over(rep78 ) over(for) showyvars ///
  19.   asyvar legend(off)  scheme(color) bar(7, color(black))
复制代码

Graph.png



55
niuniuyiwan 在职认证  发表于 2015-8-30 18:09:43
  1. // Detail and Summary box plots together

  2. sysuse auto, clear
  3. generate order = _n
  4. expand 3
  5. bysort order : generate which = _n
  6. drop if which == 1 & price > 5000
  7. drop if which == 2 & price > 10000
  8. label def which 1 "<= $5000" 2 "<= $10000" 3 "all"
  9. label val which which
  10. graph box mpg, over(which) over(foreign)

  11. *(Nick Cox - Stata list,  December 06, 2013)
复制代码

Graph.png



56
niuniuyiwan 在职认证  发表于 2015-8-30 18:11:44
  1. sysuse nlsw88, clear

  2. clonevar wagelog10=wage
  3. replace wagelog10=log10(wagelog10)

  4. mylabels 0(10)40 , myscale(log10(@)) local(labels)

  5. graph hbox wagelog10, over(ind, sort(1)) nooutside ytitle("") ylabel(`labels') ///
  6. title("Hourly wage, 1988, woman aged 34-46", span) subtitle(" ") ///
  7. note("Source:1988 data from NLS, U.S. Dept. of Labor, "          ///
  8. "Bureau of Labor Statistics",span)
  9. exit
复制代码

Graph.png



57
niuniuyiwan 在职认证  发表于 2015-8-30 18:14:10
  1. // Labelling the over variable

  2. sysuse auto, clear

  3. levelsof for, local(a)
  4. levelsof rep78, local(a1)

  5. tempfile file

  6. foreach i of local a {     //for
  7. foreach i1 of local a1 {   //rep78

  8. sysuse auto, clear

  9. count if for==`i' & rep78==`i1'  //making sure there are obs to regress

  10. if r(N)>=1 {
  11.    regress mpg weight if for==`i' & rep78==`i1'
  12.    parmest, norestore
  13.    egen axis= axis(estimate), label(parm)
  14.    gen for=`i'
  15.    gen rep78=`i1'
  16.    capture append using `file'
  17.    save `file', replace
  18.    local kk=`i1'+1
  19. }
  20. }
  21. }  //end loop

  22. summarize rep78
  23. foreach i of local a {  //for
  24.   set obs `=_N+1'
  25.   replace for=`i' if for==.
  26.   local max1=`r(max)'
  27.   replace rep78=`max1'  +1 if rep78==.
  28. }

  29. gsort for -rep78

  30. drop if parm=="_cons"
  31. gen order=_N-_n

  32. label define jj 9 "foreign=0       "  ///
  33.                 3 "foreign=1       "  ///
  34.                                 0 "rep78_3"        ///
  35.                                 1 "rep78_4"        ///
  36.                                 2 "rep78_5"        ///
  37.                                 4 "rep78_1"        ///
  38.                                 5 "rep78_2"        ///
  39.                 6 "rep78_3"        ///
  40.                                 7 "rep78_4"        ///
  41.                                 8 "rep78_5"

  42. label value order  jj

  43. twoway (scatter order est , msymbol(none))  ///
  44. (scatter order est )                        ///
  45. (rcap min95 max95 order, horizontal ),      ///
  46. ylabel(0/9, valuelabel angle(0)) legend(off) ytitle("rep78 over foreign") ///
  47. xtitle("Coefficients and confidence interval")

  48. exit
复制代码

Graph.png



58
niuniuyiwan 在职认证  发表于 2015-8-30 18:32:29
  1. // Including a distribution on top of a histogram.

  2. //The normal option for graph histogram uses the dataset's mean
  3. //and standard deviation.
  4. //This method allows you to specify these.  

  5. sysuse auto, clear
  6. histogram mpg , addplot(function normalden(x,26,10), ra(10 42))

  7. *(Nick Cox - Stata list,  Fri, 03 June 2011)
复制代码

2.png


59
niuniuyiwan 在职认证  发表于 2015-8-30 20:09:07
  1. // Double graph - if there is too much detail for one
  2. // graph then 2 graphs can be used

  3. clear
  4. set more off
  5. use "http://www.stata-press.com/data/r11/nlswork2.dta"
  6. describe

  7. replace year=1900+year

  8. gen age11= yofd(date(c(current_date)),"DMY") -(1900+birth_yr)

  9. recode age11 (56/58=1) (59/61=2) (62/64=3) (65/67=4) (68/70=5)

  10. collapse (count) union  (count) idcode ,by(year age11)
  11. gen per=(union/idcode)*100

  12. twoway                                                                   ///
  13. (line  per year   if age11==1 , lpattern(dash)  connect(l))              ///
  14. (line  per year   if age11==2 , lpattern(dash_dot)  connect(l))          ///
  15. (line   per year   if age11==3 , lpattern(dot) connect(l)) ,             ///
  16. name(a1,replace) legend(  bmargin(l=40)     stack    region(style(none)) ///
  17. label( 1 "56-58")                                                        ///
  18. label( 2 "59-61")                                                        ///
  19. label( 3 "62-64")                                                        ///
  20. rows(1)   ) xtitle("")                                                   ///
  21. xlabel(1968 "            1968"  1978 "1978             ", noticks)

  22. twoway                                                                      ///
  23. (line  per year   if age11==4 ,  yaxis(1)  lpattern(solid)  connect(l))     ///
  24. (line  per year   if age11==5 ,  yaxis(1)  lpattern(longdash)  connect(l)), ///
  25. name(a2,replace) legend(  bmargin(r=40)  stack    region(style(none))       ///
  26. label(1 "65-67")                                                            ///
  27. label( 2 "68-70")                                                           ///
  28. rows(1)   ) xtitle("")                                                      ///
  29. xlabel(1968 "            1968"  1978 "1978             ", noticks) ///
  30. yscale(alt) xline(1968)

  31. graph combine a1 a2 , imargins(zero) ycommon
复制代码

Graph2.png



60
niuniuyiwan 在职认证  发表于 2015-8-30 20:39:29
  1. sysuse auto, clear
  2. su mpg
  3. local width=1
  4. histogram mpg ,width(`width') freq ///
  5. addplot(function y =`width'*`=_N'*normalden(x,`r(mean)',`r(sd)'), ra(10 45))


  6. sysuse auto, clear
  7. su mpg
  8. local width=2   //   <-----------根据需要改变
  9. histogram mpg ,width(`width') freq ///
  10. addplot(function y =`width'*`=_N'*normalden(x,`r(mean)',`r(sd)'), ra(10 45))
复制代码

width=1.png


width=2.png

width=3.png

width=2.png


注:原do-file修改后





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

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