I want to graph tobacco use for couples in different value groups. While the values variable has 12 categories, I only want to graph the first five, and I use -inrange()- to achieve that:
Code:
graph hbar lstbcn1 lstbcn2 if inrange(values, 1, 5) , nooutsides  ///
bar(1, bfcolor(navy)) bar(2, bfcolor(maroon)) over(values, label(labsize(small))  ///
relabel(1 "G1" 2 "G2" 3 "G3" 4 "G4" 5 "G5")) /// 
ti("", size(small)) ylabel(, labsize(small)) ytick(25(50)175, grid) ///      
legend(region(lstyle(none)) order(1 "male" 2 "female") /// 
rowgap(.1) colgap(1) size(small) color(none) region(fcolor(none))) name(c1, replace) ///  
graphregion(margin(medthin) color(white) icolor(white)) plotregion(style(none) color(white) icolor(white))
Array
However, when I add the line of code with -by()- (below), the restriction imposed by -inrange(values, 1, 5) no longer holds and the labels for all the categories appear in the graph. How can I limit this to the first five categories (G1 to G5) as shown in the first figure?
Code:
by(at3, note("") graphregion(color(white))) ///
Note: "at3" is a dummy variable with values 0/1. (As an aside, how can I relabel the variable in -by()- as done with -over()-?).
Array
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int(lstbcn1 lstbcn2) byte(values at3)
 50  60 1 0
100  50 2 0
140 200 3 0
  8  25 4 1
 20   4 5 1
 10  60 2 1
 50 110 7 0
 75 100 1 0
 50  35 2 0
  8  25 3 1
 20   4 5 1
125 100 3 0
 80  25 4 0
140  60 5 0
 40  20 5 1
 60  40 6 0
  6   2 5 1
  8  25 3 1
 20   4 2 1
100 100 1 0
  7   5 2 1
 35   2 3 1
  7   5 4 1
 35   2 4 1
 60  30 2 1
  6   2 5 1
100 100 5 0
  7   5 5 1
 35   2 5 1
150 100 6 0
140  28 7 0
 15  10 6 1
 14   8 7 1
 30 160 7 1
 70  70 4 0
 50  10 4 0
 25  35 3 1
 60  30 3 0
250 140 2 0
100 140 2 0
 50  80 2 1
end
Stata v.15.1. Using panel data.