☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Quartz | Level 8

## How to get different colors for different lines in a spaghetti plot

``````/*subsetting the table to get only subjid, visit and visiopro	*/
proc sql;
create table pro as
select *
from ana.PRO_F_ADQS2(keep=d_SUBJID d_VISITN d_VPD5_Tot)
where d_SUBJID IN ( '05-07','05-08','05-09','05-10','05-11');
quit;

/* Count the number of unique values of SUBJECT. */
/* Also create macro variables containing the    */
/* style values for each plot line.  The line    */
/* color is based on the visit.        */
data total_dup1;
length color \$5.;
set pro end=eof;
by d_SUBJID;
if d_VISITN=1 then color='vibg';
if d_VISITN=2 then color='red';
if d_VISITN=3 then color='black';
if d_VISITN=4 then color='depk';
if first.d_SUBJID then do;
count+1;
call symput('gd'||trim(left(count)),
'style '|| 'GraphData' || trim(left(count)) ||
' from GraphData' || trim(left(count)) ||
' / ContrastColor=' || trim(left(color)) ||
' linestyle=1;');
end;
if eof then call symput('total',count);
run;

/* Define a macro that generates all style changes. */
%macro gdata;
%do i=1 %to &total;
&&gd&i
%end;
%mend gdata;

/* Create the custom style. */
proc template;
define style styles.mystyle;
parent=styles.default;
%gdata;
end;
run;

/*formatting the values to get the desred lenged values*/
proc format;
value visit
1 = 'Baseline'
2 = 'Timepoint 2'
3 = 'Timepoint 3'
4 = 'Timepoint 4';
run;

ods rtf file='C:\Users\ravindra.babu\OneDrive - OneWorkplace\Projs\NO8072A - RP PRO\NO8072A_F2_1_v_01.rtf'
style=rtf bodytitle nogfootnote;
goptions reset=all;
ods graphics / border=off width=18cm height=8cm ;
ods graphics / border=off width=17cm height=8.5cm;

/*ods rtf file='C:\Users\ravindra.babu\OneDrive - OneWorkplace\Projs\NO8072A - RP PRO\NO8072A_F2_1_v_01.rtf'  style=styles.mystyle;                                                   */

proc sgplot data=total_dup1 noautolegend;
series x=d_VISITN y=d_VPD5_Tot / group=d_SUBJID grouplc=count;
xaxis label="Visit" values=(1 to 4 by 1);
yaxis label="Total" values=(0 to 5 by 1);
format  d_VISITN visit.;
title "Figure 2.1. Individual patients ViSIO-PRO total score at each visit (PRO treated analysis population, N=5)";

/* Use a FOOTNOTE statement to simulate a legend. */
footnote1 box=1 bcolor=white
'Note: The PRO Treated Analysis Population are adult and adolescent patients who have received Luxturna treatment with at least one item completed on the ViSIO-PRO at one pre-Luxturna treatment timepoint (baseline) and at least one item completed on the ViSIO-PRO at one post-Luxturna treatment follow-up timepoint. ViSIO-PRO total score ranges from 0 to 5 with higher scores indicating worse visual functioning or HRQoL.'  ;

run;
quit;

ods rtf close;``````

The above is my code, i dont know where i am doing wrong, the graphs doesn't give different colors for each line all i get is the same colour but the legend appears to be in different colour, can someone help me please to get different colours for different lines.

1 ACCEPTED SOLUTION

Accepted Solutions
Quartz | Level 8

## Re: How to get different colors for different lines in a spaghetti plot

hello, thank you for the response, i had tried using the options that you had suggested, please correct me where i am doing wrong in the below code, the output seems to have only one single color.

``````ods rtf file='C:\Users\ravindra.babu\OneDrive - OneWorkplace\Projs\NO8072A - RP PRO\NO8072A_F2_2_v_01.rtf'
style=festival ;
ods graphics / border=off width=18cm height=8cm ;
ods graphics / border=off width=17cm height=8.5cm;

/* Create the graph. */
proc sgplot data=total_dup ;
styleattrs datacontrastcolors=(red green black orange blue);
series x=d_VISITN y=total / group=d_SUBJID    grouplc=domain_score name=grouping;
xaxis label="Visit" values=(1 to 4 by 1);
yaxis label="Total" values=(0 to 5 by 1);
keylegend 'grouping'/type=linecolor noborder;
format  d_VISITN visit. domain_score \$score.;
title "Figure 2.2. Spaghetti plot of ViSIO-PRO domain scores (PRO treated analysis population, N=5)";

/* Use a FOOTNOTE statement to simulate a legend. */
/*   footnote1 box=0 bcolor=white                                                                                                            */
footnote1   'Note: The PRO Treated Analysis Population are adult and adolescent patients who have
received Luxturna treatment with at least one item completed on the ViSIO-PRO
at one pre-Luxturna treatment timepoint (baseline) and at least one item
completed on the ViSIO-PRO at one post-Luxturna treatment follow-up timepoint. ViSIO-PRO domain
scores ranges from 0 to 5 with higher scores indicating worse visual functioning or HRQoL.'  ;

run;

ods rtf close;``````
3 REPLIES 3
Super User

## Re: How to get different colors for different lines in a spaghetti plot

If you go through all that rigamarole with macro variables and style elements I suggest that you look up the DATTRMAP= option for adding specific controls to a graph instead of creating and recreating a style whose contents you may not actually know at any given time.

I am not sure why you use the GROUPLC= COUNT option to control line color when you have a number of lines of code assigning values to a variable named COLOR that contains color values. But those were set according to values of your Xaxis variable, so don't make much sense either.

Hint: Provide data in the form of a datastep so we can test code.

Suggestion: Use a different different style than RTF such as MEADOW or FESTIVAL that have different color schemes. Since your code for SGPLOT shows the option NOAUTOLEGEND your comment about legends doesn't make sense or does not apply to the code shown.

Instead of creating a style or creating a dattrmap data set (useful for multiple graphs where you want things consistent ) perhaps just a STYLEATTRS(Datacontrastcolors)=(list of colors to use); would be simpler than creating a style.

Quartz | Level 8

## Re: How to get different colors for different lines in a spaghetti plot

hello, thank you for the response, i had tried using the options that you had suggested, please correct me where i am doing wrong in the below code, the output seems to have only one single color.

``````ods rtf file='C:\Users\ravindra.babu\OneDrive - OneWorkplace\Projs\NO8072A - RP PRO\NO8072A_F2_2_v_01.rtf'
style=festival ;
ods graphics / border=off width=18cm height=8cm ;
ods graphics / border=off width=17cm height=8.5cm;

/* Create the graph. */
proc sgplot data=total_dup ;
styleattrs datacontrastcolors=(red green black orange blue);
series x=d_VISITN y=total / group=d_SUBJID    grouplc=domain_score name=grouping;
xaxis label="Visit" values=(1 to 4 by 1);
yaxis label="Total" values=(0 to 5 by 1);
keylegend 'grouping'/type=linecolor noborder;
format  d_VISITN visit. domain_score \$score.;
title "Figure 2.2. Spaghetti plot of ViSIO-PRO domain scores (PRO treated analysis population, N=5)";

/* Use a FOOTNOTE statement to simulate a legend. */
/*   footnote1 box=0 bcolor=white                                                                                                            */
footnote1   'Note: The PRO Treated Analysis Population are adult and adolescent patients who have
received Luxturna treatment with at least one item completed on the ViSIO-PRO
at one pre-Luxturna treatment timepoint (baseline) and at least one item
completed on the ViSIO-PRO at one post-Luxturna treatment follow-up timepoint. ViSIO-PRO domain
scores ranges from 0 to 5 with higher scores indicating worse visual functioning or HRQoL.'  ;

run;

ods rtf close;``````
Super User

## Re: How to get different colors for different lines in a spaghetti plot

Without data there is no way to tell what you may be "doing wrong".

Either provide some example data in the form of data step code or rewrite your code to use a data set in the SASHELP library.

A typical approach to determining which of your options are not playing well with your graph is to start with very sparse code. Then add one feature/option at a time.

```proc sgplot data=total_dup ;

series x=d_VISITN y=total / group=d_SUBJID
;
xaxis label="Visit" values=(1 to 4 by 1);
yaxis label="Total" values=(0 to 5 by 1);
format  d_VISITN visit. domain_score \$score.;
run;
```

Then start adding ONE option at a time.

If you only have one color in the output then there are some possible issues related to data such as only one value of your group variable has values for the x and y variables in the ranges supplied on the XAXIS and YAXIS values= options.

Another possible is there is only one value for the group=variable.

Which is why we ask for example data.

Note that since the Styleattrs(datacontrastcolors)= option and the Grouplc= option are attempting to set colors for the same objects, the lines, when they conflict the results are unpredictable and especially without any actual data.

Discussion stats
• 3 replies
• 931 views
• 0 likes
• 2 in conversation