I generated spaghetti plots using the series statement in proc sgplot to show values over time for each patient (coming from 2 cohorts). I am repeating the same plots by cohort and would like the patients to have same colors as in the overall plot. How can I do that? Here's my code
**PLOT1;
proc sgplot;
series x= visit y=heartrate/group=patientID;
run;
* Displays values for patients 1, 2, 3, 4, 5, 6, 7 in colors red, blue, green, orange, cyan, black, brown.
* Repeating by treatment;
*PLOT2;
proc sgplot;
series x= visit y=heartrate/group=patientID;
by treatment;
run;
Patients 1, 2, 5, 7 are in treatment 1 and remaining in treatment 2. How can I make sure that the colors assigned to each patient is same as in the "PLOT1"?
Thanks so much!
In general, sgplot assigns colors in the order the group= values are encountered in the dataset. Therefore you can usually sort the dataset by the group= values to get a consistent order.
But as reeza said - the best way to guarantee your color assignments are consistent is to use an attribute map.
Thank you! I will try that
In general, sgplot assigns colors in the order the group= values are encountered in the dataset. Therefore you can usually sort the dataset by the group= values to get a consistent order.
But as reeza said - the best way to guarantee your color assignments are consistent is to use an attribute map.
Thank you so much, this worked great for me! I found this easiest to try and implement
I think there is an easier way. Instead of using PROC SGPLOT with a BY statement, switch to PROC SGPANEL and use the PANELBY statement. An example of spaghetti plots using this method is shown halfway through the article, "Create spaghetti plots in SAS."
If you want to use PROC SGPLOT, you can either
Thank you, I tried this now and panelby does work nicely. Is it also possible to include the entire cohort as one of the panel plots (in addition to one panel plot for each by group)?
Sure. Just use PROC SGPLOT without using the BY statement.
Thank you
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.