设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2374|回复: 0

stata:作图——区间阴影,柱状图

[复制链接]

26

主题

207

金钱

343

积分

入门用户

发表于 2019-6-14 14:26:06 | 显示全部楼层 |阅读模式

做US GDP相关的图
代码:
  1. import excel us_gdp.xlsx, firstrow case(lower) clear
  2. tsset year
  3. label variable gdp_growth_rate "GDP GROWTH RATE"
  4. twoway                                                                                ///
  5. function y=20,range(1929 1933) recast(area) color(green) base(0) yaxis(1)  || ///
  6. function y=20,range(1970 1983) recast(area) color(midblue) base(0) yaxis(1)|| ///
  7. function y=20,range(2008 2009) recast(area) color(red) base(0) yaxis(1)    || ///
  8. function y=0.0333977,range(1929 2017) yaxis(2) lcolor(purple)              || ///
  9. tsline ngdp,lp(dash) yaxis(1)  ytitle("GDP" "(unit: trillion)")            || ///
  10. tsline rgdp,lp(shortdash) yaxis(1)                                         || ///
  11. tsline gdp_growth_rate,yaxis(2)  xlabel(1929(8)2017)                          ///
  12.    ytitle("GDP GROWTH RATE" "(based on RGDP)",axis(2)) xtitle("year")          ///
  13.    legend(order(1 "Great Depression" 2 "Stagflation" 3 "Financial Crisis"      ///
  14.    4 "NGDP" 5 "RGDP" 6 "GDP GROWTH RATE")) scheme(s1color)                     ///
  15.    note("Source:Bureau of Economic Analysis")
  16. graph export us_gdp.pdf,replace
复制代码


做出的图如下:
1.png


做US inflation相关的图
代码1:
  1. import excel us_infla_unr_gdpr.xlsx, firstrow case(lower) clear
  2. rename inflation inflation_rate
  3. label variable unr "Unemployment rate"
  4. label variable gdp_rate "gdp growth rate"
  5. label variable inflation_rate "Inflation rate"
  6. tsset year
  7. twoway                                                                      ///
  8. function y=0.2,range(1970 1983) recast(area) color(midblue) base(-0.12) || ///
  9. function y=0.0311798,range(1929 2017) lcolor(purple)                    || ///
  10. tsline inflation_rate,lcolor(orange_red) xlabel(1929(8)2017)               ///
  11. legend(order(1 "Stagflation" 3 "Inflation rate")) ytitle("Inflation rate") ///
  12. xtitle("year") note("Source:  Bureau of Labor Statistics")
  13. graph export us_infla_0.pdf,replace
复制代码


做出的图如下:
2.png

代码2:
  1. twoway                                                                           ///
  2. function y=0.3,range(1970 1983) recast(area) color(midblue) base(-0.12)     || ///
  3. function y=0.0311798,range(1929 2017) lcolor(purple)                       || ///
  4. tsline inflation_rate,lcolor(orange_red)                                  || ///
  5. tsline unr ,lp(dash) xlabel(1929(8)2017)                                    ///
  6. legend(order(1 "Stagflation" 3 "Inflation rate" 4 "Unemployment rate"))    ///
  7. xtitle("year") note("Source:Bureau of Labor Statistics")
  8. graph export us_unr_infla_1.pdf,replace
复制代码


做出的图如下:
3.png

代码3:
  1. gen year1= real(substr(string(year),-2,.))
  2. twoway connected inflation_rate unr if year>=1961 & year<=1990 ///
  3.   ,mlabel(year1) note("Source:Bureau of Labor Statistics")
  4. graph export us_unr_infla_2.pdf,replace
复制代码

做出的图如下:
4.png

代码4:
  1. twoway scatter inflation_rate unr || lfit inflation_rate unr ,ytitle("Inflation rate") ///
  2. xtitle("Unemployment rate") title("Time:1929-2017") legend(order(2 "Fitted line"))
  3. graph export us_unr_infla_3.pdf,replace
复制代码


做出的图如下:
5.png


test 绘制区间阴影
代码如下:
  1. ssc install freduse, replace  //gets program online
  2. freduse MPRIME, clear
  3. generate ym = mofd(daten)
  4. generate ym1 = mofd(daten)
  5. tsset ym, monthly            //相当于同时设置了 format %tm ym
  6. twoway                                                                     ///
  7. function y=20.705,range(119 130) recast(area) color(gs12) base(4.7025) || ///
  8. function y=20.705,range(166 182) recast(area) color(gs12) base(4.7025) || ///
  9. function y=20.705,range(240 274) recast(area) color(gs12) base(4.7025) || ///
  10. function y= 5    ,range(119 274) lstyle(grid)                          || ///
  11. function y=10    ,range(119 274) lstyle(grid)                          || ///
  12. function y=15    ,range(119 274) lstyle(grid)                          || ///
  13. function y=20    ,range(119 274) lstyle(grid)                          || ///
  14. function y=ym(1980,11), range(4.7025 20.705) horizontal lstyle(grid)   || ///
  15. tsline MPRIME if tin(1970m1,1990m1), xlabel(,format(%tm)) lstyle(p1)      ///
  16. legend(order(5 1 "Recession" 9)) tlabel(,grid) scheme(s2color) ///
  17. graphregion(color(white))
  18.    //horizontal 表示水平作图
复制代码


做出的图如下:
6.png


test 绘制美丽的柱状图
可参考:http://www.360doc.com/content/17/0322/11/39103730_639104545.shtml
代码1:
  1. import excel using bar_test.xlsx,firstrow clear
  2. rename 一般公共预算收入 bu_re
  3. label variable year "年份"
  4. twoway bar bu_re year

  5.   //(1)color(colorstyle)和barwidth(#)
  6. twoway bar bu_re year,title("默认")
  7. graph save bar,replace

  8. twoway bar bu_re year,color(red) title("red")
  9. graph save cnbar,replace

  10. twoway bar bu_re year,barwidth(0.3) title("宽0.3")
  11. graph save wnbar,replace

  12. twoway bar bu_re year,color(red) barwidth(0.3) title("red and 宽0.3")
  13. graph save cwnbar,replace

  14. graph combine bar.gph cnbar.gph wnbar.gph cwnbar.gph
复制代码


做出的图如下:
7.png

代码2:
  1. //(2)prefix和suffix
  2. twoway bar bu_re year,title("默认")
  3. graph save bar,replace

  4. twoway bar bu_re year,ytitle("亿元",suffix) title("suffix")
  5. graph save suffix,replace

  6. twoway bar bu_re year,ytitle("亿元",prefix) title("prefix")
  7. graph save prefix,replace

  8. graph combine bar.gph suffix.gph prefix.gph
  9. graph save 3,replace
复制代码


做出的图如下:
8.png

代码3:
  1. //(3)orientation()

  2. twoway bar bu_re year,ytitle("亿元")title("默认")
  3. graph save bar,replace

  4. twoway bar bu_re year,ytitle("亿元",orientation(horizontal)) title("orientation(horizontal)")
  5. graph save oh,replace

  6. twoway bar bu_re year,ytitle("亿元",orientation(vertical)) title("orientation(vertical)")
  7. graph save ov,replace

  8. graph combine bar.gph oh.gph ov.gph
复制代码

做出的图如下:
9.png

代码4:
  1. //(4)placement()
  2. twoway bar bu_re year,ytitle("亿元")title("默认")
  3. graph save bar,replace

  4. twoway bar bu_re year,ytitle("亿元",placement(north)) title("placement(north)")
  5. graph save pn,replace

  6. twoway bar bu_re year,ytitle("亿元",placement(center)) title("placement(center)")
  7. graph save pc,replace

  8. twoway bar bu_re year,ytitle("亿元",placement(south)) title("placement(south)")
  9. graph save ps,replace

  10. graph combine bar.gph pn.gph pc.gph ps.gph
复制代码

做出的图如下:
10.png

代码5:
  1. //做出我们想要的柱状图
  2. twoway bar bu_re year,color(midblue)  barwidth(0.3)       ///
  3. ylabel(0(500)3500,angle(horizontal)) xlabel(2010(1)2016) ///
  4. ytitle("亿元", orientation(horizontal) placement(north))
复制代码

做出的图如下:
11.png
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表