Hi folks,

Apologies if this question is simple but I was trying to find a good solution.

I want to create a box plot of a number of mg_x variables by (or 'over') sex. The over command is OK for grouping by sex, but separate male/female on left and right side and I ideally want the variables (e.g. mg_1 mg_2) side by side for sex and colour coded (e.g. white or grey) for men vs. women (see Figure).

I can get close with my final section of code below (i.e. //SEX - SIDE BY SIDE - also see Figure attached) - but what I really want is something like that using 'over' - so that male and female are side by side (e.g. white or grey colour for male or female) per each mg_x variable? At the moment multicoloured across all variables and genders.

Just wondered if anyone knew a good solution to this without having to recolour each box? Example data attached.

Thanks,
Patrick


Code:
*
Example generated by -dataex-. To install: ssc install dataex
clear
input float(sex ACM mg_1 mg_2 mg_3 mg_4 mg_5 mg_6 mg_7 mg_8 mg_9 mg_10 mg_15 mg_20 mg_30 mg_50 mg_60 mg_70 mg_80 mg_100)
1 0 .786 .628 .513 .432 .377  .34 .314 .296 .283 .272 .231 .201 .156 .087 .066 .053 .044 .031
1 0 .843 .734 .645 .573 .523 .482 .451 .428  .41 .394 .337 .291 .223 .136 .107 .085 .068 .045
0 0 .811 .702 .618 .546 .486 .449 .426 .406 .395 .387 .359 .335 .291 .217 .187 .159 .135 .096
1 0 .744 .578 .461  .37 .314 .277 .253 .236 .225 .216 .183 .159 .114 .055 .041 .033 .027  .02
1 0   .8 .676 .582 .505 .453 .418 .398 .384 .375 .368 .341 .319 .269 .187 .161 .138 .118 .086
0 0 .863 .789 .715  .66 .617 .578  .54 .505 .478 .454 .353 .281 .196 .107  .08  .06 .046 .029
1 0 .858 .753 .666   .6 .553 .519 .492 .469 .449 .432 .372 .333 .278 .206 .181  .16 .142 .112
0 0 .806 .683 .589 .514 .464  .43  .41 .396 .385 .376 .344 .317 .272 .199 .169 .143 .119 .082
0 0 .819 .705 .617 .556 .512 .478 .456 .438 .425 .411 .363 .326 .265 .188  .16 .135 .114 .081
1 0 .792  .71 .637 .587 .545 .512 .481 .455 .437 .421 .368 .323 .259 .176 .146 .121   .1 .066
0 0 .825 .708 .618 .548 .492 .454 .423 .402 .387 .376 .335 .304 .255 .189 .165 .142 .122 .089
1 0 .791 .671 .571 .488 .426 .379 .342 .318 .299 .286  .25 .224 .179 .121 .104  .09 .079 .062
0 0 .809 .706 .633 .579 .532 .497 .472 .453  .44 .429 .384 .337 .254 .178 .153 .131 .112  .08
1 0 .802 .695 .628 .576  .54 .512 .492 .474  .46 .449 .417 .391 .343 .253 .215 .181 .152 .105
0 0 .846 .745 .669 .616  .57  .53 .498  .47 .448 .428 .374  .34 .286 .203 .174  .15 .129 .095
1 0 .741 .629  .55 .491  .44 .395 .363 .341  .32 .304 .242  .21 .171  .13 .117 .104 .092 .072
1 0 .857 .772 .702 .648 .603 .573 .549 .528 .509 .493  .43 .383 .305 .195 .154 .121 .095  .06
1 0 .755 .622 .534 .462 .414  .38 .359 .348 .339 .332 .305 .286 .252 .196 .173 .152 .133 .102
1 0 .814 .707 .636  .57 .515 .478 .457 .441  .43 .421 .389 .361  .31  .23   .2 .174 .152 .115
1 0 .807 .671 .557 .464 .396 .359 .333 .314 .297 .283  .23  .19 .132 .068 .049 .036 .026 .014
0 0 .766 .621 .517 .442 .391 .361  .34 .327 .317 .308  .28 .259 .226 .163 .133 .107 .085 .055
1 0 .832 .733  .65  .59 .544  .51 .485 .464 .449 .435 .385 .348 .294 .227 .203  .18 .161 .127
0 0 .792 .684 .612 .559 .516 .482 .456 .438 .426 .412 .377 .352 .301 .216 .181 .149 .121 .078
0 0 .809  .71 .612 .545 .492 .451 .419 .395 .378 .364 .321  .29  .24 .162 .131 .105 .084 .054
1 0 .832 .713 .619 .555 .508 .473 .454  .44 .429  .42 .379 .343 .277 .176 .139 .109 .085 .053
1 0 .791  .66 .556 .481 .429  .39 .362 .343 .329 .319 .278 .249 .199 .132 .111 .093 .079 .059
0 0 .846 .729 .637 .571 .531 .504 .481 .463  .45 .439 .395 .362 .309 .237 .209 .184 .162 .127
0 0 .832 .743 .677 .624 .584 .551 .526  .51 .495 .483  .44 .407  .35 .268 .236 .209 .184 .143
0 0 .823 .738 .663   .6 .545   .5 .471 .449 .433  .42 .371 .334 .285 .224   .2 .179  .16  .13
1 0 .837 .738 .664 .614  .57 .532 .506 .487 .471 .459 .415 .381 .325 .248 .222 .201 .182  .15
1 0 .813 .691 .602 .537 .489 .455 .433 .421 .411 .402 .371 .346 .301 .231 .205 .182 .162 .128
1 0 .793 .672 .583 .513  .46 .429 .404 .387 .373  .36 .312 .266 .197 .132 .111 .095 .081  .06
0 0 .831 .733 .658 .599 .552 .518 .496 .478 .464  .45   .4 .356 .279 .177 .144 .118 .098 .069
0 0 .815 .698 .608 .534 .474 .425 .396  .38 .369  .36 .332 .307 .261 .193 .169 .151 .135 .109
1 0 .825 .703 .603 .526  .47 .426 .393 .367 .346 .331 .281 .242 .188 .112 .086 .065 .049 .028
1 0 .711 .531 .404 .329 .274 .238 .214 .197 .186 .177  .15 .132   .1 .057 .045 .035 .028 .019
1 0 .782 .635 .523  .45 .398 .368 .349 .332 .318 .305 .263  .23 .176 .117 .097 .081 .068 .048
1 0 .844 .717  .62 .557 .511  .48  .46 .444 .433 .426 .402  .38 .334 .244 .201 .161 .125 .071
0 0 .799 .678 .587 .525 .482 .452  .43 .416 .405 .395 .364 .336 .281 .199 .168  .14 .118 .082
0 0  .83 .723 .641 .576 .526 .493 .471  .45 .436 .425 .373 .325 .251 .152 .118 .091 .071 .047
1 0 .808 .681 .594 .526 .479  .44 .417   .4 .387 .376 .341 .313  .26 .196 .171 .149 .129 .097
0 0 .829 .706 .623 .565 .521 .489 .465 .449 .436 .425 .383 .348  .29 .211 .184 .162 .142 .109
0 0 .695 .616 .551 .497 .459 .429 .407 .386 .368 .354 .312 .281 .232 .161 .137 .118 .101 .076
0 0   .8 .685 .606 .542 .498 .466 .444 .427 .411 .398 .352 .316  .26 .183 .153 .126 .103 .068
1 0 .845  .73  .65 .597 .559  .53 .509 .491 .476 .464 .423 .393 .344 .272 .245  .22 .198 .156
0 0 .765 .692 .632 .585  .54   .5 .464  .43 .405 .385 .329 .295 .247 .176 .148 .125 .106 .076
0 0 .806 .684 .588 .519 .469 .428 .405 .392 .382 .373 .331 .289 .221 .144 .121 .102 .086 .061
1 0 .809 .718  .64 .578  .52 .478 .445 .423 .406 .391 .337 .289  .21 .146 .128 .113 .099 .077
0 0 .803 .671 .577 .511 .476 .453 .438 .428 .419 .412 .381 .347 .287 .203 .169  .14 .119 .086
end
label values sex sex
label def sex 0 "Female", modify
label def sex 1 "Male", modify

    
//ALL 
graph box mg_1-mg_10 mg_15 mg_20 mg_30 mg_50 mg_60 mg_70 mg_80 mg_100, ///  
    nooutsides ytitle("Fraction of time spent at acceleration >= mg_x") note("") leg(off) scheme(s1mono) graphregion(fc(white) ifc(white) lc(white) ilc(white)) ///
    showyvars yvar(label(labsize(small) angle(vertical))) 

    // BY SEX
graph box mg_1-mg_10 mg_15 mg_20 mg_30 mg_50 mg_60 mg_70 mg_80 mg_100, ///  
    over(sex) ///
    nooutsides ytitle("Fraction of time spent at acceleration >= mg_x") note("") leg(off) scheme(s1mono) graphregion(fc(white) ifc(white) lc(white) ilc(white)) ///
    showyvars yvar(label(labsize(small) angle(vertical)))

//SEX - SIDE BY SIDE
foreach var of varlist mg_1-mg_10 mg_15 mg_20 mg_30 mg_50 mg_60 mg_70 mg_80 mg_100{
gen `var'_M = `var' if sex ==1
gen `var'_F = `var' if sex ==0
}

graph box mg_1_M-mg_100_F, /// 
    nooutsides ytitle("Fraction of time spent at acceleration >= mg_x") note("") leg(off) scheme(s1mono) graphregion(fc(white) ifc(white) lc(white) ilc(white)) ///
    showyvars yvar(label(labsize(vsmall) angle(vertical)))

e.g. - can I get something similar to the below, but with men and women side by side and colour coded as pairs of white vs black per mg_x, as per 'over' command. Rather than having to create new mg_x_M and mg_x_F variables.


Array