Hello,
I have the following sample data pchg has both positive and negative values ranging from -100 to 1000. I wanted plot a spaghetti plot and to have legend as cohort and each cohort has unique color irrespective of number of subjects in it. I am using the following code but unable to do coloring by cohort, is there any option in series line group = id, cohort. or is there any other option plot by id but color code by cohort and legend to be cohort.
proc sgplot data=dta;
format visitc $visitfmt.;
series x=visitc y=pchg / group=id lineattrs=(pattern=solid thickness=1) break markers markerattrs=(symbol=circlefilled size=1.5);
xaxis type=discrete label='Visits' offsetmax=0.02 offsetmin=0.02;
yaxis label='Percent Change' ;
run;
circlefilled option is not getting applied and hard to read this graph.
Use the GROUPLC= option to specify that the line colors should be set by using the COHORT variable instead of the ID variable:
proc sgplot data=dta;
*format visitc $visitfmt.;
series x=visitc y=pchg / group=id GROUPLC=COHORT
lineattrs=(pattern=solid thickness=1)
break markers markerattrs=(symbol=circlefilled size=2);
xaxis type=discrete label='Visits' offsetmax=0.02 offsetmin=0.02;
yaxis label='Percent Change' ;
keylegend / type=linecolor title="";
run;
You can also use the TYPE=LINECOLOR option on the KEYLEGEND statement to add a legend for the cohorts.
You'll probably want to control the colours manually in this case.
You can use a data attribute map to assign colours.
If you're not sure of what colours to use, I would suggest getting a set of 12 different colours from here and assign them as needed.
https://colorbrewer2.org/#type=qualitative&scheme=Paired&n=12
Use the GROUPLC= option to specify that the line colors should be set by using the COHORT variable instead of the ID variable:
proc sgplot data=dta;
*format visitc $visitfmt.;
series x=visitc y=pchg / group=id GROUPLC=COHORT
lineattrs=(pattern=solid thickness=1)
break markers markerattrs=(symbol=circlefilled size=2);
xaxis type=discrete label='Visits' offsetmax=0.02 offsetmin=0.02;
yaxis label='Percent Change' ;
keylegend / type=linecolor title="";
run;
You can also use the TYPE=LINECOLOR option on the KEYLEGEND statement to add a legend for the cohorts.
Thank you very much Rick. This is working but somehow circle filled is not populating in the figure. Do you have any suggestions please.
use SIZE=6 instead of SIZE=2.
Great, this is working great. Thank you so much Rick.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.