【问题标题】:consistent barplot colors across graphs in StataStata中图表的条形图颜色一致
【发布时间】:2015-06-04 20:04:38
【问题描述】:

我在 Stata 中输出堆叠条形图,每个堆叠条从底部 -> 向上排序:最大 -> 每个团队的最小获胜百分比。

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

问题在于,各个图表中的球队颜色各不相同,因为并非所有常规赛球队都出现在季后赛中,并且颜色是按字母顺序分配的。例如,图 1 中的红袜为黄色,而图 2 中为绿色。

从 Stata 的帮助菜单中,唯一的修改似乎是条形 #: bar(#, barlook_options) look of #th yvar bar

例如: graph bar yvar1 yvar2, bar(1,color(green)) bar(2,color(red))

我在找

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 提供指导,但不提供上述结果。

【问题讨论】:

  • 重组,让每个团队都是一个单独的变量,然后您可以将条形与变量关联起来。
  • 为了获得更好的答案,请举一个可重现的问题示例。我们无法使用您的数据。
  • 我已经修改为包含示例数据。 (想象这是一个循环的一部分,因为有另一个变量将白天与夜间比赛、双头比赛等分成不同的类别,并且每个类别都有自己的图表,看看如果大都会不玩双头比赛并被排除在外从那个单独的图表中,字母表中在他们之后的每个团队都会有不同的颜色,)
  • 我再次修改以显示循环

标签: graph stata


【解决方案1】:

这只是部分答案,如果其他人或我可以进一步补充,请予以补充。

以您的沙盒为例,并以对您的问题不重要的方式重写代码,

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

我的想法是为每个团队分成一个变量:

separate wins, by(team) veryshortlabel

然后允许这种图:

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

这可能是您解决更复杂问题的更好基础。 (我不确定我们是否应该了解棒球奥术。确实,这是棒球吗?这些细节并没有被普遍理解。)

我自己的观点是,堆叠条形会使图表变得更糟,但这是一个不同的问题。

【讨论】:

  • 尼克,我再次修改以在数据中包含循环。一般来说,我同意堆叠条不是最好的,尽管对于我拥有的更复杂的数据(我试图用这个人为的棒球数据来简化)我相信堆叠条是最好的。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
相关资源
最近更新 更多