下面的数据有两组:Group 1和Group 2。每一组都有多个主题。
data sgplot_CPA;
input trt_group$ time$ subject$ 11. results;
datalines;
Group-1 1 203-070-001 20
Group-1 3 203-070-001 30
Group-1 7 203-070-001 35
Group-1 10 203-070-001 50
Group-1 14 203-070-001 40
Group-1 21 203-070-001 25
Group-1 28 203-070-001 40
Group-1 US 203-070-001 30
Group-1 1 203-070-003 25
Group-1 3 203-070-003 35
Group-1 7 203-070-003 30
Group-1 10 203-070-003 40
Group-1 14 203-070-003 50
Group-1 21 203-070-003 21
Group-1 28 203-070-003 24
Group-1 US 203-070-003 31
Group-1 1 203-070-005 32
Group-1 3 203-070-005 22
Group-1 7 203-070-005 30
Group-1 10 203-070-005 56
Group-1 14 203-070-005 28
Group-1 21 203-070-005 35
Group-1 28 203-070-005 29
Group-1 US 203-070-005 41
Group-2 1 203-070-007 15
Group-2 3 203-070-007 45
Group-2 7 203-070-007 23
Group-2 10 203-070-007 48
Group-2 14 203-070-007 26
Group-2 21 203-070-007 17
Group-2 28 203-070-007 35
Group-2 US 203-070-007 11
Group-2 1 203-070-008 27
Group-2 3 203-070-008 40
Group-2 7 203-070-008 25
Group-2 10 203-070-008 30
Group-2 14 203-070-008 40
Group-2 21 203-070-008 19
Group-2 28 203-070-008 28
Group-2 US 203-070-008 39
;
run;我想在意大利面条的情节中添加两个传说,一个是Treatment group (trt_group),另一个是subject。对于第一组来说,所有的线条都是紫色的,在每个组内分别标记每一个主题以识别主题趋势。
这是我试过的代码,
proc sgplot data=sgplot_CPA;
styleattrs datacontrastcolors=(purple green orange)
datasymbols=(squarefilled trianglefilled circlefilled StarFilled TriangleDownFilled )
datalinepatterns=(Solid Solid Solid ShortDash ShortDash MediumDash LongDash MediumDashShortDash);
/* title 'Study Results by Treatment Group';*/
series x=time y=results / group=subject grouplc=trt_group name='grouping' groupdisplay=cluster clusterwidth=0.25 Markers MARKERATTRS = (color = black) ;
scatter x =time y = results / group = subject name = 'subjects' groupdisplay=cluster clusterwidth=0.25 markerattrs=(color = black );
keylegend 'grouping' / type=linecolor sortorder = ASCENDING Position = TopLeft title="Treatment";
keylegend 'subjects' / type =marker sortorder = ASCENDING Position = Bottom title="Subject";
xaxis label="Visit";
yaxis label="Result Score";
footnote J=L "US=Unscheduled";
run;这是输出

如何将突出显示的图例更改为不同的标记样式?例如,203-070-001应该有带紫色的星形符号,203-070-003应该有带紫色的方形,而203-070-005应该有紫色的三角形,对于第二组,任何帮助都是非常感谢的。
发布于 2022-08-27 13:09:53
您将需要使用数据属性映射。我发现关于这个主题的SAS文档有些薄弱,而且在不同的页面和简单的示例中分布得太广了。
示例:
所演示的数据属性映射假定一个主题只有一个处理方式。
/*
* derive attribute maps SERIES_ATTR and MARKERS_ATTR
* based on actual data being plotted
*/
data attribute_map;
length id $30 value $50;
set sgplot_cpa;
by trt_group subject;
* zero-based arrays for easy modulus indexing that will loop through each array;
array colors [0:2] $32 _temporary_
('purple' 'green' 'orange');
array markers[0:4] $32 _temporary_
('starfilled' 'squarefilled' 'trianglefilled' 'circlefilled' 'diamondfilled');
retain counter1 -1;
if first.trt_group then do;
* wanted: series attributes that vary by treatment;
counter1 ++ 1; * increase modulus index for treatment;
counter2 = -1; * reset modulus index for subject;
id = 'SERIES_ATTR';
value = trt_group;
linecolor = colors[mod(counter1,dim(colors))]; * apply modulus index;
output;
call missing (id, value, linecolor);
end;
if first.subject;
* wanted: scatter attribute that vary by subject, resetting for each treatment;
counter2 ++ 1; * increase modulus index for subject within treatment;
id = 'MARKERS_ATTR';
value = subject;
markercolor = colors[mod(counter1,dim(colors))];
markersymbol = markers[mod(counter2,dim(markers))];
output;
keep id value markercolor markersymbol linecolor;
run;
proc sort;
by id value;
run;
ods html file='report.html';
* series and scatter are used because two different keylegends are wanted;
proc sgplot data=sgplot_CPA dattrmap=attribute_map;
series x=time y=results
/ name='treatments'
group=subject
groupdisplay=cluster
clusterwidth=0.25
grouplc=trt_group
lcattrid=SERIES_ATTR
;
scatter x=time y=results
/ name = 'subjects'
group = subject
attrid = MARKERS_ATTR
groupdisplay=cluster
clusterwidth=0.25
;
keylegend 'treatments' / type=linecolor sortorder=ascending position=TopLeft title="Treatment";
keylegend 'subjects' / type=marker sortorder=ascending position=Bottom title="Subject";
xaxis label="Visit";
yaxis label="Result Score";
footnote J=L "US=Unscheduled";
run;
ods html close;

https://stackoverflow.com/questions/73492433
复制相似问题