PROC SGPLOT中带有curvelabelpos和xaxis的SAS问题

PROC SGPLOT中带有curvelabelpos和xaxis的SAS问题,sas,ods,sgplot,Sas,Ods,Sgplot,我目前正在尝试使用SAS中的PROC SGPLOT创建一个包含五行的系列图(8年级、10年级、12年级、大学生和年轻人)。yaxis是吸毒流行率的百分比,范围为0-100。xaxis是1975-2019年,但已格式化(使用proc格式),因此它将年份值显示为“75-”19。我想用其各自的组(8年级-年轻成人)标记每条线。但当我使用: proc sgplot data = save.fig2_1data noautolegend ; series x=year y=eighth / lineatt

我目前正在尝试使用SAS中的PROC SGPLOT创建一个包含五行的系列图(8年级、10年级、12年级、大学生和年轻人)。yaxis是吸毒流行率的百分比,范围为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 ;


“curvelabelpos=”不允许将我的标签放置在“12年级”和“大学生”的第一个数据点上方,这样我的xaxis就不会在绘图的左侧占据所有空间。如何将这两个标签移动到每行的第一个数据点上方,以使xaxis没有空白?

没有生成所需标签的
系列
语句选项

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

在此示例代码中,
curvelabel=
选项设置为
'
,因此该过程生成一条使用最宽水平绘图空间的系列线。<代码> SGANNO./COD>数据集包含注释函数,这些函数将在空白的Currababele附近绘制第一个数据点附近的CurrababelTeX。根据需要调整
%sgtext
anchor=
值。请务必阅读文档以了解所有文本注释功能

对于希望在系列行中进行人工拆分的情况,有两种方法可以尝试:

  • 引入一个假年份2012.5,该年份的所有系列变量都没有值。我试过这个,但5个系列中只有1个画上了“假”分割
  • 为需要拆分的N行引入N个新变量。对于拆分后的时间框架,将数据复制到新变量中,并将原始值设置为“缺失”。
    • 为新变量添加
      系列
      语句

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 ;

实际上,我还有一个问题要问另一张图。我必须在2013年引入一个断线。我会为每一行创建两个不同的系列,并对它们进行相同的颜色编码,还是在proc sgplot中有引入换行符的选项?换行符我想你的意思是Y数据在一些年份中丢失了。如果是这样,请在您的
系列
语句中使用
/BREAK
选项。很抱歉,要说得更具体一些。从技术上讲,没有缺失数据。调查问题的措辞在2013年发生了变化,人为增加了措辞。我们只想打断这条线,以表明问题发生了变化。假设中断(或增加)适用于使用
系列
绘制的所有变量是否正确?很高兴它成功了,欢迎来到stackoverflow。确保标记答案和其他有用的帖子和问题。看看如何
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 ;