1
我製作了許多圖表,比較了兩個組(例如,男性/女性)跨多個變量的圖表。標準-graph條形輸出將所有男士條形碼組合在一起,並將所有條形碼女士組合在一起。我希望找到一種簡單的方法來創建條形圖,這些條形圖首先通過目標變量(即變量繪製圖形)對組進行分組,然後通過-over-變量(例如性別)進行分組。按目標變量排序圖形條,然後是排序變量
我有一個這樣做的方法,但它非常繁瑣。請參閱下圖。
*Set seed + obs
clear
set seed 442
set obs 100
*Generate two outcomes
gen x1 = uniform()
gen x2 = uniform()
*Generate crossing variable
gen gender = 0 in 1/50
replace gender = 1 in 51/100
label define gender_lab 0 "Male" 1 "Female"
label values gender gender_lab
*Extract means by gender
gen b_male = .
gen b_female = .
sum x1 if gender == 0
replace b_male = r(mean) in 1
sum x1 if gender == 1
replace b_female = r(mean) in 1
sum x2 if gender == 0
replace b_male = r(mean) in 2
sum x2 if gender == 1
replace b_female = r(mean) in 2
*Establish order of graph
gen index_male = _n*3 in 1/2
gen index_female = (_n*3) + 1 in 1/2
*This is what -graph bar- produces naturally
graph bar x1 x2, over(gender)
*This is closer to what I want
twoway bar b_male index_male || bar b_female index_female, xlabel(3.5 "x1" 6.5 "x2", notick labgap(4)) xmlabel(3 "Male" 4 "Female" 6 "Male" 7 "Female") legend(off)
是否有使用graph bar
但仍建立我想要的排序順序的簡單方法?我有時每天都會生成數十個這樣的圖表,所以我想盡可能地避免不必要的步驟。
我聽說過-statplot-的一次,但是忘記了它。我很高興能夠被提醒,並用完美的插圖來解決我的問題。謝謝! – Amberopolis
我也有時忘了它,而我是它的第二作者。 –