楼主: 匿名
20348 212

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

71
niuniuyiwan 在职认证  发表于 2015-8-31 08:38:53
  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



72
niuniuyiwan 在职认证  发表于 2015-8-31 08:39:56
  1. // Stacked bar graph - percentage

  2. sysuse auto, clear

  3. gen a=_n
  4. collapse a, by(for rep78)
  5. drop if missing(for, rep78)

  6. bysort for rep78: gen sum = sum(a)
  7. replace a=0 if a==.

  8. levelsof rep78, local(aa)

  9. generate csum=.

  10. foreach i of local aa {
  11.   summ a if `i'==rep78
  12. replace csum=a/`r(sum)' if `i'==rep78
  13. }

  14. graph bar (sum)csum ,  over(for) over(rep78)        ///
  15. asyvars stack legend(size(vsmall)) per  blabel(bar, ///
  16. position(center) format(%3.1f))


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

Graph.png



73
niuniuyiwan 在职认证  发表于 2015-8-31 08:42:09
  1. // Editing a graph with the gr_edit command

  2. //The graph with the y axis line

  3. sysuse auto, clear
  4. graph hbar   weight price, yscale(off)

  5. //The graph with the y axis line removed

  6. sysuse auto, clear
  7. graph hbar   weight price, ysc(off)
  8. gr_edit .varaxis.style.editstyle linestyle(color(none)) editcopy

  9. //The above can also be produced with:

  10. graph hbar weight price ,showyvar  yvaroptions(axis(lc(none)))

  11. From the Statalist:
  12. Kieran McCaul
  13. 11/6/2010

  14. David Airey
  15. 11/6/2010
复制代码

Graph.png



74
niuniuyiwan 在职认证  发表于 2015-8-31 08:43:43
  1. // Breaking up text automatically in a long note

  2. webuse auto, clear
  3. set more off

  4. //input the text to place in the graph - can be on one line if you wish

  5. local z1 "The development of Linux is one of the most prominent"
  6. local z2 " examples of free  and  open source software"
  7. local z3 "local z3 collaboration; typically all the underlying source code"
  8. local z4 "can beused, freely  modified, and redistributed, both commercially"
  9. local z5 "and  non-commercially, by anyone under licenses such as the GNU"
  10. local z6 "General Public License."
  11. local z7 "Typically Linux is packaged in a format known as a Linux"
  12. local z8 "distribution for desktop and server use."

  13. local total "`z1' `z2' `z3' `z4' `z5' `z6' `z7' `z8'"

  14. local size=40   //adjust for length of line

  15. local t1

  16. forvalues i=1/12 {                //increase the 12 if more lines required
  17. local a`i' : piece `i' `size' of "`total'"   //extended macro function

  18. local t1  `"`t1' `=char(34)+ "`a`i''"+char(34)' "'
  19. }

  20. graph bar price,bargap(-30) over(foreign)         ///
  21. note( `t1' ,   margin(large) justification(left))

  22. exit
复制代码

Graph.png



75
niuniuyiwan 在职认证  发表于 2015-8-31 08:44:48
  1. // Breaking up long labels

  2. sysuse auto, clear

  3. #delimit
  4. label define origin 0 `"group0 group0 group0 group0 group0
  5. group0 group0 group0 group0 group0"', modify;

  6. label define origin 1 "group1 group1 group1 group1 group1
  7. group1 group1 group1 group1 group1 ", modify;
  8. #delimit cr


  9. local relabels
  10. local relabels1

  11. levelsof for, local(groups)
  12.   
  13. local s_len=20  //Change to required length

  14. foreach g of local groups {

  15.   local label : label origin `g'
  16.   local len : length local label

  17.   if `len'>`s_len' {
  18.         forvalues i=1/  `=`len'/`s_len'+1' {
  19.             local p1 : piece `i' `s_len' of `"`label'"', nobreak
  20.             local relabels `"`relabels' `=char(34)+ "`p1'" +char(34)' "'
  21.         }

  22.         local relabels1  `relabels1'   `=`g'+1'    `"`relabels'"'
  23. local relabels
  24.        
  25.    }
  26. }

  27. graph hbar mpg, over(for, relabel(`relabels1' ))
  28. exit
复制代码

Graph.png



76
niuniuyiwan 在职认证  发表于 2015-8-31 08:47:19
  1. *<-------------修改后

  2. ssc install splitvallabels
  3. sysuse auto, clear
  4.    
  5. label define rep78 1 "Very low rating in 1978" ///
  6. 2 "Mediocre - Low Rating in 1978"              ///
  7. 3 "Medium Rating in 1978"                      ///
  8. 4 "Not bad rating in 1978, considering"        ///
  9. 5 "Excellent in 1978--the best of the year!"
  10.         
  11. label values rep78 rep78
  12.         
  13. splitvallabels rep78 if rep78>=3, length(11) recode
  14.         
  15. graph bar mpg if rep78>=3, over(rep78, relabel(`r(relabel)') ) scheme(s1)

  16. exit
复制代码

Graph.png



77
niuniuyiwan 在职认证  发表于 2015-8-31 08:50:15
  1. //Combining many graphs on a page
  2. sysuse auto, clear
  3. graph drop _all
  4. drop make
  5. foreach i of varlist _all{
  6.         capture confirm numeric variable `i' if _rc==0
  7.         histogram `i', name(`i')
  8.         local z "`z' `i'"
  9.         graph combine `z'
  10. }
复制代码

Graph.png


*修改后的do-file


78
niuniuyiwan 在职认证  发表于 2015-8-31 09:11:48
  1. //Changing the order of twoway graphs where the by() option is used

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

  6. //the following is an example of order that the graphs are in when
  7. //determined by Stata
  8. // ie. lowest rep78 value to highest
  9. sysuse auto,clear
  10. scatter mpg weight , by(rep78) name(a2, replace)

  11. //If you require the order to be the mean mpg of the levels of rep78,
  12. //the following can be done:  
  13.   
  14. egen mean1 = mean(mpg) , by(rep78)

  15. egen axis = axis1(mean1 rep78), label(rep78) reverse  // egenmore command

  16. scatter mpg weight , by(axis) name(a1, replace)
复制代码

a2.png



79
niuniuyiwan 在职认证  发表于 2015-8-31 09:13:23
  1. //Using the compact option in the by() option

  2. sysuse auto, clear

  3. forvalues i=10(10)40 {
  4.   local m`i'=string(`i'*0.4251437,"%6.2f")
  5. }

  6. twoway (scatter mpg weight, yaxis(1 2) xlabel(,angle(45)) ///
  7. yscale( axis(2, alt))                                     ///
  8. ylabel(                                                   ///
  9. 10 "`m10'"                                                ///
  10. 20 "`m20'"                                                ///
  11. 30 "`m30'"                                                ///
  12. 40 "`m40'"                                                ///
  13. ,axis(2))) ,                                              ///
  14. by(for, title( "km/L",  orientation(vertical)            ///
  15. size(med) position(3)) xrescale compact)  
复制代码

Graph.png



80
niuniuyiwan 在职认证  发表于 2015-8-31 09:14:48
  1. //Adding titles to the left hand side of the graph


  2. sysuse auto, clear

  3. fillin rep78 foreign
  4. twoway scatter price mpg, by(foreign rep78, cols(5) compact)

  5. egen group = group(foreign rep78)

  6. label define group 1 "Poor" ///
  7. 2 "Fair"         ///
  8. 3 "Average"      ///
  9. 4 "Good"         ///
  10. 5 "Excellent"    ///
  11. 6" "             ///
  12. 7" "             ///
  13. 8" "             ///
  14. 9" "             ///
  15. 10 " "  


  16. label value group group ///


  17. twoway scatter price mpg, ///
  18. by(group, cols(5)         ///
  19. r1title("Car type",       ///
  20. orientation(rvertical)    ///
  21. size(medsmall))           ///
  22. t1title("Repair status",  ///
  23. size(medsmall))           ///
  24. note("") compact)  


  25. gr_edit plotregion1.r1title[5].style.editstyle drawbox(yes) editcopy

  26. gr_edit plotregion1.r1title[5].as_textbox.setstyle, style(yes)
  27. gr_edit plotregion1.r1title[10].as_textbox.setstyle, style(yes)

  28. gr_edit plotregion1.r1title[5].text = {}
  29. gr_edit plotregion1.r1title[5].text.Arrpush Domestic

  30. gr_edit plotregion1.r1title[10].text = {}
  31. gr_edit plotregion1.r1title[10].text.Arrpush Foreign

  32. gr_edit plotregion1.r1title[5]._set_orientation rvertical
  33.                                        
复制代码

Graph.png



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

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