楼主: 黃河泉
17606 10

[学习心得] 如何绘制散点图和箱线图结合的图。 [推广有奖]

大师

52%

还不是VIP/贵宾

-

威望
3
论坛币
34526 个
通用积分
55889.9092
学术水平
3538 点
热心指数
3698 点
信用等级
2921 点
经验
308746 点
帖子
14500
精华
12
在线时间
7393 小时
注册时间
2016-7-9
最后登录
2024-4-25

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
原先之问题在此 https://bbs.pinggu.org/thread-6252218-1-1.html,而我上美国 Stata forum 寻求解答之答案于此 https://www.statalist.org/forums ... -in-the-same-figure,所有 credit 归于回答者。
box-and-scatter.png
其之 code 为:
  1. // ericmelse
  2. * Load example file:
  3. sysuse nlsw88, clear

  4. * The distribution underlying the box plot for inspection:
  5. histogram wage, width(0.1) start(0) lc(navy%50) horizontal yla(, ang(h))
  6. histogram hours, width(0.1) start(0) lc(navy%50) vertical

  7. * Graph the box plots for inspection:
  8. graph box wage, marker(1, ms(oh) mc(navy%50)) yla(, ang(h))
  9. graph box hours, marker(1, ms(oh) mc(navy%50)) yla(, ang(h)) hor

  10. * Compute required data for Boxplot of Wage:
  11. gen maxX = 84    // boxplot x-scale position
  12. * The median and quartiles are easiest:
  13. egen med_w = median(wage) //
  14. egen upq_w = pctile(wage), p(75) //
  15. egen loq_w = pctile(wage), p(25) //
  16. egen mean_w = mean(wage) //
  17. * We could now get the IQR by subtraction, upq - loq, which would be more efficient,
  18. * but we will mention that it has its own egen function.
  19. egen iqr_w = iqr(wage) //,
  20. * For upper limits of whiskers:
  21. egen upper_w = max(min(wage, upq_w + 1.5 * iqr_w)) //,
  22. * For lower limits of whiskers:
  23. egen lower_w = min(max(wage, loq_w - 1.5 * iqr_w)) //,

  24. * Compute required data for Boxplot of Wage:
  25. gen maxY = 43 // boxplot y-scale position
  26. * The median and quartiles are easiest:
  27. egen med_h = median(hours) //
  28. egen upq_h = pctile(hours), p(75) //
  29. egen loq_h = pctile(hours), p(25) //
  30. egen mean_h = mean(hours) //
  31. * We could now get the IQR by subtraction, upq - loq, which would be more efficient,
  32. * but we will mention that it has its own egen function.
  33. egen iqr_h = iqr(hours) //,
  34. * For upper limits of whiskers:
  35. egen upper_h = max(min(hours, upq_h + 1.5 * iqr_h)) //,
  36. * For lower limits of whiskers:
  37. egen lower_h = min(max(hours, loq_h - 1.5 * iqr_h)) //,

  38. * Boxplots and scatterplot in the same figure:
  39. twoway /// Box plot of Wage
  40. (rbar med_w upq_w maxX, blc(navy) bfc(white) barw(1.7) ) ///
  41. (rbar med_w loq_w maxX, blc(navy) bfc(white) barw(1.7) ) ///
  42. (rspike upq_w upper_w maxX , blc(navy) ) ///
  43. (rspike loq_w lower_w maxX , blc(navy) ) ///
  44. (rcap upper_w upper_w maxX , blc(navy) msize(*1) ) ///
  45. (rcap lower_w lower_w maxX , blc(navy) msize(*1) ) ///
  46. (scatter mean_w maxX, pstyle(p1) ms(dh) msize(*1) mls(*.8) mlc(gs9) ) /// Diamond for mean
  47. (scatter wage maxX if !inrange(wage, lower_w, upper_w), ms(oh) mc(navy%20) legend(off)) /// Outliers
  48. (rbar med_h upq_h maxY, blc(navy) bfc(white) barw(1.3) hor) /// Box plot of Hours
  49. (rbar med_h loq_h maxY, blc(navy) bfc(white) barw(1.3) hor) ///
  50. (rspike upq_h upper_h maxY , blc(navy) hor) ///
  51. (rspike loq_h lower_h maxY , blc(navy) hor) ///
  52. (rcap upper_h upper_h maxY , blc(navy) msize(*1) hor) ///
  53. (rcap lower_h lower_h maxY , blc(navy) msize(*1) hor) ///
  54. (scatter maxY mean_h , pstyle(p1) ms(dh) msize(*1) mls(*.8) mlc(gs9) ) /// Diamond for mean
  55. (scatter maxY hours if !inrange(hours, lower_h, upper_h), ms(oh) mc(navy%20) legend(off)) /// Outliers
  56. (scatter wage hours, msymbol(oh) /// Scatter plot of Wage & Hours
  57. legend(off) xscale(range(-1 80)) ///
  58. mcolor(navy%50)) ///
  59. , ytitle(Hourly wage) /// General plot controls
  60. xtitle(Usual hours worked)

  61. * The above final code can be edited to modify the plot using the regular twoway options etc.
复制代码

二维码

扫码加我 拉你入群

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

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

关键词:散点图 箱线图 distribution scatterplot INSPECTION

已有 4 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Sunknownay + 3 + 3 + 3 精彩帖子
guoyijj + 2 + 2 + 2 精彩帖子
葫芦娃大王 + 50 + 5 + 1 精彩帖子
Stakiny + 3 + 3 + 3 精彩帖子

总评分: 经验 + 50  论坛币 + 5  学术水平 + 9  热心指数 + 8  信用等级 + 8   查看全部评分

本帖被以下文库推荐

沙发
zabbyy 发表于 2018-3-2 08:35:38 |只看作者 |坛友微信交流群
谢谢老师分享!

使用道具

藤椅
葫芦娃大王 学生认证  发表于 2018-3-2 08:52:29 |只看作者 |坛友微信交流群
谢谢老师分享!

使用道具

板凳
奇犽dsp 学生认证  发表于 2018-3-2 10:25:40 |只看作者 |坛友微信交流群
谢谢老师分享!

使用道具

报纸
黃河泉 在职认证  发表于 2018-3-2 17:39:20 |只看作者 |坛友微信交流群
另外也有彩色的 boxplots 如下: box-and-scatter-color.png
其程序如下(接连上面的):
  1. // color: red/blue
  2. * Boxplots and scatterplot in the same figure, colors adjusted
  3. twoway /// Box plot of Wage (vertical)
  4. (rbar med_w upq_w maxX, blc(blue) bfc(white) barw(3) ) ///
  5. (rbar med_w loq_w maxX, blc(blue) bfc(white) barw(3) ) ///
  6. (rspike upq_w upper_w maxX , blc(blue) ) ///
  7. (rspike loq_w lower_w maxX , blc(blue) ) ///
  8. (rcap upper_w upper_w maxX , blc(blue) msize(*1) ) ///
  9. (rcap lower_w lower_w maxX , blc(blue) msize(*1) ) ///
  10. (scatter mean_w maxX, pstyle(p1) ms(dh) msize(*1) mls(*.8) mlc(navy%20) ) /// Diamond for mean (horizontal)
  11. (scatter wage maxX if !inrange(wage, lower_w, upper_w), ms(oh) mc(blue%20) legend(off)) /// Outliers
  12. (rbar med_h upq_h maxY, blc(red) bfc(white) barw(1.5) hor) /// Box plot of Hours
  13. (rbar med_h loq_h maxY, blc(red) bfc(white) barw(1.5) hor) ///
  14. (rspike upq_h upper_h maxY , blc(red) hor) ///
  15. (rspike loq_h lower_h maxY , blc(red) hor) ///
  16. (rcap upper_h upper_h maxY , blc(red) msize(*1) hor) ///
  17. (rcap lower_h lower_h maxY , blc(red) msize(*1) hor) ///
  18. (scatter maxY mean_h , pstyle(p1) ms(dh) msize(*1) mls(*.8) mlc(red%20) ) /// Diamond for mean
  19. (scatter maxY hours if !inrange(hours, lower_h, upper_h), ms(oh) mc(red%20) legend(off)) /// Outliers
  20. (scatter wage hours, msymbol(oh) /// Scatter plot of Wage & Hours
  21.            legend(off) xscale(range(-1 80)) ///
  22.            mcolor(navy%50)) ///
  23. , xsize(9) ysize(9) graphregion(margin(1 -3 1 -3)) /// General plot controls from here
  24. yscale(noextend) xscale(noextend) /// Disable axis extension
  25. ytitle(Hourly wage, margin(0 2 0 0)) /// specified margins; left, right, bottom, top
  26. xtitle(Usual hours worked, margin(0 0 0 2)) // last code line
复制代码

使用道具

地板
yuqr1986 学生认证  发表于 2018-3-5 09:04:20 |只看作者 |坛友微信交流群
这个程序有用,多谢黄老师分享

使用道具

7
黃河泉 在职认证  发表于 2018-3-5 09:29:19 |只看作者 |坛友微信交流群
yuqr1986 发表于 2018-3-5 09:04
这个程序有用,多谢黄老师分享
老实说,还蛮 fancy 的。

使用道具

8
阿璇2016 发表于 2018-3-5 16:34:58 |只看作者 |坛友微信交流群
最后画图的程序看不懂头都大了
老师厉害!

使用道具

9
黃河泉 在职认证  发表于 2018-3-5 16:39:58 |只看作者 |坛友微信交流群
阿璇2016 发表于 2018-3-5 16:34
最后画图的程序看不懂头都大了
老师厉害!
这是别人写的,不是我,我也不是很懂(还没花时间去了解)。借花献佛而已。

使用道具

10
mzdg 在职认证  学生认证  发表于 2018-3-6 12:41:08 |只看作者 |坛友微信交流群
厉害厉害,黄老师出品 必为精品

使用道具

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

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

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

GMT+8, 2024-4-26 18:33