0

我目前正在尝试在 proc sgplot 中为基于日期的数据创建一个并排的双轴条形图。我目前被困在最后一件事上,我无法使用 vbar 上的离散偏移选项移动条形图,因为我在 xaxis 上使用 Type=time。如果我对此发表评论,则条形会移动,但 xaxis 刻度值看起来很笨拙。所以我想知道是否有任何其他选项可以移动日期/时间数据的条形?以下是我的 SAS 代码。

data input;
input people visits outcome date date9.;
datalines;
41 448 210 1-Jan-18
43 499 207 1-Feb-18
45 544 221 1-Mar-18
49 564 239 1-Apr-18
39 575 236 1-May-18
37 549 210 1-Jun-18
51 602 263 1-Jul-18
32 586 208 1-Aug-18
52 557 225 1-Sep-18
41 534 227 1-Oct-18
48 499 217 1-Nov-18
44 514 235 1-Dec-18
31 582 281 1-Jan-19
33 545 269 1-Feb-19
38 574 259 1-Mar-19
29 564 247 1-Apr-19
29 642 274 1-May-19
28 556 216 1-Jun-19
20 531 187 1-Jul-19
31 604 226 1-Aug-19
19 513 186 1-Sep-19
24 483 185 1-Oct-19
28 401 156 1-Nov-19
18 450 158 1-Dec-19
21 418 178 1-Jan-20
28 396 149 1-Feb-20
43 488 177 1-Mar-20
33 539 205 1-Apr-20
57 631 244 1-May-20
54 695 291 1-Jun-20
58 732 309 1-Jul-20
62 681 301 1-Aug-20
42 654 291 1-Sep-20
57 749 365 1-Oct-20
60 627 249 1-Nov-20
56 623 244 1-Dec-20
54 712 298 1-Jan-21
62 655 262 1-Feb-21
;
run;

proc sgplot data=input;
  format date monyy7.;
  styleattrs datacolors=(Red DarkBlue) datacontrastcolors=(black black) datalinepatterns=(solid);
  vbar date / response=visits discreteoffset=-0.17 barwidth=0.3; 
  vbar date / response=outcome discreteoffset=0.17 barwidth=0.3;
  vline date / response=people y2axis lineattrs=(color=black thickness=3);
  xaxis display=(nolabel) /*fitpolicy=rotate valuesrotate=vertical*/ type=time /*interval=month*/;
  yaxis grid label='Label1' values=(0 to 800 by 100);
  y2axis label='Label2' values=(0 to 70 by 10);
  keylegend / title="";
run;

我得到的输出: 我得到的输出

我想要的输出:(有移位的条,但它正在改变日期) 我想要的输出

感谢任何帮助!谢谢你。

4

1 回答 1

1

重塑数据,transpose以便并排需要的变量成为分类变量,即名称值对。该名称可以vbar用作group=with groupdisplay=cluster

注意:似乎基于 vbar 变量的格式执行特殊检查,并且当该格式为 我从未在文档中讨论xaxis type=time过时,将呈现一个漂亮的两线轴标签。date9.

例子:

在绘图语句中使用name=,以便 keylegend 看起来更漂亮。

proc transpose data=input out=plot;
  by rowid date;
  copy people;
  var visits outcome;
run;

proc sgplot data=plot;
  vbar date / response=col1 group=_name_ groupdisplay=cluster name='relatedcounts';
  vline date / response=people group=_name_ y2axis lineattrs=(color=black thickness=3) name='people';

  xaxis 
    type = time
    interval = month
  ;

  format date date9.;

  yaxis grid label='Related counts' values=(0 to 800 by 100);
  y2axis label='# People' values=(0 to 70 by 10);

  keylegend 'relatedcounts' / title="";
run;

会产生

在此处输入图像描述

于 2021-03-30T20:48:18.973 回答