📄 bargraph_g.class
字号:
di as error "`shadevar' not in graph
exit 198
}
}
end
program _parse_over
gettoken bymac 0 : 0
gettoken o1mac 0 : 0
gettoken o2mac 0 : 0
gettoken o3mac 0 : 0
gettoken optmac 0 : 0
gettoken colon 0 : 0
gettoken asbars 0 : 0
gettoken asgroups 0 : 0
local i 0
syntax [, Over(string asis) * ]
while `"`over'"' != `""' {
_over1 var opts : `over'
local o`++i' `"`opts'"'
local by `var' `by'
local 0 `", `options'"'
syntax [, Over(string asis) * ]
}
if `i' > 3 {
di as error "may not specify more than 3 over() options"
exit 198
}
if `i' == 3 {
c_local `o1mac' `o1' // varopt
c_local `o2mac' `o2' // grpopt
c_local `o3mac' `o3' // supopt
}
else {
if `asbars' {
c_local `o1mac' `o1' // varopt
c_local `o2mac' `o2' // grpopt
}
else if `asgroups' & `i' == 1 {
c_local `o3mac' `o1' // supopt
}
else {
c_local `o2mac' `o1' // grpopt
c_local `o3mac' `o2' // supopt
}
}
c_local `bymac' `by'
c_local `optmac' `"`options'"'
end
program _over1
gettoken vmac 0 : 0
gettoken omac 0 : 0
gettoken colon 0 : 0
syntax varlist(min=1 max=1) [ , noLABel * ]
if ("`label'" != "") label values `varlist'
c_local `vmac' `varlist'
c_local `omac' `"`options'"'
end
program _set_labels
gettoken varnm labels : 0
local lbl : value label `varnm'
if "`lbl'" == "" {
tempname lbl
local lbl _t_`lbl'
label values `varnm' `lbl'
}
capture label define `lbl' `labels' , modify
if _rc {
di as error "option labels() incorrectly specified"
di as error in smcl `"{p 8 8} expects, # "label" # label ..."'
exit 198
}
end
program _sub_tempnames
gettoken tmac rest : 0
gettoken colon rest : rest
gettoken prefix rest : rest
// substitute <prefix># for any tempnames
local vlist `"`rest'"'
local i 0
foreach var of local rest {
if substr(`"`var'"', 1, 2) == "__" {
if ! `:list var in done' {
rename `var' `prefix'`++i'
local vlist : subinstr local vlist ///
"`var'" "`prefix'`i'" , all
local done `"`done' `var'"'
}
}
}
c_local `tmac' `vlist'
end
program _sub_vtempnames
args vmac amac colon prefix rest anything
// substitute <prefix># for any tempnames
local vlist `"`rest'"'
local i 0
foreach var of local rest {
if substr(`"`var'"', 1, 2) == "__" {
if ! `:list var in done' {
rename `var' `prefix'`++i'
local vlist : subinstr local vlist ///
"`var'" "`prefix'`i'" , all
local anything : subinstr local anything ///
"`var'" "`prefix'`i'" , all
local done `"`done' `var'"'
}
}
}
c_local `vmac' `"`vlist'"'
c_local `amac' `"`anything'"'
end
program SetSort
gettoken levname sort : 0
local g = cond("`levname'"== "" , "grp" , substr("`levname'", 1, 3))
capture numlist "`sort'" , min(1) max(1) range(>0) integer
if _rc { // varname
gettoken stat name : sort , match(paren)
if ("`paren'" == "") {
local name `stat'
local stat (mean) _`g'0_sort=`name'
}
else local stat (`stat') _`g'0_sort=`name'
confirm numeric variable `name'
.PushDo ._nsorts = `._nsorts' + 1
.PushDo .`g'srt = 1 + `._ngrps' + `._nsorts'
class exit "`stat'"
}
// vargrp #
._height_sorts = `"`._height_sorts' "`g' `sort'" "'
end
program PushDo
`0'
.__LOG.Arrpush `0'
end
program _relabel_var
args var
forvalues j = 1/`=_N' {
label define _T_relbl `j' `"`:label (`var') `=`var'[`j']''"' , add
}
qui replace `var' = _n
label values `var' _T_relbl
end
program BoxData
syntax varlist(numeric ts) [if] [aw fw pw] [, BY(string) CW ///
TOTal(integer 0) noLABel VARGROUP(name) SORTVARS(namelist) ]
if "`:list dups varlist'" != "" {
di as error "variables must be unique, the following are repeated:"
di as error in smcl "{p 8 8}`:list dups varlist'"
exit 198
}
if `"`exp'"' != `""' { // weight
tempname wtvar
qui gen double `wtvar' `exp'
local exp "= `wtvar'"
if ("`weight'" == "pweight") local weight aweight
}
if ("`cw'" == "") local novar novarlist // sample
marksample touse , `novar'
qui keep if `touse'
local unused : subinstr local varlist "." "." , count(local is_ts)
if `is_ts' {
tsrevar `varlist' , list
local tslist `r(varlist)'
_ts tvar pvar , panel
}
else local tslist `varlist'
qui keep `tslist' `by' `sortvars' `tvar' `pvar' `wtvar' `.mlabvars'
if ("`vargroup'" == "_spacer") {
qui gen byte _spacer = 1
local vargroup _variables
}
local mlabvars `.mlabvars'
local k 0
foreach var of local varlist { // labels
capture local lab : variable label `var'
local ts = _rc
if (`ts' | "`lab'" == "" | "`label'" != "") local lab `var'
label define _T_barlbl `++k' `"`lab'"' , add
if (`ts') qui gen _bd_`k' = `var'
else {
if `:list var in mlabvars' {
gen _bd_`k' = `var'
}
else rename `var' _bd_`k'
}
}
capture drop `pvar' `tvar'
if `:word count `varlist'' > 1 {
tempname id
gen long `id' = _n
qui reshape long _bd_ , i(`id') j(`vargroup')
label values `vargroup' _T_barlbl
}
else {
capture confirm variable `vargroup'
if _rc {
gen `vargroup' = 1
label values `vargroup' _T_barlbl
}
}
if (`:word count `by'' < 3) local by `by' `vargroup'
unab varlist : _bd_* , max(1)
if (`total') preserve
if "`by'" != "" { // Make data
local by_prefix bysort `by' :
}
`by_prefix' Box1By `varlist' [`weight'`exp'] , byvars(`by') ///
sortvars(`sortvars')
if (c(N) == 0) local K 0
qui drop if `varlist' >= . // keep outliers
if `total' { // all option
tempfile dat
qui save `"`dat'"'
restore
BoxAll `varlist' [`weight'`exp'] , byvars(`by') ///
sortvars(`sortvars') offset(`K') total(`total')
qui append using `"`dat'"'
}
qui gen _box_sort = 9999 // box sort
.PushDo ._nsorts = `._nsorts' + 1
.PushDo .boxsrt = 1 + `._ngrps' + `._nsorts'
// box data
local N = _N + 1
qui set obs `=max(0, `N'+5*`K'-1)'
forvalues i = 1/`K' {
local N4 = `N' + 4
gettoken val rest : byvals`i'
foreach var in `by' `sortvars' {
if substr("`:type `var''", 1, 3) == "str" {
qui replace `var' = `"`val'"' in `N'/`N4'
}
else qui replace `var' = `val' in `N'/`N4'
gettoken val rest : rest
}
local j 0
foreach stat of local bystat`i' {
local n = `N' + `j++'
qui replace `varlist' = `stat' in `n'
qui replace _box_sort = `j' in `n'
}
local N = `N' + 5
}
capture confirm variable _spacer
if (! _rc) qui replace _spacer = 1
rename `varlist' _values
end
program Box1By , byable(recall , noheader)
syntax varname(ts) [aw fw pw] , BYVARS(varlist) ///
[ SORTVARS(varlist) OFFSET(integer 0) ]
marksample touse0 , novarlist
marksample touse
qui summarize `varlist' [`weight'`exp'] if `touse' , detail
local p25 = r(p25)
local p50 = r(p50)
local p75 = r(p75)
local adjl0 = 2.5*`p25' - 1.5*`p75'
local adjh0 = 2.5*`p75' - 1.5*`p25'
if `adjl0' == `adjh0' {
// IQR is zero
local adjl .
local adjh .
qui replace `varlist' = . ///
if `touse' & ///
(`adjl0' == `varlist') & ///
(`varlist' == `adjh0')
}
else {
// non-zero IQR
summarize `varlist' if `touse' & `varlist' >= `adjl0' & ///
`varlist' < `p25', meanonly
local adjl = r(min)
summarize `varlist' if `touse' & `varlist' <= `adjh0' & ///
`varlist' > `p75' , meanonly
local adjh = r(max)
qui replace `varlist' = . ///
if `touse' & ///
(`adjl0' < `varlist') & ///
(`varlist' < `adjh0')
}
tempname id
gen `id' = _n
summarize `id' if `touse0' , meanonly
local j = r(min)
foreach v of local byvars {
local byvals `"`byvals' `"`=`v'[`j']'"'"'
}
foreach v of local sortvars {
local byvals `"`byvals' `"`=`v'[`j']'"'"'
}
c_local bystat`=_byindex()+`offset'' `p50' `p25' `p75' `adjl' `adjh'
c_local byvals`=_byindex()+`offset'' `"`byvals'"'
c_local K = _byindex()+`offset'
end
program BoxAll
syntax varname(ts) [aw fw pw] , BYVARS(varlist) [ SORTVARS(varlist) ///
TOTal(integer 0) OFFSET(integer 0) ]
local k_by : word count `byvars'
local totalvar : word `=`k_by'-`total'+1' of `byvars'
capture confirm variable _spacer
if (! _rc) qui replace _spacer = 1
// create total category
// and label it
if substr("`:type `totalvar''", 1, 3) != "str" {
summarize `totalvar' , meanonly
local totalcat = `r(max)' + 1
qui replace `totalvar' = `totalcat'
local totlbl : value label `totalvar'
if ("`totlbl'" == "") {
local totlbl _t_totlbl
label values `totalvar' _t_totlbl
}
label define `totlbl' `totalcat' "(total)" , modify
}
else qui replace `totalvar' = "(total)"
foreach srt of local sortvars { // total at end
qui summarize `srt' , meanonly
qui replace `srt' = 1.1 * r(max)
}
// make total box data
if "`byvars'" != "" { // Make data
local by_prefix bysort `byvars' :
}
local K0 = `offset' + 1
`by_prefix' Box1By `varlist' [`weight'`exp'] , ///
byvars(`byvars') sortvars(`sortvars') offset(`offset')
qui drop if `varlist' >= . // keep only outliers
// pass back box data
forvalues k = `K0'/`K' {
c_local bystat`k' `"`bystat`k''"'
c_local byvals`k' `"`byvals`k''"'
}
c_local K `K'
end
program LabelMissing
exit
foreach v of 0 {
local lbl : value label `v'
if "`lbl'" == "" {
local lbl _tm_`lbl'
label values `v' `lbl'
}
if
label
}
end
/* -------------------------------------------------------------------------*/
/* Creates a bargraph given an existing sersets array.
Usage: .makegraph , graph_options bargraph_opts
*/
program makegraph
syntax [ , DOTChart BOXPLOT ///
HORizontal VERTical STACK PERcentages ///
EXCLUDE0 YREVerse ///
BARGAP(string) BOXGAP(string) OUTERGAP(string) ///
LINEGap(string) LINEType(string) NDOTs(integer -99) ///
INTENsity(string) LINTENsity(string) ///
ALsize(string) CAPsize(string) ///
MEDType(string) MEDLine(string) MEDMarker(string) ///
NOCWHiskers CWHiskers ///
RWIDth(string) ///
LOWEXtension(real -999) HIGHEXtension(real -999) ///
NOEXTendlines EXTendlines ///
YALTernate XALTernate ///
STYLE(passthru) SHOWYVARs ///
VARAXOPT(string asis) AXOPT(string asis) ///
SUPERAXOPT(string asis) ///
GSTYle(string) ///
noFILL ///
PCYCle(real -999) ///
ASPECTratio(string asis) ///
BNDOPTIONS(string) * ]
local origopts `"`options'"'
tempname log // use log tools
.`log' = {}
if `"`aspectratio'"' != `""' {
_fr_aspect_parse_and_log `log' `aspectratio'
}
if `pcycle' != -999 {
.`log'.Arrpush .style.editstyle pcycle(`pcycle') editcopy
}
else {
local pcycle `.style.pcycle.val'
}
if ("`horizontal'" != "") .horiz_signal = 1
if ( `.horiz_signal' & "`vertical'"=="" ) local horizontal "horizontal"
if ( `.dot_signal' ) local dotchart "dotchart"
if ( `.box_signal' ) local boxplot "boxplot"
if "`dotchart'" != "" {
.graphstyle.ref = .barstyle.new , style(scheme dot)
.dotplot.set_true
if "`vertical'" == "" { // dots default horiz
local horizontal horizontal
}
}
else if "`boxplot'" != "" {
.graphstyle.ref = .barstyle.new , style(scheme box)
.boxplot.set_true
}
else .graphstyle.ref = .barstyle.new , style(scheme)
if ("`gstyle'" != "") .graphstyle.setstyle , style(`gstyle')
.swap_scaleaxis.setstyle , style(`.graphstyle.swap_scaleaxis.stylename')
.swap_groupaxis.setstyle , style(`.graphstyle.swap_groupaxis.stylename')
.reverse_scale.setstyle , style(`.graphstyle.reverse_scale.stylename')
.barlabel_method.setstyle, style(`.graphstyle.barlabel_method.snm')
.barlabel_pos.setstyle , style(`.graphstyle.barlabel_pos.stylename')
.custom_whiskers.setstyle, style(`.graphstyle.custom_whiskers.snm')
.median_type.setstyle , style(`.graphstyle.median_type.stylename')
if "`.style'" != "" {
.graphstyle.setstyle, `style'
}
if "`stack'" != "" {
if `.boxplot.istrue' {
di as error "option stack not allowed"
exit 198
}
.stack.set_true
}
if "`percentages'" != "" {
if `.boxplot.istrue' {
di as error "option percentages not allowed"
exit 198
}
.percentages.set_true
}
if "`exclude0'" != "" {
if `.boxplot.istrue' {
di as error "option exclude0 not allowed"
exit 198
}
.include0.set_false
}
if "`yreverse'" != "" {
.reverse_scale.setstyle , style(yes)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -