Plot 描述组平均值的连接线图,按类别划分
我有数据表明,在两个变量中有许多观察结果,比如Plot 描述组平均值的连接线图,按类别划分,plot,graph,line,stata,connect,Plot,Graph,Line,Stata,Connect,我有数据表明,在两个变量中有许多观察结果,比如rep78和净空 我想取第三个变量的平均值,比如每个rep78+headloom单元格中的weight。对于每个headloom类别,我想分别用rep78在x轴上绘制(连接点图)这些平均值。我认为更直观的方法是用year替换rep78,用state替换headloom 我已经能够通过以下代码获得我想要的: sysuse auto2, clear bys rep78 headroom: egen mean_w=mean(weight) twoway
rep78
和净空
我想取第三个变量的平均值,比如每个rep78
+headloom
单元格中的weight
。对于每个headloom
类别,我想分别用rep78
在x轴上绘制(连接点图)这些平均值。我认为更直观的方法是用year
替换rep78
,用state
替换headloom
我已经能够通过以下代码获得我想要的:
sysuse auto2, clear
bys rep78 headroom: egen mean_w=mean(weight)
twoway (scatter mean_w rep78 if headroom==1.5, connect(l)) ///
(scatter mean_w rep78 if headroom==2.0, connect(l)) ///
(scatter mean_w rep78 if headroom==2.5, connect(l)) ///
(scatter mean_w rep78 if headroom==3.0, connect(l)) ///
(scatter mean_w rep78 if headroom==3.5, connect(l)) ///
(scatter mean_w rep78 if headroom==4.0, connect(l)) ///
(scatter mean_w rep78 if headroom==4.5, connect(l)) ///
(scatter mean_w rep78 if headroom==5.0, connect(l)), ///
legend(label(1 "1.5") label(2 "2.0") label(3 "2.5") ///
label(4 "3.0") label(5 "3.5") label(6 "4.0") ///
label(7 "4.5") label(8 "5.0"))
但是,是否有一种更简单(即更短)的方法来实现这一点
我很乐意简化此代码的任何部分。感谢MCVE
你说得对。下面是一个较短的解决方案,用于获得相同的图形,可以对小细节进行调整:
sepscatter mean_w rep78, sep(headroom) recast(connected) name(G2, replace)
使用SSC发布的sepstrict
。在StatList上使用sepstrict
作为搜索词可以找到更多示例 谢谢你的MCVE
你说得对。下面是一个较短的解决方案,用于获得相同的图形,可以对小细节进行调整:
sepscatter mean_w rep78, sep(headroom) recast(connected) name(G2, replace)
使用SSC发布的
sepstrict
。在StatList上使用sepstrict
作为搜索词可以找到更多示例 我看不出这里有什么复杂的东西
社区贡献的命令sepstrict
是一个很好的包装器,但是如果担心代码的简洁性,您也可以在不安装任何东西的情况下执行以下操作:
sysuse auto2, clear
bysort rep78 headroom: egen mean_w=mean(weight)
levelsof headroom, local(head)
foreach x of local head {
local scatterlist `scatterlist' (scatter mean_w rep78 if headroom == `x', connect(l))
}
twoway `scatterlist'
为了减少任何精度问题,如果变量未生成为double,可以使用float()
:
(scatter mean_w rep78 if headroom == float(`x'), connect(l))
我看这里没有什么复杂的东西 社区贡献的命令
sepstrict
是一个很好的包装器,但是如果担心代码的简洁性,您也可以在不安装任何东西的情况下执行以下操作:
sysuse auto2, clear
bysort rep78 headroom: egen mean_w=mean(weight)
levelsof headroom, local(head)
foreach x of local head {
local scatterlist `scatterlist' (scatter mean_w rep78 if headroom == `x', connect(l))
}
twoway `scatterlist'
为了减少任何精度问题,如果变量未生成为double,可以使用float()
:
(scatter mean_w rep78 if headroom == float(`x'), connect(l))