You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Stata聚类堆叠条形图的左右侧条柱设置差异化颜色(含透明度)?

在Stata中为聚类堆叠条形图的特定组设置颜色与透明度

你可以通过在gr bar的bar()选项中添加条件判断,为scenario=1(左侧条柱组)单独设置带透明度的颜色,具体修改如下:

修改后的完整代码

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") ///
// 左侧scenario=1的FW条柱:带透明度的蓝色
bar(1, if(scenario==1) fcolor("0 114 189%80")) ///
// 右侧scenario=2的FW条柱:原不透明蓝色
bar(1, if(scenario==2) fcolor("0 114 189")) ///
// 左侧scenario=1的HE条柱:带透明度的红色
bar(2, if(scenario==1) fcolor("217 83 25%80")) ///
// 右侧scenario=2的HE条柱:原不透明红色
bar(2, if(scenario==2) fcolor("217 83 25")) ///
plotregion(lcolor(black))

核心修改点

  • 利用bar(#, if(scenario==X))的条件语法,区分左侧(scenario=1)和右侧(scenario=2)的条柱组。
  • 透明度通过颜色代码后的%N控制:%80表示80%不透明度(数值越小越透明,范围0-100),可按需调整。
  • 右侧组保留原代码的不透明颜色,仅对左侧组添加透明效果,保证类别颜色的一致性。

内容的提问来源于stack exchange,提问作者phdstudent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 00:44:52