I am trying to create a spaghetti plot using PROC SGPLOT.
I am using Subject ID as the group variable. I want to display
different markers for each subjects. In the current plot all the subjects have
same markers. I would different markers. How can this be achieved ?
Here is the very simple starting code...
proc sgplot data = lab ;
series x = weeks y = lbstresn / markerattrs =(symbol=circlefilled )group = subjid ;
yaxis type = log logstyle = logexpand display = all;
xaxis values= (0 to 30 by 1 );
run;
What version of SAS are you running? In SAS 9.4m2 you can use the GROUPLC=, GROUPLP=, GROUPMC=, and GROUPMS= options to specify the colors and patterns of lines and the colors and symbols of markers, respectively.
For your example, I think you can write:
series x=weeks y=lbsresn / group=subjid groupms=subjid markers;
Of course, if you have more than about 20 subjects, you will run out of markers.
For more on spaghetti plots in SAS see
Create spaghetti plots with SGPLOT
What version of SAS are you running? In SAS 9.4m2 you can use the GROUPLC=, GROUPLP=, GROUPMC=, and GROUPMS= options to specify the colors and patterns of lines and the colors and symbols of markers, respectively.
For your example, I think you can write:
series x=weeks y=lbsresn / group=subjid groupms=subjid markers;
Of course, if you have more than about 20 subjects, you will run out of markers.
For more on spaghetti plots in SAS see
Create spaghetti plots with SGPLOT
Thank you. GROUPMC options works.
Great! Just to be clear, GROUPMC= changes the color. GROUPMS= changes the symbol.
Hi Rick,
I was wondering if you could please help me with this request. I'm trying to get the cumulative incidence function (CIF) by cognitive status. I would like to have different line patterns for the 3 cognitive categories. However, I can't make them as DASH, SOLID or LONGDASH. I just get different colors but the journal would like to see different line patterns when printed out in black and white.
I do have SAS 9.4 M3, so I thought the grouplp would help but it is not even recognized (grouplp doesn't become green in SAS). Ultimately, I would also like the legend to show different line patterns for each CIF curve. As of now, it is only showing different colors.
With grouplp and keylegend "step"/type=linepattern added to the code, SAS gives me an error message. (see code below).
proc sgplot data=curvecox_t;
yaxis max=.25; xaxis max=10;
step x=FLUPYEARS y=CIF / group = COGNITIVE_STATUS grouplp=COGNITIVE_STATUS markers lineattrs = (thickness = 1)
markerattrs=(symbol=circlefilled size = 4) name="series";
keylegend "step"/type=linepattern ;
run;
Without grouplp and keylegend "step"/type=linepattern ; in the code, SAS runs fine albeit not giving me the line patterns I want.
Thanks very much for your time and help Rick!
Tien
Just because the editor displays a statement in red does NOT mean that the statement is invalid. Try it and see if it works.
I suggest you use the "journal" style. Tthe doc has two good references:
Styles in ODS Statistical Graphics
SAS/STAT Statistical Graphics Using ODS
Thank you for getting back to me Rick,
I tried running the code but it wouldn't run.
Here is my data. I'm trying to produce CIF curves for different cognitive category (dementia, mild, normal) by follow up time. I would like to get the curves as DASH, SOLID, LONGDASH for these 3 curves. However, as of now, I can only produce 3 curves with 3 different colors, which is not what I want. I have pasted my code below. If you could help me with the request, that would be great.
Thank you so much for your help!
data curvecox_t;
input COGNITIVE_STATUS $ FLUPYEARS CIF;
datalines;
DEMENTIA 0 0
DEMENTIA 0.63527 0.0041
DEMENTIA 0.69551 0.00825
DEMENTIA 0.94469 0.0125
DEMENTIA 1.00219 0.01682
DEMENTIA 1.1391 0.02119
DEMENTIA 1.3253 0.02562
DEMENTIA 1.35816 0.03006
DEMENTIA 1.40745 0.03453
DEMENTIA 1.454 0.039
DEMENTIA 1.63472 0.04358
DEMENTIA 1.82092 0.0482
DEMENTIA 1.85378 0.05282
DEMENTIA 2.0701 0.05765
DEMENTIA 2.38773 0.06261
DEMENTIA 3.23658 0.06847
DEMENTIA 3.63089 0.0749
DEMENTIA 3.85268 0.08175
DEMENTIA 4.33461 0.0898
DEMENTIA 4.592 0.09831
DEMENTIA 4.67415 0.10724
DEMENTIA 4.95893 0.11684
DEMENTIA 7.57941 0.13838
DEMENTIA 8.63636 0.18625
MILDCOGNITIVEIMPAIRMENT 0 0
MILDCOGNITIVEIMPAIRMENT 0.04381 0.00286
MILDCOGNITIVEIMPAIRMENT 0.07941 0.00572
MILDCOGNITIVEIMPAIRMENT 0.20263 0.0086
MILDCOGNITIVEIMPAIRMENT 0.48467 0.01148
MILDCOGNITIVEIMPAIRMENT 0.66265 0.01439
MILDCOGNITIVEIMPAIRMENT 0.69825 0.01732
MILDCOGNITIVEIMPAIRMENT 0.85433 0.0203
MILDCOGNITIVEIMPAIRMENT 0.94195 0.02328
MILDCOGNITIVEIMPAIRMENT 1.00493 0.02628
MILDCOGNITIVEIMPAIRMENT 1.26232 0.02929
MILDCOGNITIVEIMPAIRMENT 1.41566 0.03238
MILDCOGNITIVEIMPAIRMENT 1.44031 0.03547
MILDCOGNITIVEIMPAIRMENT 1.83461 0.03876
MILDCOGNITIVEIMPAIRMENT 2.02629 0.04213
MILDCOGNITIVEIMPAIRMENT 2.08653 0.04553
MILDCOGNITIVEIMPAIRMENT 2.10022 0.04893
MILDCOGNITIVEIMPAIRMENT 2.30832 0.05237
MILDCOGNITIVEIMPAIRMENT 2.42059 0.05587
MILDCOGNITIVEIMPAIRMENT 2.85871 0.05964
MILDCOGNITIVEIMPAIRMENT 2.97371 0.06345
MILDCOGNITIVEIMPAIRMENT 3.21468 0.06732
MILDCOGNITIVEIMPAIRMENT 3.36254 0.07126
MILDCOGNITIVEIMPAIRMENT 3.42278 0.07523
MILDCOGNITIVEIMPAIRMENT 4.06079 0.07959
MILDCOGNITIVEIMPAIRMENT 4.17579 0.08399
MILDCOGNITIVEIMPAIRMENT 4.20865 0.08842
MILDCOGNITIVEIMPAIRMENT 4.51533 0.09302
MILDCOGNITIVEIMPAIRMENT 4.68784 0.09777
MILDCOGNITIVEIMPAIRMENT 4.76451 0.10262
MILDCOGNITIVEIMPAIRMENT 4.86309 0.10758
MILDCOGNITIVEIMPAIRMENT 5.0931 0.11292
MILDCOGNITIVEIMPAIRMENT 5.09858 0.11827
MILDCOGNITIVEIMPAIRMENT 5.4874 0.12403
MILDCOGNITIVEIMPAIRMENT 6.19113 0.13057
MILDCOGNITIVEIMPAIRMENT 6.27875 0.13715
MILDCOGNITIVEIMPAIRMENT 7.96002 0.14768
NORMAL 0 0
NORMAL 0.17251 0.00115
NORMAL 0.19989 0.0023
NORMAL 0.26835 0.00346
NORMAL 0.36692 0.00462
NORMAL 0.51205 0.00578
NORMAL 0.56955 0.00694
NORMAL 0.59967 0.0081
NORMAL 0.78587 0.00927
NORMAL 0.79135 0.01045
NORMAL 0.86254 0.01162
NORMAL 0.90909 0.0128
NORMAL 1.21303 0.014
NORMAL 1.26506 0.0152
NORMAL 1.31161 0.01641
NORMAL 1.34995 0.01762
NORMAL 1.61008 0.01885
NORMAL 1.64567 0.02008
NORMAL 1.67853 0.02131
NORMAL 1.70865 0.02254
NORMAL 2.03998 0.0238
NORMAL 2.30559 0.02508
NORMAL 2.36583 0.02637
NORMAL 2.51369 0.02767
NORMAL 2.54655 0.02897
NORMAL 2.5575 0.03028
NORMAL 2.59858 0.03159
NORMAL 2.65882 0.0329
NORMAL 2.73823 0.03422
NORMAL 2.75465 0.03554
NORMAL 2.7793 0.03687
NORMAL 2.93264 0.0382
NORMAL 2.96824 0.03954
NORMAL 3.1736 0.0409
NORMAL 3.2092 0.04226
NORMAL 3.39814 0.04364
NORMAL 3.41731 0.04503
NORMAL 3.67196 0.04643
NORMAL 3.75958 0.04785
NORMAL 3.78149 0.04927
NORMAL 3.83899 0.0507
NORMAL 4.03341 0.05216
NORMAL 4.04984 0.05363
NORMAL 4.1402 0.05511
NORMAL 4.23604 0.05661
NORMAL 4.3264 0.05812
NORMAL 4.37568 0.05964
NORMAL 4.53998 0.06119
NORMAL 4.55641 0.06274
NORMAL 4.57831 0.06429
NORMAL 4.58653 0.06585
NORMAL 4.61939 0.06741
NORMAL 4.68237 0.06897
NORMAL 4.96714 0.07058
NORMAL 5.09584 0.07221
NORMAL 5.13417 0.07386
NORMAL 5.19168 0.0755
NORMAL 5.53943 0.0772
NORMAL 5.82147 0.07896
NORMAL 5.87349 0.08073
NORMAL 6.01862 0.08252
NORMAL 6.03505 0.08431
NORMAL 6.14732 0.08615
NORMAL 6.72508 0.08812
NORMAL 6.7552 0.09009
NORMAL 6.78806 0.09208
NORMAL 6.83461 0.09408
NORMAL 7.00986 0.09613
NORMAL 7.04545 0.09819
NORMAL 7.12486 0.10026
NORMAL 7.15772 0.10233
NORMAL 7.17963 0.10442
NORMAL 7.22618 0.10652
NORMAL 7.38499 0.10866
NORMAL 7.51643 0.11086
NORMAL 7.54655 0.11307
NORMAL 7.7793 0.1154
NORMAL 8.17634 0.11807
NORMAL 8.81161 0.12276
NORMAL 8.81982 0.12753
NORMAL 9.06627 0.1348
;
run;
*This code produces the plot but with solid line patter, different colors. Not what I want. I would like 3 curves with different line patterns like dash, long dash and solid*;
proc sgplot data=curvecox_t;
yaxis max=.25; xaxis max=10;
step x=FLUPYEARS y=CIF / group = COGNITIVE_STATUS ;
run;
*This code specifying grouplp didn't even run*;
proc sgplot data=curvecox_t;
yaxis max=.25; xaxis max=10;
step x=FLUPYEARS y=CIF / group = COGNITIVE_STATUS grouplp=COGNITIVE_STATUS markers lineattrs = (thickness = 1)
markerattrs=(symbol=circlefilled size = 4) name="series";
keylegend "step"/type=linepattern ;
run;
The key point to note here is that you are likely using HTML output with the HTMLBlue style. This is an ATTRPRIORITY=COLOR style, which means that only colors are cycled first holding line pattern and symbol to the first one till the 12 colors are exhausted. Then, the line pattern and symbol shift to the next value. Most other styles are ATTRPRIORITY=NONE, including JOURNAL, LISTING and so on. These styles cause the color, line pattern and symbols to be cycled together.
You can change this behavior for any style by setting ATTRPRIORITY in the ODS GRAPHICS statement. This will be applicable till the this is changed.
Thank you for your help. But please see my SAS code for what I want to accomplish. I just want to have three curves with different line patterns instead of 3 solid lines. It would be great if you could help me with this request. Thank you!
data curvecox_t;
input COGNITIVE_STATUS $ FLUPYEARS CIF;
datalines;
DEMENTIA 0 0
DEMENTIA 0.63527 0.0041
DEMENTIA 0.69551 0.00825
DEMENTIA 0.94469 0.0125
DEMENTIA 1.00219 0.01682
DEMENTIA 1.1391 0.02119
DEMENTIA 1.3253 0.02562
DEMENTIA 1.35816 0.03006
DEMENTIA 1.40745 0.03453
DEMENTIA 1.454 0.039
DEMENTIA 1.63472 0.04358
DEMENTIA 1.82092 0.0482
DEMENTIA 1.85378 0.05282
DEMENTIA 2.0701 0.05765
DEMENTIA 2.38773 0.06261
DEMENTIA 3.23658 0.06847
DEMENTIA 3.63089 0.0749
DEMENTIA 3.85268 0.08175
DEMENTIA 4.33461 0.0898
DEMENTIA 4.592 0.09831
DEMENTIA 4.67415 0.10724
DEMENTIA 4.95893 0.11684
DEMENTIA 7.57941 0.13838
DEMENTIA 8.63636 0.18625
MILDCOGNITIVEIMPAIRMENT 0 0
MILDCOGNITIVEIMPAIRMENT 0.04381 0.00286
MILDCOGNITIVEIMPAIRMENT 0.07941 0.00572
MILDCOGNITIVEIMPAIRMENT 0.20263 0.0086
MILDCOGNITIVEIMPAIRMENT 0.48467 0.01148
MILDCOGNITIVEIMPAIRMENT 0.66265 0.01439
MILDCOGNITIVEIMPAIRMENT 0.69825 0.01732
MILDCOGNITIVEIMPAIRMENT 0.85433 0.0203
MILDCOGNITIVEIMPAIRMENT 0.94195 0.02328
MILDCOGNITIVEIMPAIRMENT 1.00493 0.02628
MILDCOGNITIVEIMPAIRMENT 1.26232 0.02929
MILDCOGNITIVEIMPAIRMENT 1.41566 0.03238
MILDCOGNITIVEIMPAIRMENT 1.44031 0.03547
MILDCOGNITIVEIMPAIRMENT 1.83461 0.03876
MILDCOGNITIVEIMPAIRMENT 2.02629 0.04213
MILDCOGNITIVEIMPAIRMENT 2.08653 0.04553
MILDCOGNITIVEIMPAIRMENT 2.10022 0.04893
MILDCOGNITIVEIMPAIRMENT 2.30832 0.05237
MILDCOGNITIVEIMPAIRMENT 2.42059 0.05587
MILDCOGNITIVEIMPAIRMENT 2.85871 0.05964
MILDCOGNITIVEIMPAIRMENT 2.97371 0.06345
MILDCOGNITIVEIMPAIRMENT 3.21468 0.06732
MILDCOGNITIVEIMPAIRMENT 3.36254 0.07126
MILDCOGNITIVEIMPAIRMENT 3.42278 0.07523
MILDCOGNITIVEIMPAIRMENT 4.06079 0.07959
MILDCOGNITIVEIMPAIRMENT 4.17579 0.08399
MILDCOGNITIVEIMPAIRMENT 4.20865 0.08842
MILDCOGNITIVEIMPAIRMENT 4.51533 0.09302
MILDCOGNITIVEIMPAIRMENT 4.68784 0.09777
MILDCOGNITIVEIMPAIRMENT 4.76451 0.10262
MILDCOGNITIVEIMPAIRMENT 4.86309 0.10758
MILDCOGNITIVEIMPAIRMENT 5.0931 0.11292
MILDCOGNITIVEIMPAIRMENT 5.09858 0.11827
MILDCOGNITIVEIMPAIRMENT 5.4874 0.12403
MILDCOGNITIVEIMPAIRMENT 6.19113 0.13057
MILDCOGNITIVEIMPAIRMENT 6.27875 0.13715
MILDCOGNITIVEIMPAIRMENT 7.96002 0.14768
NORMAL 0 0
NORMAL 0.17251 0.00115
NORMAL 0.19989 0.0023
NORMAL 0.26835 0.00346
NORMAL 0.36692 0.00462
NORMAL 0.51205 0.00578
NORMAL 0.56955 0.00694
NORMAL 0.59967 0.0081
NORMAL 0.78587 0.00927
NORMAL 0.79135 0.01045
NORMAL 0.86254 0.01162
NORMAL 0.90909 0.0128
NORMAL 1.21303 0.014
NORMAL 1.26506 0.0152
NORMAL 1.31161 0.01641
NORMAL 1.34995 0.01762
NORMAL 1.61008 0.01885
NORMAL 1.64567 0.02008
NORMAL 1.67853 0.02131
NORMAL 1.70865 0.02254
NORMAL 2.03998 0.0238
NORMAL 2.30559 0.02508
NORMAL 2.36583 0.02637
NORMAL 2.51369 0.02767
NORMAL 2.54655 0.02897
NORMAL 2.5575 0.03028
NORMAL 2.59858 0.03159
NORMAL 2.65882 0.0329
NORMAL 2.73823 0.03422
NORMAL 2.75465 0.03554
NORMAL 2.7793 0.03687
NORMAL 2.93264 0.0382
NORMAL 2.96824 0.03954
NORMAL 3.1736 0.0409
NORMAL 3.2092 0.04226
NORMAL 3.39814 0.04364
NORMAL 3.41731 0.04503
NORMAL 3.67196 0.04643
NORMAL 3.75958 0.04785
NORMAL 3.78149 0.04927
NORMAL 3.83899 0.0507
NORMAL 4.03341 0.05216
NORMAL 4.04984 0.05363
NORMAL 4.1402 0.05511
NORMAL 4.23604 0.05661
NORMAL 4.3264 0.05812
NORMAL 4.37568 0.05964
NORMAL 4.53998 0.06119
NORMAL 4.55641 0.06274
NORMAL 4.57831 0.06429
NORMAL 4.58653 0.06585
NORMAL 4.61939 0.06741
NORMAL 4.68237 0.06897
NORMAL 4.96714 0.07058
NORMAL 5.09584 0.07221
NORMAL 5.13417 0.07386
NORMAL 5.19168 0.0755
NORMAL 5.53943 0.0772
NORMAL 5.82147 0.07896
NORMAL 5.87349 0.08073
NORMAL 6.01862 0.08252
NORMAL 6.03505 0.08431
NORMAL 6.14732 0.08615
NORMAL 6.72508 0.08812
NORMAL 6.7552 0.09009
NORMAL 6.78806 0.09208
NORMAL 6.83461 0.09408
NORMAL 7.00986 0.09613
NORMAL 7.04545 0.09819
NORMAL 7.12486 0.10026
NORMAL 7.15772 0.10233
NORMAL 7.17963 0.10442
NORMAL 7.22618 0.10652
NORMAL 7.38499 0.10866
NORMAL 7.51643 0.11086
NORMAL 7.54655 0.11307
NORMAL 7.7793 0.1154
NORMAL 8.17634 0.11807
NORMAL 8.81161 0.12276
NORMAL 8.81982 0.12753
NORMAL 9.06627 0.1348
;
run;
*This produces 3 curves with solid lines of different colors. But i would like three curves of different line patterns like dash , solid, long dash*;
proc sgplot data=curvecox_t;
yaxis max=.25; xaxis max=10;
step x=FLUPYEARS y=CIF / group = COGNITIVE_STATUS ;
run;
*This code didn't even work*;
proc sgplot data=curvecox_t;
yaxis max=.25; xaxis max=10;
step x=FLUPYEARS y=CIF / group = COGNITIVE_STATUS grouplp=COGNITIVE_STATUS markers lineattrs = (thickness = 1)
markerattrs=(symbol=circlefilled size = 4) name="series";
keylegend "step"/type=linepattern ;
run;
Please look at the doc for the STEP statement and notice that the GROUPLP= option is not a valid option.
To get the journal style, put the following statement before your calls to SGPLOT:
ods html style=journal; /* or whatever ODS destination you are using */
If you want colors, you can use the following instead:
ods graphics / attrpriority=none;
Thank you Rick for your help! ods html style=journal; seems to be doing the trick.
But when I do ods graphics / attrpriority=none; I don't get colors. Why is this? Thanks!
Because the Journal style does not use colors. To get colors, you have to use a style that uses colors:
ods html style=HTMLBlue;
ods graphics / attrpriority=none;
PS. I linked to the documentation before, but clearly you did not read it, so here it is again. Please read:
Thank you so much Rick!!! Sorry I just quickly browsed through and missed it.
It worked now!
Your help and time is much appreciated!
Tien
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.