sas - PROC SGPLOT中curvelabelpos和xaxis的SAS问题
问题描述
我目前正在尝试在 SAS 中使用 PROC SGPLOT 创建一个具有五行(8 年级、10 年级、12 年级、大学生和年轻人)的系列情节。y 轴是药物使用流行率的百分比,范围为 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 没有空白空间?
解决方案
没有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;
推荐阅读
- python - to_sql chunksize太多后的Pandas sqlalchemy错误
- swift - Falling Max Swift Combine 出版商
- java - 如何在 ubuntu 20.04 LTS 中为 Tomcat 释放 8080 端口
- firebase - React Native Firebase 删除用户数据
- python - 无法从嵌套的 JSON 文件创建 pandas DataFrame
- ssl-certificate - 是否可以获得用于端口转发 VPN 服务的 SSL 证书?
- python - 我在编写函数时遇到问题。(vowels_repeated) 返回一个只有元音重复的字符串
- reactjs - 将发布方法 404 反应到公共 URL
- python - pyautogui.leftclick 和/或右键单击不起作用
- arrays - 使用 C 从字符串数组中获取特定字符