Data visualization with SAS programming

SGPLOT Series - Group Option Line Styles

Accepted Solution Solved
Reply
Regular Contributor
Posts: 223
Accepted Solution

SGPLOT Series - Group Option Line Styles

I am trying to run a time series plot with multiple lines, but I can't figure out how to group the lines into colors.  Currently, I am getting a line for each LESID, which in turn produces a different line of a different color and pattern.  I still want a line for each LESID, but I just want 2 different line types:  a solid blue where POSNEG=Positive and a solid red where POSNEG=Negative.  The final graph should show 3 red lines and 7 blue lines.  Is it possible to make that change?  I have tried playing with the GROUP= option in the SERIES statement, but that just messes things up.

data lesions;

      input LESID POSNEG $ PCTCHG WEEKS;

      informat pctchg percent9.1;

      format pctchg percent9.1;

      cards;

1     Negative    0.0%  0.0

1     Negative    0.0%  7.4

1     Negative    -3.9% 15.1

1     Negative    -19.2%      24.0

1     Negative    -19.2%      31.4

1     Negative    -26.9%      39.4

1     Negative    -26.9%      48.4

1     Negative    -19.2%      54.4

2     Positive    0.0%  0.0

2     Positive    0.0%  7.4

2     Positive    1.2%  15.4

2     Positive    3.7%  24.1

2     Positive    -12.4%      31.4

2     Positive    -14.8%      41.1

2     Positive    -23.5%      48.4

2     Positive    -23.5%      55.4

3     Positive    0.0%  0.0

3     Positive    -2.9% 7.4

3     Positive    -4.3% 15.4

3     Positive    1.4%  24.1

3     Positive    1.4%  31.4

3     Positive    1.4%  41.1

3     Positive    -54.3%      48.4

3     Positive    -1.4% 55.4

4     Positive    0.0%  0.0

4     Positive    8.3%  7.4

4     Positive    -16.7%      15.4

4     Positive    -8.3% 24.1

4     Positive    -8.3% 31.4

4     Positive    -8.3% 41.1

4     Positive    8.3%  48.4

4     Positive    8.3%  55.4

5     Positive    0.0%  0.0

5     Positive    3.0%  7.4

5     Positive    -3.0% 15.4

5     Positive    9.1%  24.1

5     Positive    3.0%  31.4

5     Positive    15.2% 41.1

5     Positive    9.1%  48.4

5     Positive    3.0%  55.4

6     Positive    0.0%  0.0

6     Positive    -25.0%      4.1

7     Negative    0.0%  0.0

7     Negative    -5.9% 4.1

8     Positive    0.0%  0.0

8     Positive    -11.1%      4.1

9     Positive    0.0%  0.0

9     Positive    -24.0%      4.1

10    Negative    0.0%  0.0

10    Negative    5.6%  7.3

10    Negative    0.0%  13.3

;

run;

goptions reset=all;

proc sgplot data=lesions;

      series x=weeks y=pctchg / group=lesid;

      refline 0.20 -0.30 / lineattrs=(pattern=2);

      xaxis label='Time on Study (weeks)' values=(0 to 60 by 6);

      yaxis grid label='Percent Change from Baseline' values=(-1 to 0.6 by 0.2);

run;

quit;


Accepted Solutions
Solution
‎02-20-2015 09:47 AM
Respected Advisor
Posts: 3,782

Re: SGPLOT Series - Group Option Line Styles

I think this is right.  You need to added missing obs at each LESID and use BREAK option on SERIES statement.

data lesions;
   set lesions;
   by lesid;
   output;
  
if last.lesid then do;
     
call missing(week, pctchg);
      output;
     
end;
  
run;
proc sgplot data=lesions;
      series x=weeks y=pctchg / group=posneg break;
     
refline 0.20 -0.30 / lineattrs=(pattern=2);
      xaxis label='Time on Study (weeks)' values=(0 to 60 by 6);
      yaxis grid label='Percent Change from Baseline' values=(-1 to 0.6 by 0.2);
run;
quit;

2-20-2015 8-46-14 AM.png

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: SGPLOT Series - Group Option Line Styles

I am no expert to SAS graph, and I am probably not becoming one :smileycry:. However, this is how I survive so far with the help from EG. The following code is EG generated and seems to do what you asked. I am not aware of a way you can define line color based on another variable besides the one that you use to group, but you can do it manually:

data lesions;

      input LESID POSNEG $ PCTCHG WEEKS;

      informat pctchg percent9.1;

      format pctchg percent9.1;

      cards;

1     Negative 0.0%  0.0

1     Negative 0.0%  7.4

1     Negative -3.9% 15.1

1     Negative -19.2%      24.0

1     Negative -19.2%      31.4

1     Negative -26.9%      39.4

1     Negative -26.9%      48.4

1     Negative -19.2%      54.4

2     Positive 0.0%  0.0

2     Positive 0.0%  7.4

2     Positive 1.2%  15.4

2     Positive 3.7%  24.1

2     Positive -12.4%      31.4

2     Positive -14.8%      41.1

2     Positive -23.5%      48.4

2     Positive -23.5%      55.4

3     Positive 0.0%  0.0

3     Positive -2.9% 7.4

3     Positive -4.3% 15.4

3     Positive 1.4%  24.1

3     Positive 1.4%  31.4

3     Positive 1.4%  41.1

3     Positive -54.3%      48.4

3     Positive -1.4% 55.4

4     Positive 0.0%  0.0

4     Positive 8.3%  7.4

4     Positive -16.7%      15.4

4     Positive -8.3% 24.1

4     Positive -8.3% 31.4

4     Positive -8.3% 41.1

4     Positive 8.3%  48.4

4     Positive 8.3%  55.4

5     Positive 0.0%  0.0

5     Positive 3.0%  7.4

5     Positive -3.0% 15.4

5     Positive 9.1%  24.1

5     Positive 3.0%  31.4

5     Positive 15.2% 41.1

5     Positive 9.1%  48.4

5     Positive 3.0%  55.4

6     Positive 0.0%  0.0

6     Positive -25.0%      4.1

7     Negative 0.0%  0.0

7     Negative -5.9% 4.1

8     Positive 0.0%  0.0

8     Positive -11.1%      4.1

9     Positive 0.0%  0.0

9     Positive -24.0%      4.1

10    Negative 0.0%  0.0

10    Negative 5.6%  7.3

10    Negative 0.0%  13.3

;

run;

PROC SORT

      DATA=WORK.LESIONS(KEEP=WEEKS PCTCHG LESID)

      OUT=WORK.SORTTempTableSorted

      ;

      BY WEEKS;

RUN;

SYMBOL1

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=RED

      CV = _STYLE_

;

SYMBOL2

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=BLUE

      CV = _STYLE_

;

SYMBOL3

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=BLUE

      CV = _STYLE_

;

SYMBOL4

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=BLUE

      CV = _STYLE_

;

SYMBOL5

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=BLUE

      CV = _STYLE_

;

SYMBOL6

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=BLUE

      CV = _STYLE_

;

SYMBOL7

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=RED

      CV = _STYLE_

;

SYMBOL8

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=BLUE

      CV = _STYLE_

;

SYMBOL9

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=BLUE

      CV = _STYLE_

;

SYMBOL10

      INTERPOL=JOIN

      HEIGHT=10pt

      VALUE=NONE

      LINE=1

      WIDTH=2

      CI=RED

      CV = _STYLE_

;

Legend1

      FRAME

      ;

Axis1

      STYLE=1

      WIDTH=1

      MINOR=NONE

;

Axis2

      STYLE=1

      WIDTH=1

      MINOR=NONE

;

TITLE;

TITLE1 "Line Plot";

FOOTNOTE;

PROC GPLOT DATA = WORK.SORTTempTableSorted

;

PLOT PCTCHG * WEEKS      =LESID

/

      VAXIS=AXIS1

      HAXIS=AXIS2

FRAME LEGEND=LEGEND1

;

/* -------------------------------------------------------------------

   End of task code.

------------------------------------------------------------------- */

RUN; QUIT;

TITLE; FOOTNOTE;

GOPTIONS RESET = SYMBOL;

Solution
‎02-20-2015 09:47 AM
Respected Advisor
Posts: 3,782

Re: SGPLOT Series - Group Option Line Styles

I think this is right.  You need to added missing obs at each LESID and use BREAK option on SERIES statement.

data lesions;
   set lesions;
   by lesid;
   output;
  
if last.lesid then do;
     
call missing(week, pctchg);
      output;
     
end;
  
run;
proc sgplot data=lesions;
      series x=weeks y=pctchg / group=posneg break;
     
refline 0.20 -0.30 / lineattrs=(pattern=2);
      xaxis label='Time on Study (weeks)' values=(0 to 60 by 6);
      yaxis grid label='Percent Change from Baseline' values=(-1 to 0.6 by 0.2);
run;
quit;

2-20-2015 8-46-14 AM.png
Regular Contributor
Posts: 223

Re: SGPLOT Series - Group Option Line Styles

My graph apparently defaults to a dashed line for the negative group.  Yours didn't.  Do you know why?  If we can't figure it out, it won't be a huge issue.

Respected Advisor
Posts: 3,782

Re: SGPLOT Series - Group Option Line Styles

My SAS Version is AUTOMATIC SYSVLONG4 9.03.01M2P08152012

and the RTF output does use two line types.  I will have to RTM to figure how to change it.

2-20-2015 8-58-10 AM.png

Respected Advisor
Posts: 3,782

Re: SGPLOT Series - Group Option Line Styles

series x=weeks y=pctchg / group=posneg break LINEATTRS=(pattern=1);
Super Contributor
Posts: 252

Re: SGPLOT Series - Group Option Line Styles

Hello djbateman.

Try adding this code to your lesion program. It utilises the index option in GTL:


data lesions2;
  set lesions;
  if posneg = "Negative" then posnegn = 1;
  else posnegn = 2;
run;

proc sort data = lesions2;
  by lesid weeks;
run;


proc template;
  define statgraph lesionplot;
    begingraph;
   layout overlay / yaxisopts = (label='Percent Change from Baseline' linearopts = (tickvaluesequence = (start= -1 end = 0.6 increment = 0.2)) )
                    xaxisopts = (label='Time on Study (weeks)' linearopts = (tickvaluesequence = (start= 0 end = 60 increment = 6)) );
     seriesplot x =weeks y=pctchg / group = lesid index = POSNEGn;
  referenceline y = 0.20  / lineattrs=(pattern=2);
  referenceline y = -0.30  / lineattrs=(pattern=2);
   endlayout;
endgraph;
  end;
run;

proc sgrender data = lesions2 template = lesionplot;
run;

SAS Super FREQ
Posts: 925

Re: SGPLOT Series - Group Option Line Styles

If you have SAS 9.4m2, the solution is very simple -- use the GROUPLC option. You can assign variables to each of the line or marker attributes in the series, such as line color (GROUPLC), line pattern (GROUPLP), marker symbol (GROUPMS), and marker color (GROUPMC). Notice that the legend type is set to display only the line color. That way, only the color swatches for positive and negative appear.

For SAS 9.3 and greater users, you could create an attributes map where each LESID value is associated with a color based on the positive or negative value. Let me know if you need more info on how to do that.

proc sgplot data=lesions;

      series x=weeks y=pctchg / group=lesid grouplc=posneg name="series";

      refline 0.20 -0.30 / lineattrs=(pattern=2);

      keylegend "series" / type=linecolor;

      xaxis label='Time on Study (weeks)' values=(0 to 60 by 6);

      yaxis grid label='Percent Change from Baseline' values=(-1 to 0.6 by 0.2);

run;

quit;


SGPlot2.png

SAS Super FREQ
Posts: 1,134

Re: SGPLOT Series - Group Option Line Styles

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 1353 views
  • 0 likes
  • 6 in conversation