请选择 进入手机版 | 继续访问电脑版
楼主: 黃河泉
1154 4

[程序分享] twby --- 創建圖形的交叉列表 [推广有奖]

大师

52%

还不是VIP/贵宾

-

威望
3
论坛币
34526 个
通用积分
55777.7809
学术水平
3537 点
热心指数
3697 点
信用等级
2920 点
经验
308719 点
帖子
14491
精华
12
在线时间
7389 小时
注册时间
2016-7-9
最后登录
2024-3-26

黃河泉 在职认证  发表于 2019-9-23 18:34:15 |显示全部楼层 |坛友微信交流群
相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Maarten Buis 推出 (ssc install) twby:

  1. webuse auto2, clear

  2. set scheme s1color

  3. // ex1
  4. twby foreign rep78 : scatter price weight

  5. // ex2
  6. // use more sensible units
  7. replace weight = 0.00045359237*weight
  8. label variable weight "Weight (tonnes)"
  9. replace price = price / 1000
  10. label variable price "Price (1000s {c S|})"

  11. //create the graph
  12. twby foreign rep78, compact :    ///
  13.     scatter price weight,        ///
  14.     ylab(,angle(0)) xlab(1(.5)2)

  15. // ex3
  16. // open example data   
  17. sysuse nlsw88, clear

  18. // create the necessary categorical variables
  19. gen byte urban = c_city + smsa if !missing(c_city,smsa)
  20. label define urban 2 "central city" ///
  21.                    1 "suburban"     ///
  22.                    0 "rural"
  23. label value urban urban
  24. label variable urban "urbanicity"

  25. gen byte marst = !never_married + married if !missing(never_married,married)
  26. label define marst 0 "never married" ///
  27.                    1 "widowed/divorced" ///
  28.                    2 "married"
  29. label value marst marst
  30. label var marst "marital status"
  31.                   
  32. gen byte edcat = cond(grade <  12, 1,     ///
  33.                  cond(grade == 12, 2,     ///
  34.                  cond(grade <  16, 3,4))) ///
  35.                  if !missing(grade)
  36. label variable edcat "education"
  37. label define edcat 1 "< highschool"    ///
  38.                    2 "highschool"      ///
  39.                    3 "some college"    ///
  40.                    4 "college"            
  41. label value edcat edcat                  

  42. // the three way table we want to visualize
  43. bys edcat: tab urban marst, row nofreq

  44. // recreate that table as variables
  45. contract edcat marst urban, zero nomiss
  46. egen tot = total(_freq), by(urban edcat)
  47. gen perc = _freq / tot *100

  48. // variables that helps display the numbers in the graph
  49. gen lab = strofreal(perc, "%5.0f")
  50. gen y = -5

  51. // the graph
  52. twby urban marst ,                                             ///
  53.         compact left xoffset(0.5) legend(off)                  ///
  54.         title("Percentage in each marital status"              ///
  55.               "given education and urbanicity") :              ///
  56.     twoway bar perc edcat ,                                    ///
  57.         xlab(1/4, val alt) yscale(range(0 75))                 ///
  58.         ylab(none) ytitle("") barw(.5)                      || ///
  59.     scatter y edcat ,                                          ///
  60.         msymbol(none) mlab(lab) mlabpos(0) mlabcolor(black)
复制代码
twby-ex1.png

twby-ex2.png

twby-ex3.png

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


已有 1 人评分经验 论坛币 学术水平 收起 理由
葫芦娃大王 + 10 + 10 + 1 精彩帖子

总评分: 经验 + 10  论坛币 + 10  学术水平 + 1   查看全部评分

三重虫 发表于 2019-9-23 21:58:17 |显示全部楼层 |坛友微信交流群
感谢分享!

使用道具

无情兽 发表于 2019-9-23 22:28:11 |显示全部楼层 |坛友微信交流群
谢谢黄老师分享

使用道具

葫芦娃大王 学生认证  发表于 2019-9-24 22:42:47 |显示全部楼层 |坛友微信交流群
谢谢黄老师分享

使用道具

tianwk 发表于 2020-3-31 15:24:05 |显示全部楼层 |坛友微信交流群
thanks for sharing

使用道具

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

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-28 18:45