简体   繁体   English

如何在 SAS 的 SGPLOT 图例中添加不同的颜色

[英]How to add different color in SGPLOT legend in SAS

Following data has two groups Group 1 and Group 2 .以下数据有两组Group 1Group 2 Each group has multiple subjects.每组有多个科目。

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;

I would like to add two legend in the Spaghettin plot, one for Treatment group (trt_group) and one for subject .我想在 Spaghettin plot 中添加两个图例,一个用于Treatment group (trt_group) ,一个用于subject Say for Group-1, All line will be purple color, within each group separate marker for each subject to identify the subject trend.比如说第 1 组,所有线都将是紫色,在每个组内为每个主题单独标记以识别主题趋势。

Here is code I have tried,这是我尝试过的代码,

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;

Here is the output这是 output

在此处输入图像描述

How can I change the highlighted legend to different marker style?如何将突出显示的图例更改为不同的标记样式? For example, 203-070-001 should have star symbol with purple color, 203-070-003 should have square with purple color, and 203-070-005 should have triangle with purple color and similar for Group 2. Any help is appreciated.例如,203-070-001 应该有紫色的星号,203-070-003 应该有紫色的正方形,203-070-005 应该有紫色的三角形和第 2 组类似的三角形。感谢任何帮助.

You will need to use data attribute maps.您将需要使用数据属性映射。 I find that the SAS documentation on this topic is somewhat weak and too widely dispersed amongst different pages and simplistic examples.我发现有关此主题的 SAS 文档有些薄弱,并且分散在不同页面和简单示例中的范围太广。

Example:例子:

The data attribute maps demonstrated presumes a subject has only single treatment.所展示的数据属性图假定受试者只有一次治疗。

/* 
 * 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;

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM