Graph 按分类变量从组合条形图中删除图例

Graph 按分类变量从组合条形图中删除图例,graph,stata,Graph,Stata,我正在尝试使用graph combine组合两个条形图。两个图都有相同的图例,所以我希望有一个图例消失 但由于条形图本身是一种组合图,legend(off)将无法工作 以下是如何生成这两个图: graph bar (mean)over_econ over_lab over_cul over_coh over_fut, ytitle(Frequency) xsize(6.5) by(respondent) graph bar (mean)over_econ over_lab over_cul o

我正在尝试使用
graph combine
组合两个条形图。两个图都有相同的图例,所以我希望有一个图例消失

但由于条形图本身是一种组合图,
legend(off)
将无法工作

以下是如何生成这两个图:

graph bar (mean)over_econ over_lab over_cul over_coh over_fut, ytitle(Frequency) xsize(6.5) by(respondent)

graph bar (mean)over_econ over_lab over_cul over_coh over_fut, ytitle(Frequency) xsize(6.5) by(city_problem)
其中
受访者
城市问题
指分类变量。图例由Stata自动生成


任何一种情况下如何删除图例的建议?

grc1leg
适用于需要为组合图添加一个图例的情况

首先安装它

net install grc1leg,from( http://www.stata.com/users/vwiggins/)

代码示例:

net install grc1leg,from( http://www.stata.com/users/vwiggins/)

clear   all


input quest str25 q      a1 a2 a3 a4 a5 a6
1 "Question 1"           0  2  37 45 12 4
1 "Benchmark Q1"         2  5  25 47 17 4
2 "Question 2"           1  37  2 40 17 3
2 "Benchmark Q2"         2  5  25 47  4 17
3 "Question 3"           1  2  40 37 17 3
3 "Benchmark Q3"         2  5  25 47 17 4
4 "Question 4"           1  2  37  17 3 40
4 "Benchmark Q4"         2  5  47 25 17 4
end 

graph hbar a1-a6 if quest==1, percent over(quest, gap(1)) over(q, gap(10))   ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
saving(a1, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)

graph hbar a1-a6 if quest==2, percent over(quest, gap(1)) over(q, gap(10))   ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)lcolor(black))  ///
saving(a1a, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)

graph hbar a1-a6 if quest==3, percent over(quest, gap(1)) over(q, gap(10))    ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)  lcolor(black)) ///
saving(a1b, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)


graph hbar a1-a6 if quest==4, percent over(quest, gap(1)) over(q, gap(20))    ///
stack legend(span rows(1) label(1 "Missing") label(2 "Never")                 ///
label(3 "Rarely") label(4 "Occasionly  ") label(5 "Mostly")                   ///
label(6 "Always    ") size(small))                                            ///
yline(20 40 60 80 ,lwidth(0.25 ) lcolor(black)) saving(a2, replace)


grc1leg a1.gph a1a.gph a1b.gph a2.gph, cols(1) ///
imargin(0 0 0 0) ycommon xcommon legendfrom(a2.gph)

exit

请参见

grc1leg
中的示例,该示例适用于需要为组合图创建一个图例的情况

首先安装它

net install grc1leg,from( http://www.stata.com/users/vwiggins/)

代码示例:

net install grc1leg,from( http://www.stata.com/users/vwiggins/)

clear   all


input quest str25 q      a1 a2 a3 a4 a5 a6
1 "Question 1"           0  2  37 45 12 4
1 "Benchmark Q1"         2  5  25 47 17 4
2 "Question 2"           1  37  2 40 17 3
2 "Benchmark Q2"         2  5  25 47  4 17
3 "Question 3"           1  2  40 37 17 3
3 "Benchmark Q3"         2  5  25 47 17 4
4 "Question 4"           1  2  37  17 3 40
4 "Benchmark Q4"         2  5  47 25 17 4
end 

graph hbar a1-a6 if quest==1, percent over(quest, gap(1)) over(q, gap(10))   ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
saving(a1, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)

graph hbar a1-a6 if quest==2, percent over(quest, gap(1)) over(q, gap(10))   ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)lcolor(black))  ///
saving(a1a, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)

graph hbar a1-a6 if quest==3, percent over(quest, gap(1)) over(q, gap(10))    ///
stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25)  lcolor(black)) ///
saving(a1b, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30)


graph hbar a1-a6 if quest==4, percent over(quest, gap(1)) over(q, gap(20))    ///
stack legend(span rows(1) label(1 "Missing") label(2 "Never")                 ///
label(3 "Rarely") label(4 "Occasionly  ") label(5 "Mostly")                   ///
label(6 "Always    ") size(small))                                            ///
yline(20 40 60 80 ,lwidth(0.25 ) lcolor(black)) saving(a2, replace)


grc1leg a1.gph a1a.gph a1b.gph a2.gph, cols(1) ///
imargin(0 0 0 0) ycommon xcommon legendfrom(a2.gph)

exit

参见

中的示例当我开始思考
by()
的作用时,我自己找到了答案。必须通过
legend(off)
by()
:中切换图例:


当我开始思考
by()
的作用时,我自己找到了答案。必须在
by()中通过
legend(off)
关闭图例: