consistent barplot colors across graphs in Stata -


i outputting stacked bar plots in stata, each stacked bar ordered bottom -> : largest -> smallest wins % per team.

clear set obs 10 gen team = "yankees" if inlist(_n, 1, 6)  replace team = "red sox" if inlist(_n, 2, 7)  replace team = "mets" if inlist(_n, 3, 8)  replace team = "nationals" if inlist(_n, 4, 9)  replace team = "astros" if inlist(_n, 5, 10)  gen wins = -10 + 20 * _n  replace wins = wins[11 - _n] in 6/10  gen year = cond(_n <= 5, 2013, 2014)  gen season = "regular" in 1/10  set obs 16 replace team = "yankees" if inlist(_n, 11, 14) replace team = "red sox" if inlist(_n, 12, 15) replace team = "astros" if inlist(_n, 13)  replace team = "mets" if inlist(_n, 16)  replace wins = -10 + 30 * (_n-10) in 11/16 replace wins = wins[17 - _n] in 14/16  replace year = 2013 in 11/13 replace year = 2014 in 14/16 replace season = "playoffs" in 11/16  foreach x in "regular" "playoffs"{    preserve    keep if season == "`x'"    #delimit ;    graph bar (mean) wins, over(team, sort(1) descending) over(year, label(ticks labs(small))) asyvars stack       ytitle("wins (%)")      title("wins percentages in `x'")      blabel(bar, position(center) format(%9.0f) size(2.5) color(white))      legend(size(2) rowgap(*.45) pos(6) rows (2) region(style(legend) fcolor(gs15) margin(medsmall)) colgap(*.75) symxsize(*.75) keygap(*.33));     #delimit cr    restore 

enter image description here enter image description here

the issue team colors vary across graphs, because not regular season teams present in playoffs, , color assigned alphabetically. example, red sox yellow in graph 1, green in graph 2.

from stata's menu, modification appears bar #: bar(#, barlook_options) of #th yvar bar

for example: graph bar yvar1 yvar2, bar(1,color(green)) bar(2,color(red))

i'm looking

graph bar yvar1 yvar2, bar(team=="yankees",color(blue)) bar(team=="red sox",color(red))

http://www.stata.com/statalist/archive/2011-03/msg00097.html offers guidance, not outcome described above.

this partial answer only, supplemented if else or can take further.

taking sandbox example, , rewriting code in ways not important question,

clear set obs 10 gen team = "yankees" if inlist(_n, 1, 6)  replace team = "red sox" if inlist(_n, 2, 7)  replace team = "mets" if inlist(_n, 3, 8)  replace team = "nationals" if inlist(_n, 4, 9)  replace team = "astros" if inlist(_n, 5, 10)  gen wins = -10 + 20 * _n  replace wins = wins[11 - _n] in 6/10  gen year = cond(_n <= 5, 2013, 2014)   #delimit ; graph bar (mean) wins, over(team, sort(1) descending) over(year, label(ticks labs(small))) asyvars stack    ytitle("wins (%)")   title("wins")   blabel(bar, position(center) format(%9.0f) size(2.5) color(white))   legend(size(2) rowgap(*.45) pos(6) rows (2) region(style(legend) fcolor(gs15) margin(medsmall)) colgap(*.75) symxsize(*.75) keygap(*.33)); #delimit cr 

my idea separate variable each team:

separate wins, by(team) veryshortlabel 

that allows kind of graph:

graph hbar (asis) wins? , over(team) over(year) nofill legend(off) 

enter image description here

this might better foundation more complicated problems. (i not sure whether expected understand baseball arcana. indeed, baseball? these details not universally understood.)

my own view stacking of bars make graph worse, different question.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -