在Stata中的图表中保持一致的barplot颜色 [英] consistent barplot colors across graphs in Stata

查看:471
本文介绍了在Stata中的图表中保持一致的barplot颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Stata输出堆积条形图,每个堆叠栏从底部开始排序 - >向上:最大 - >最小 - 每个团队赢得%。

 清除
set obs 10
gen team =yankeesif inlist(_n,1,6 )
如果inlist(_n,2,7)
替换team =red sox如果inlist(_n,3,8)替换team =mets
替换team =nationals如果inlist(_n,4,9)
如果inlist(_n,5,10)
gen wins = -10 + 20 * _n
替换team =astros 11 - _n]在6/10
gen year = cond(_n <= 5,2013,2014)
gen season =regularin 1/10

set obs 16
如果inlist(_n,11,14)替换team =yankees
如果inlist(_n,12,15)替换team =red sox
替换team =astros if inlist(_n,13)
如果inlist(_n,16)
替换team =mets11/16
替换wins = -10 + 30 *(_n-10)在14/16
替换year = 2013
在14/16
替换year = 2014取代wins = wins [17 - _n]
替换season =playoffs在11 / 16

在常规季后赛中x foreach {
保存
保留季节==`x'
#delimit;
图形条(平均值)胜过,(团队,排序(1)降序)over(year,label(ticks labs(small)))asyvars stack
ytitle(Wins(%))
title(赢得x的百分比)
blabel(bar,position(center)format(%9.0f)size(2.5)color(white))
legend(size(2 (*。45)posgap(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)#th yvar bar



例如:
< code $>图形条yvar1 yvar2,bar(1,颜色(绿色))条(2,颜色(红色))

我在寻找

图表栏yvar1 yvar2,bar(team ==Yankees,color(blue))bar(team == Red Sox,颜色(红色))



http://www.stata.com/statalist/archive/2011-03/msg00097.html 提供指导,但不是上述结果。

解决方案

这只是部分答案,如果别人或我可以进一步采取补充。



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

 清除
set obs 10
gen team =yankeesif inlist(_n,1,6)
如果inlist(_n,2)替换team =red sox ,7)如果inlist(_n,3,8)
替换team =mets,如果inlist(_n,4,9)替换team =nationals
替换team =astros if inlist(_n,5,10)
gen wins = -10 + 20 * _n
在6/10
gen year = cond(_n替换wins = wins [11 - _n] < = 5,2013,2014)

#delimit;
图形条(平均值)胜过,(团队,排序(1)降序)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

我的想法是将每个团队分成一个变量: (团队)veryshortlabel

然后允许这种图形:

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



这可能是更复杂问题的更好基础。 (我不确定是否期望我们能理解棒球奥古斯塔,事实上,这是棒球吗?这些细节并没有得到普遍的理解)。我自己的观点是堆叠的酒吧会使图表变差,但这是一个不同的问题。

I am outputting stacked bar plots in Stata, with each stacked bar ordered from bottom -> up : 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

The issue is that team colors vary across graphs, because not all regular season teams are present in the playoffs, and color is assigned alphabetically. For example, the red sox are yellow in graph 1, but green in graph 2.

From Stata's help menu, the only modification appears to be by bar #: bar(#, barlook_options) look of #th yvar bar

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

I'm looking for

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, but not the outcome described above.

解决方案

This is a partial answer only, to be supplemented if someone else or I can take it further.

Taking your sandbox example, and rewriting the code in ways not important for your 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 was to separate into a variable for each team:

separate wins, by(team) veryshortlabel

That then allows this kind of graph:

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

This might be a better foundation for your more complicated problems. (I am not sure whether we are expected to understand the baseball arcana. Indeed, is this baseball? These details are not universally understood.)

My own view is that stacking of bars would make the graph worse, but that is a different question.

这篇关于在Stata中的图表中保持一致的barplot颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆