SAS 中按组和子组排列的堆积条形图

Stacked bar chart by group and subgroup in SAS

我无法在 sas9.4 中按组和子组创建堆叠图表,我想要类似于 excel 图表的图表。请在下面找到示例数据和 excel 图表(第一张图片)以及 SAS 图表(第二张图片)。

我无法在同一轴(年)上设置 SEGMENT 'ACTUAL' AND 'FORECAST' 的共同年份。实际是指2014年之前的数据,预测是2014年之后的数据,两者应该在同一个轴上。

goptions reset=all ;
goptions colors=(red blue green);
legend1 label=none ;

proc gchart data=NEW;
vbar year/ discrete type=sum sumvar=VALUE 
  group= segment subgroup=WKSCOPE      ;
  where year le 2020 AND YEAR ge 2012;
run;

我会用注释来解决这个问题。我比 GCHART 更了解 SGPLOT 所以我会这样回答。

data have;
  input segment $ year wkscope $ value;
  datalines;
ACTUAL 2012 PH 5
ACTUAL 2012 PH 1
ACTUAL 2012 BHS 1
ACTUAL 2012 RES 2
ACTUAL 2013 PH 2
ACTUAL 2013 PH 5
ACTUAL 2013 BHS 1
ACTUAL 2014 RES 2
FORECAST 2015 PH 3
FORECAST 2015 BHS 0
FORECAST 2016 PH 4
FORECAST 2016 RES 1
FORECAST 2017 PH 5
FORECAST 2017 BHS 1
FORECAST 2017 RES 2
;;;;
run;

data sgannods;
  x1space='wallpercent';
  y1space='wallpercent';
  x1=75;
  y1=-10;
  label="Forecast";
  function='text';
  output;
  x1=25;
  label="Actual";
  output;
run;

proc sgplot data=have sganno=sgannods;
    vbar year/response=value group=wkscope groupdisplay=stack;
run;

基本上,做除分段之外的所有操作,然后使用该值进行注释。您可以像我一样手动生成它,或者(最好)从原始数据生成它(如果它可以更改)。我使用 WALLPERCENT 因为上半场是实际的,下半场是预测的,但如果它可以改变(2 实际 4 预测)那么你不应该那样做;您应该使用 WALLPERCENT 并从数据中计算出正确的位置(可能使用 proc freq)或使用 DATAVALUE 并将其放在中间值之下。

如果这还不够近,我会去 robslink.com, which has a nice set of examples (and is written by one of the developers of the GCHART set of procs). Sanjay also has a blog, Graphically Speaking which has some great examples, and both post on SAS Communities

我制作的图像如下。它在其他方面并不是特别接近,但所有这些都很容易修复(配色方案、大小、图例位置)。

数据标签是您无法真正拥有的一件事;如果您使用 VBARPARM,它们是可添加的,但这需要提前汇总数据。 Sanjay 在他的一篇关于 9.4M2 的 blog posts 中对此进行了介绍(如果您有 M2 维护版本);我也在我的 MWSUG 论文中介绍了这一点,轻松标记:如何使用 SGPLOT 和 GTL Without Annotate 如果你有旧版本。