Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:温凯迪 (中山大学)
邮箱:wkd6107@163.com
目录
燃油效率,定义为产生特定推力或马力使用的燃油所含的能量除以这份燃油所含的全部潜在能量。其评价指标为每加仑燃油或每千克燃油所能产生的公里数,用 RPM/kg 或 RPM/gal 表示。不同车型的燃油效率不同,本图以 ggplot2
中的 mpg.csv 作为数据来源,分汽车种类描绘了城市燃油效率 (cty),高速公路燃油效率 (hwy) 两个变量之间的关系,如下图所示。
本图将散点图、lowess 拟合曲线与单变量正态分布密度图结合在同一个图上,有利于我们更精确的描述两个变量间的数据关系。散点图可以直观标示两变量间的数量关系,lowess 拟合趋势线展示了两变量的局部规律和趋势,单变量正态分布密度图表示了两变量分别的正态分布。
grc1leg
命令
grc1leg
用于组合图形,在具体用法上与 graph combine
相同,只是它为所有组合图形显示一个公共图例,该公共图例是组合图形中的图例之一。
* 命令安装
net install grc1leg, from("http://www.stata.com/users/vwiggins")
* 命令语法
grc1leg name [name ...] [, combine_options legend_options ]
其中,name
是图片名称。combine_options
包括:
colfirst
、rows
、cols
、holes
:用于指定在最终生成的组合图中各图的排列顺序,colfirst
表 示该图片显示在下面一列;rows()
和 cols()
指定图片显示在具体的行和列中;holes
指定留出空白的区域。iscale
可用于指定文本和标志的字体大小。imargin
用于确定单个图表的边缘长度。ycommon
和 xcommon
为 X 轴和 Y 轴指定常用刻度。scheme
为图片设定特定模板。name
指定组合图的名称。legend_options
包括:
legendfrom
指定要从中获取组合图例的图形,默认值为列表中的第一个图形。position
和 ring
用于覆盖图例的默认位置,该位置通常位于绘图区域下方的居中。position
可以使图例位于绘图区域本身内,并允许将图例放置在绘图内。根据 12 小时制表盘上的小时指定图例放置方向,如 position(12)
意为将图例添加在绘图区域正上方。ring
可以指定图例与绘图区域间的距离。ring(0)
表示可将图例放置于绘图区域内部;ring(k)
当 k>0 时,图例被放置在绘图区域以外。span
将图例放置在跨越整个图片宽度或高度的区域。schemepack
命令
schemepack
命令包括许多 Stata 预设图片方案,如 white_tableau
、black_tableau
、gg_tableau
、white_cividis
、black_cividis
、gg_cividis
等。
* 命令安装
ssc install schemepack, replace
palettes
及colrspace
命令
* 命令安装
ssc install palettes, replace
ssc install colrspace, replace
该绘图方案来自 fahad-mirza 的 github 主页,文件源代码为 Sideplots_Distribution_by_Group_Stata.do。
.
. clear all
. * Necessary Package Installations (One time only)
. * net install grc1leg, from("http://www.stata.com/users/vwiggins") replace
. * ssc install schemepack, replace
. * ssc install palettes, replace
. * ssc install colrspace, replace
. * Loading the example dataset from GitHub
. lxhget mpg.txt, replace
. import delimited using mpg.txt, clear
. * Using loop to write and store the plotting commands and syntax by class
. levelsof class, local(classes)
`"2seater"' `"compact"' `"midsize"' `"minivan"' `"pickup"' `"subcompact"' `"suv"'
. foreach class of local classes {
2. local sctr `sctr' scatter cty hwy if class == "`class'", ///
> mcolor(%60) mlwidth(0) ||
3. quietly summarize cty if class == "`class'"
4. local cty `cty' function normalden(x, `r(mean)', `r(sd)'), ///
> horizontal range(cty) base(0) n(500) xlabel(, nogrid) ///
> recast(area) fcolor(%50) lwidth(0) ||
5. quietly summarize hwy if class == "`class'"
6. local hwy `hwy' function normalden(x, `r(mean)', `r(sd)'), ///
> range(hwy) base(0) n(500) ylabel(, nogrid) recast(area) ///
> fcolor(%50) lwidth(0) ||
7. }
. * Plotting each of the above saved commands and storing them for combining later using name()
. twoway `sctr' || lowess cty hwy ||, legend(off) name(lowess) ///
> ytitle("City MPG") xtitle("Highway MPG") ysc(r(10(5)35)) ///
> xsc(r(10(10)40)) xlabel(, nogrid) ylabel(, nogrid)
. twoway `cty', graphregion(margin(b=0)) name(cty) leg(off) ///
> fxsize(25) ytitle("") ylabel(none) xtitle("⠀") ysc(r(10(5)35))
. twoway `hwy', graphregion(margin(b=0)) name(hwy) leg(label(1 "2 Seater") ///
> label(2 "Compact") label(3 "Mid-Size") label(4 "Minivan") ///
> label(5 "Pickup") label(6 "Sub-Compact") label(7 "SUV") size(2) ///
> row(2) col(4)) fysize(25) xtitle("") xla(none) ytitle("⠀") xsc(r(10(10)50))
. * Combining all the plots saved above
. grc1leg hwy lowess cty, title("{bf}Fuel Economy by Vehicle Type", color(navy) ///
> size(3) j(left) pos(11) margin(l=6)) subtitle("Side plots for density", ///
> size(2) pos(11) margin(l=6)) legendfrom(hwy) span hole(2) rows(3) ///
> imargin(zero) commonscheme scheme(white_tableau)
. * Exporting the visual
. graph export "Sideplots_Distribution_by_Group_Stata.png", ///
> as(png) name("Graph") width(1920) replace
Note:产生如下推文列表的 Stata 命令为:
lianxh 散点图, m
安装最新版lianxh
命令:
ssc install lianxh, replace
免费公开课
最新课程-直播课
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 文本分析、机器学习、效率专题、生存分析等 | |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
和songbl
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh