Data visualization with SAS programming

Unique combination of color and marker

Accepted Solution Solved
Reply
Regular Contributor
Posts: 180
Accepted Solution

Unique combination of color and marker

Hello all,

 

I have a relatively simple dataset, with 3 variables: SubjectID, Method1, Method2, where Method1 and Method2 are continuous.

 

I wish to plot something similar to the Bland-Altman plot, only that my agreement limits will be calculated in somewhat a different way. However the plot should be similar and should look roughly like this:

 

https://en.wikipedia.org/wiki/Bland%E2%80%93Altman_plot#/media/File:Bland-Alman_Plot_with_CI%27s_on_...

 

Assuming that I have calculated the mean (red line) and two limits (blue lines), I can either merge them into to the data or store them into macro variables using SQL or other way.

 

My question is however about the graph. In the x-axis I will put the mean of Method1 and Method2, and in the y-axis the diff between Method2 and Method1. I have 60 subjects, with repeated measurements for each, giving a total of thousands of data points. I am not sure how to make this graph clear. I thouhgt, for each subject, to plot all his/her points in a different combination of color and marker. 

 

I wanted to ask you how do I tell SAS to create a unique combination ? How do I put a legend below the graph, in a way that doesn't disturb? Does anyone has a better idea how to plot this graph?

 

I wish to use sgplots, to get the best quality plot I can, is it correct that I need to use refline once to create all 3 lines ?

 

Thank you in advance !


Accepted Solutions
Solution
‎02-16-2016 12:22 AM
Super Contributor
Posts: 252

Re: Unique combination of color and marker

Okay, here is an example. This one does plot the legend too, however in this case the legend is only 1 character. It's better to plot without the legend, and only label certain subjects if needed.

 

To add in the error bars just requires two more scatter statements.

 

data test;
  do i = 1 to 60;
  subject = i;
  m1 = rand("normal", 20, 2);
  m2 = m1 + rand("uniform");
  output;
  end;
run;


proc sgplot data = test;
  refline 10 30 / axis = y lineattrs = (color = red pattern = 2); * Reference line of limits;
  refline 20 / axis = y lineattrs = (color = blue); * Mean reference line;

  scatter x = m1 y = m2 / group = subject; 
  yaxis min = 0 max = 40; * yaxis range;
run;

View solution in original post


All Replies
Super Contributor
Posts: 252

Re: Unique combination of color and marker

Hello, just wondering how many plots do you want to have? Is it 1 or 60?

 

If it is one, then you can use the group option in sgplot to give you the unique combination of colors and markers? Although some may be repeated. Having a legend of 60 subjects will probably take up to much space though, so you could leave out the legend, and only label certain points (i.e. outliers). What version of SAS do you have please? Have you got an example dataset?

Regular Contributor
Posts: 180

Re: Unique combination of color and marker

It is one indeed. I am using SAS 9.4. I do not have data I can share. The values of Method1 and Method2 are somewhere between 6 and 20, with good correlation between the two.
Super User
Super User
Posts: 7,727

Re: Unique combination of color and marker

One one?  We have to plot grouped data with multiple timepoints, but I wouldn't put it all in one graph, just an example, but 60*10=600, thats going to be very crowded on a normal plot.   I would suggest looking at grouping by certain criteria, maybe subjects in the same treatment arm for comparison purposes (sorry, don't know your data so can't give a related example).  You could also produce a summarised overall table, mean/min/max say for each subject and just plot the 60 points.  

To get you started, there are many examples of graphs for all kinds of purpose given here:

http://blogs.sas.com/content/graphicallyspeaking/

 

Solution
‎02-16-2016 12:22 AM
Super Contributor
Posts: 252

Re: Unique combination of color and marker

Okay, here is an example. This one does plot the legend too, however in this case the legend is only 1 character. It's better to plot without the legend, and only label certain subjects if needed.

 

To add in the error bars just requires two more scatter statements.

 

data test;
  do i = 1 to 60;
  subject = i;
  m1 = rand("normal", 20, 2);
  m2 = m1 + rand("uniform");
  output;
  end;
run;


proc sgplot data = test;
  refline 10 30 / axis = y lineattrs = (color = red pattern = 2); * Reference line of limits;
  refline 20 / axis = y lineattrs = (color = blue); * Mean reference line;

  scatter x = m1 y = m2 / group = subject; 
  yaxis min = 0 max = 40; * yaxis range;
run;
Regular Contributor
Posts: 180

Re: Unique combination of color and marker

I ran your code on a small sample of the subjects, and the markers are all circles. Will SAS automatically change a marker when it runs out of colors ?
Super Contributor
Posts: 252

Re: Unique combination of color and marker

All the markers are circles? That shouldn't be the case. It should cycle through the colours and the markers. Are all the markers the same colour too? Please check you have not got this option: markerattrs = (symbol = circle)

 

If your only plotting one subject but have different timepoints, in that case or markers will be the same.

 

Also I believe you are plotting subjects and time because you're doing repeated measures? In that case you could use time as the symbol and subject as the colour. You will need a different solution to achieve this though. Let me know if this is what you're after?

Regular Contributor
Posts: 180

Re: Unique combination of color and marker

The colors are different. Now I ran the full data, and the markers started to change (probably only after the colors were over). It is a sort of repeated measures, it's bland and altman plot.
Super Contributor
Posts: 252

Re: Unique combination of color and marker

Okay, the default is that the markers and symbols will change for each distinct value in the group statement. If you have a lot of groups though, there is a chance that the markers and symbols can be repeated. Does the plot show you what you expect?

SAS Super FREQ
Posts: 1,148

Re: Unique combination of color and marker

If you are using HTML destination the style is HTMLBlue, which is a "Color Priority" style.  This means only color will change for the first 12 groups, while using same (first) symbol and pattern from the group style elements.  Then, at group 13, the symbol and pattern will change to the 2nd in the style, and all the colors will be cycled again.  

 

If you want colors, Symbols and Patterns to change per group, use a style with ATTRPRIORITY=NONE like LISTING.  Or, add this option on the ODS Graphics statement.

 

See:  http://blogs.sas.com/content/graphicallyspeaking/2015/06/28/attributes-priority-for-the-inquiring-mi...

 

Regular Contributor
Posts: 180

Re: Unique combination of color and marker

The graph is very "crowded", it is difficult to distinguish between subjects, however I still find it informative. I have another question. I used the refline for the horizontal lines. The values of the y-axis for the lines are stored in macro variables, and have many decimal digits. I wish to maintain the level of accuracy, but to present a shorter number on the graph itself.

My current line is:

refline &Lower &Upper / axis = y lineattrs = (color = red pattern = 2 thickness = 3) label=("Lower Limit = &&Lower" "Upper Limit = &&Upper") ;

Instead of having for example 3.234756576 printed on the graph, I wish to see 3.235, without rounding before plotting. Is it possible ?
Regular Contributor
Posts: 180

Re: Unique combination of color and marker

I have tried the following, which I found on a SAS SUGI, but it did not work:

%MACRO CHNGFMT(INVAR,INFMT);
%LET &INVAR = %SYSFUNC(INPUTN(&&&INVAR,&INFMT));
%MEND;

%CHNGFMT(Mean,f8.2);
Super Contributor
Posts: 252

Re: Unique combination of color and marker

You can create another macro variable that is rounded to what you want, and then afterwards you can use the label statement in refline to show the formatted value.

 

The macro variable can be rounded to the number of decimal places you want by using the picture format.

 

There could be a quicker way to do this, but this way works too.

 

Here is an example.

 

data test;
  do i = 1 to 60;
    do j= 1 to 2;
      subject = i;
      m1 = rand("normal", 20, 2);
      m2 = m1 + rand("uniform");
      output;
    end;
  end;
run;

 

* Creating picture format;


proc format;
  picture reffmt (round)
  0-9 = 9.999
  10-99 = 99.999
  100-999 = 999.9999
  1000-9999 = 9999.999
  10000-99999 = 99999.999;
run;


%let refline1 = 20.52135467;
%let refline1fmt = %sysfunc(putn(&refline1, reffmt.));


proc sgplot data = test;
  refline 10 30 / axis = y lineattrs = (color = red pattern = 2); * Reference line of limits;
  refline &refline1. / axis = y lineattrs = (color = blue) label="&refline1fmt."; * Mean reference line;
  scatter x = m1 y = m2 / group = subject;
  yaxis min = 0 max = 40; * yaxis range;
run;

☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 655 views
  • 5 likes
  • 4 in conversation