创建并排条形图

时间:2015-09-24 16:47:02

标签: plot stata

我想基于频率创建并排条形图。在Side-by-side bars in bar plot找到了一个R例子。这可以在Stata中找到吗?

假设我有这些数据:

lock(&sched.lock)

我可以使用用户编写的命令clear set obs 1000 set seed 1 gen var1 = runiform()*10 set seed 2 gen var2 = runiform()*10 set seed 3 gen var3 = runiform()*10 gen var1_value = "A" if var1<3 replace var1_value = "B" if var1>=3 & var1<=9 replace var1_value = "C" if var1>9 gen var2_value = "A" if var2<4 replace var2_value = "B" if var2>=4 & var2<=6 replace var2_value = "C" if var2>6 gen var3_value = "A" if var3<1 replace var3_value = "B" if var3>=1 & var3<=3 replace var3_value = "C" if var3>3 tabplot来单独绘制这些命令:

catplot

然而,我希望这三个在同一个情节中,并列价值。如果可能的话,我希望每个值并排放置许多图,而不是本例中的3个。

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。最简单的可能是简单的数据重组。按照您的示例,我假设先前为ssc inst catplot。为不同颜色的razzmatazz添加asyvars等选项。

clear
set obs 1000
set seed 1
gen var1 = runiform()*10
set seed 2
gen var2 = runiform()*10
set seed 3
gen var3 = runiform()*10

gen var1_value = "A" if var1<3
replace var1_value = "B" if var1>=3 & var1<=9
replace var1_value = "C" if var1>9

gen var2_value = "A" if var2<4
replace var2_value = "B" if var2>=4 & var2<=6
replace var2_value = "C" if var2>6 

gen var3_value = "A" if var3<1
replace var3_value = "B" if var3>=1 & var3<=3
replace var3_value = "C" if var3>3

forval j = 1/3 { 
    clonevar value`j' = var`j'_value 
}

gen id = _n 
reshape long value, i(id) j(which) 
catplot which value, asyvars 
catplot value which, asyvars