Hello! Hope you can help me. When creating stacked bars - and sort it - the labels somehow get messed up for one group. In my case the treatment-group get the same labels as my control-group.
Any ideas what is going on?

Code:
clear
input float label_id byte date float treatment double var1
 4 2 1 1
 4 3 1 1
 5 2 1 1
 6 2 1 1
 8 2 1 1
13 2 1 1
16 2 1 1
19 2 0 1
19 4 0 1
23 2 1 1
23 4 1 1
24 2 0 1
24 4 0 1
25 2 1 1
26 2 0 1
27 2 1 1
28 2 0 1
31 2 0 1
36 3 0 1
40 2 0 1
42 2 0 1
47 2 0 1
47 3 0 1
49 2 1 1
53 2 0 1
54 2 0 1
55 3 0 1
57 3 0 1
62 3 0 1
end
label values date date
label def date 2 "2. date", modify
label def date 3 "3. date", modify
label def date 4 "4. date", modify
label values treatment asg
label def asg 0 "no treatment", modify
label def asg 1 "treatment", modify
Code:
bysort label_id: gen N = _N
gsort label_id, gen(bargroup)  
gsort treatment -N label_id, gen(ordergroup)
sort ordergroup

// define color-group for multiple observations
qui levelsof bargroup, l(bargroup)
qui levelsof bargroup if N >1, l(bargroup2)
local bargroup2 = subinstr("`bargroup2'"," ",",",.)        // replace "space" with "," for inlist()

// create local with barlook-option for each group/bar
local barcolors ""
foreach x of local bargroup {
    if !inlist(`x',`bargroup2') local barcolors "`barcolors' bar(`x',col(gray)) "
    if inlist(`x',`bargroup2') local barcolors "`barcolors' bar(`x',col(red)) "
}

// LEFT GRAPH: with sorting: produces wrong labels for treatment-group.
graph bar (sum) var1 , over(label_id, sort(ordergroup)) over(treatment) ///
    by(date, row(1) title("") note("") legend(off)) stack ///
    title("") ytitle("") subtitle(,s(small)) ylab(0(2)10) asyvars ///
    blabel(name, pos(center) c(black)) ///
    `barcolors'

// RIGHT GRAPH: correct labels for treatment, but sorting is missing
graph bar (sum) var1 , over(label_id) over(treatment) ///
    by(date, row(1) title("") note("") legend(off)) stack ///
    title("") ytitle("") subtitle(,s(small)) ylab(0(2)10) asyvars ///
    blabel(name, pos(center) c(black)) ///
    `barcolors'