PROC SGPLOT 中 curvelabelpos 和 xaxis 的 SAS 问题

标签 sas ods sgplot

我目前正在尝试使用 SAS 中的 PROC SGPLOT 创建一个包含 5 条线的系列图(8 年级、10 年级、12 年级、大学生和年轻人)。 Y 轴是吸毒流行率的百分比,范围为 0-100。 x 轴是 1975-2019 年,但已格式化(使用 proc 格式),以便将年份值显示为“75-”19。我想使用各自的组(八年级 - 青少年)来标记每一行。但是当我使用时:

proc sgplot data = save.fig2_1data noautolegend ;
series x=year y=eighth / lineattrs=(color=orange) curvelabel='8th Grade' curvelabelpos=start ;
series x=year y=tenth / lineattrs=(color=green) curvelabel='10th Grade' curvelabelpos=start ;
series x=year y=twelfth / lineattrs=(color=blue) curvelabel='12th Grade' curvelabelpos=start;
series x=year y=college / lineattrs=(color=red) curvelabel='College Students' curvelabelpos=start;
series x=year y=youngadult / lineattrs=(color=purple) curvelabel='Young Adults' curvelabelpos=start ;
xaxis label="YEAR" values=(1975 to 2019 by 2) minor;
yaxis label="PERCENT" max=100 min=0 ;
format year yr. ; run ;

Series Plot enter image description here

“curvelabelpos=”没有提供将标签放置在“12年级”和“大学生”的第一个数据点上方的选项,因此我的 xaxis 没有图左侧的所有空间。如何将这两个标签移动到每行的第一个数据点上方,以使 x 轴没有空白空间?

最佳答案

没有series语句选项可以生成您想要的标签。

您必须为 sgplot 创建注释数据集。

在此示例代码中,curvelabel= 选项设置为 '',因此该过程会生成一条使用最宽水平绘图空间的系列线。 sganno 数据集包含注释函数,这些函数将在带有空白曲线标签的系列的第一个数据点附近绘制您自己的曲线标签文本。根据需要调整 %sgtext anchor= 值。请务必阅读SG Annotation Macro Dictionary了解所有文本注释功能的文档。

对于想要在系列线中人为分割的情况,可以尝试以下两种方法:

  • 引入一个假年份 2012.5,其中所有系列变量都没有值。我尝试了这个,但 5 个系列中只有 1 个是用“假”分割绘制的。
  • 为需要拆分的 N 行引入 N 个新变量。对于分割后的时间范围,将数据复制到新变量中,并将原始数据设置为缺失。
    • 为新变量添加 SERIES 语句。
data have;
  call streaminit(1234);

  do year = 1975 to 2019;
    array response eighth tenth twelfth college youngadult;

    if year >= 1991 then do;
      eighth = round (10 + rand('uniform',10), .1);
      tenth = eighth + round (5 + rand('uniform',5), .1);
      twelfth = tenth + round (5 + rand('uniform',5), .1);

      if year in (1998:2001) then tenth = .;
    end;
    else do;
      twelfth = 20 + round (10 + rand('uniform',25), .1);
    end;

    if year >= 1985 then do;
      youngadult = 25 + round (5 + rand('uniform',20), .1);
    end;

    if year >= 1980 then do;
      college = 35 + round (7 + rand('uniform',25), .1);
    end;

    if year >= 2013 then do _n_ = 1 to dim(response);
      %* simulate inflated response level;
      if response[_n_] then response[_n_] = 1.35 * response[_n_];
    end;

    output;
  end;
run;

data have_split;
  set have;
  array response  eighth  tenth  twelfth  college  youngadult;
  array response2 eighth2 tenth2 twelfth2 college2 youngadult2;

  if year >= 2013 then do _n_ = 1 to dim(response);
    response2[_n_] = response[_n_];
    response [_n_] = .;
  end;
run;

ods graphics on;
ods html;

%sganno;

data sganno;
  %* these variables are used to track '1st' or 'start' point 
  %* of series being annotated
  ;
  retain y12 ycl;

  set have;
  if missing(y12) and not missing(twelfth)  then do; 
    y12=twelfth;
    %sgtext(label="12th Grade", textcolor="blue", drawspace="datavalue", anchor="top", x1=year, y1=y12, width=100, widthunit='pixel')
  end;     

  if missing(ycl) and not missing(college) then do; 
    ycl=college; 
    %sgtext(label="College Students", textcolor="red", drawspace="datavalue", anchor="bottom", x1=year, y1=ycl, width=100, widthunit='pixel')
  end;
run;


proc sgplot data=have_split noautolegend sganno=sganno;
series x=year y=eighth     / lineattrs=(color=orange) curvelabel='8th Grade'        curvelabelpos=start;*auto curvelabelloc=outside ;
series x=year y=tenth      / lineattrs=(color=green)  curvelabel='10th Grade'       curvelabelpos=start;*auto curvelabelloc=outside ;
series x=year y=twelfth    / lineattrs=(color=blue)   curvelabel='' curvelabelpos=start;*auto curvelabelloc=outside ;
series x=year y=college    / lineattrs=(color=red)    curvelabel='' curvelabelpos=start;*auto curvelabelloc=outside ;
series x=year y=youngadult / lineattrs=(color=purple) curvelabel='Young Adults'     curvelabelpos=start;*auto curvelabelloc=outside ;

* series for the 'shifted' time period use the new variables;
series x=year y=eighth2     / lineattrs=(color=orange) ;
series x=year y=tenth2      / lineattrs=(color=green)  ;
series x=year y=twelfth2    / lineattrs=(color=blue)   ;
series x=year y=college2    / lineattrs=(color=red)    ;
series x=year y=youngadult2 / lineattrs=(color=purple) ;

xaxis label="YEAR" values=(1975 to 2019 by 2) minor;
yaxis label="PERCENT" max=100 min=0 ;
run ;

ods html close;
ods html;

enter image description here

关于PROC SGPLOT 中 curvelabelpos 和 xaxis 的 SAS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59974818/

相关文章:

sas - 使用 Proc sgplot 的条形图中每个条形的不同标签

csv - 在 SAS 中导入具有较长不同长度字段的 CSV 会浪费磁盘空间

sas - 如何在 SAS 中进行逐行求和

python - 电子表格到python字典的转换

mysql - 将 ODS 文件导入 MySQL

sas - 如何将自定义拟合线添加到 SAS SGplot Scatter

sas - 通过组控制线条颜色/where 子句

machine-learning - SAS Enterprise Miner - 集群节点 - 可以将坐标矩阵作为输入吗?

sas - 更改存储的宏 SAS

python - 如何从 OpenOffice Calc .ods 文件中读取单元格的值?