PROC SGPLOT 中 curvelabelpos 和 xaxis 的 SAS 问题

SAS problem with curvelabelpos and xaxis in PROC SGPLOT

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

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

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

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

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

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

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

  • 引入一个假年份 2012.5,其中 none 的系列变量具有值。我试过了,但 5 个系列中只有 1 个使用 'fake' 拆分绘制。
  • 为需要拆分的N行引入N个新变量。对于 post 拆分时间范围,将数据复制到新变量中并将原始变量设置为缺失。
    • 为新变量添加 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;

Richard 回答了您明确想要的内容,但我认为从图形的角度来看您想要的内容并不理想 - 这就是 SAS 不会为您做的原因。

线条上的标签难以阅读,尤其是当您使用与线条相同的颜色时。图表外的标签更清晰,将标签放在 keylegend 中也是如此。

在这种情况下,我会使用 CURVELABELLOC=OUTSIDE,或者使用 CURVELABELPOS=MAX(默认,将它们放置在图表的右侧),或者 CURVELABELPOS=MIN,根据您的喜好将它们放置在更靠近起点的位置但也覆盖了轴(看起来不那么干净)。

以此为例。这是非常清晰的,曲线标签位于眼睛自然移动到的位置,并且不会改变轴的大小。将它们放在右边也意味着它们在所有行的同一位置,这比将它们放在交错的行的开头更干净。

data fig2_1data;
  call streaminit(7);
  tenth  = 0.5;
  twelfth= 0.6;
  do year=1975 to 2019;
    if year eq 1987 then eighth=0.4;
    eighth = rand('Uniform',0.2)-0.1 + eighth;
    tenth = rand('Uniform',0.2)-0.1 + tenth;
    twelfth = rand('Uniform',0.2)-0.1 + twelfth;
    output;
 end;
run;
proc sgplot data = fig2_1data noautolegend ;
series x=year y=eighth / lineattrs=(color=orange) 
                         curvelabel='8th Grade' curvelabelpos=max curvelabelloc=outside;
series x=year y=tenth / lineattrs=(color=green) 
                        curvelabel='10th Grade' curvelabelpos=max curvelabelloc=outside;
series x=year y=twelfth / lineattrs=(color=blue) 
                        curvelabel='12th Grade' curvelabelpos=max curvelabelloc=outside;
xaxis label="YEAR" values=(1975 to 2019 by 2) minor;
yaxis label="PERCENT" max=1 min=0 ;
format year yr. ; run ;