簇状堆叠条形图

问题描述 投票:0回答:1

我终于成功地在 Stata 中创建了一个簇状堆叠条形图(经过大量的试验和错误)。这是我使用的代码和输出:

clear
 
input time_period scenario s str2 cid 
3 1 4.013453 "fw" 
4 1 4.064307 "fw" 
5 1 4.370211 "fw" 
3 1 19.20553 "he" 
4 1 22.62123 "he" 
5 1 25.16719 "he" 
3 2 6.894791 "fw" 
4 2 6.960844 "fw" 
5 2 9.851804 "fw" 
3 2 14.96675 "he" 
4 2 18.20208 "he" 
5 2 17.93641 "he" 
end
 

gr bar (asis) s, over(cid) over(scenario, label(nolabel) gap(0))
over(time_period) stack ytitle("Total wealth")
graphregion(color(white)) legend(rows(1) cols(2) order(1 "FW" 2 "HE")
ring(1) position(6)) b1title("Period") bar(1, fcolor("0 114 189"))
bar(2, fcolor("217 83 25")) plotregion(lcolor(black))

enter image description here

现在有什么方法可以使左侧的条形与右侧的条形颜色不同吗? (最好稍微透明一些)

graph stata
1个回答
0
投票

您实际上想要四种不同的颜色。我取得了一些进步。我无法让它与你自己的颜色一起使用。

clear

input time_period scenario s str2 cid 
3 1 4.013453 "fw" 
4 1 4.064307 "fw" 
5 1 4.370211 "fw" 
3 1 19.20553 "he" 
4 1 22.62123 "he" 
5 1 25.16719 "he" 
3 2 6.894791 "fw" 
4 2 6.960844 "fw" 
5 2 9.851804 "fw" 
3 2 14.96675 "he" 
4 2 18.20208 "he" 
5 2 17.93641 "he" 
end


gr bar (asis) s, over(cid) over(scenario, label(nolabel) gap(0))   ///
over(time_period) stack ytitle("Total wealth")                     ///
graphregion(color(white)) legend(rows(1) cols(2) order(1 "FW" 2 "HE") ///
ring(1) position(6)) b1title("Period") bar(1, fcolor("0 114 189")) /// 
bar(2, fcolor("217 83 25")) plotregion(lcolor(black)) name(G1, replace)

reshape wide s, i(time_period scenario) j(cid) string 
gen timeL = time_period - 0.15 
gen timeR = time_period + 0.15 

gen S = sfw + she 

local colour1 `" "0 114 189" "'
local colour2 `" "217 83 25" "' 
local colour1 red 
local colour2 blue 

local lc lc(black)

twoway bar sfw timeL if scenario == 1, barw(0.3) base(0) fcolor(`colour1'%50) `lc' ///
|| rbar sfw S timeL if scenario == 1, barw(0.3) fcolor(`colour2'%50) `lc' ///
|| bar sfw timeR if scenario == 2, barw(0.3) base(0) fcolor(`colour1') `lc'  ///
|| rbar sfw S timeR if scenario == 2, barw(0.3) fcolor(`colour2') `lc' /// 
ytitle(Total wealth) yla(0(10)30) xla(3/5, noticks) xtitle(Period) /// 
legend(order(- "Scenario 1:" 2 "HE" 1 "FW" - "  Scenario 2:" 4 "HE" 3 "FW" ) pos(6) row(1)) /// 
name(G2, replace)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.