Data visualization with SAS programming

Color coding data points by third variable in sgplot?

Reply
Occasional Contributor
Posts: 15

Color coding data points by third variable in sgplot?

I am trying to create a plot using sgplot and scatter that will color code data points based on the value of an endogenous variable. I used to do this through SAS Insight, but that is gone.  I thought I could do it through attribute maps, but I am getting errors.  In the code below I am plotting 6 values (x,y pairs) color coding by the value of x.  Is there a better/easier way to do this.  Please excuse the rough state of the code.

SAS 9.4 64 bit TS1M1

Windows 7 under Mac Book Pro (Yosemite 10.10.3): Parallels 10.2.1

Thanks for any help on this.

Bill

data attrib (drop=x red green blue) ;

  format red green blue hex2. ;

  length linecolor $8 fillcolor $8;

  input @1 x  @1 value$ y;

  ID = 'Test';

  red = int((x/48)*255);

  green = 0;

  blue=255 - red;

  fillcolor= cats('CX',put(red, hex2.),put(green, hex2.),put(blue, hex2.));

  linecolor='Black';

  cards;

  1.5 64

  3 57

  6 48

  12 29

  24 13

  48 2

  ;

run;

data trial ;

  input x y;

  cards;

  1.5 64

  3 57

  6 48

  12 29

  24 13

  48 2

  ;

run;

proc sgplot  data=trial DATTRMAP=attrib;

  scatter x=x y=y/FILLEDOUTLINEDMARKERS markerattrs=(symbol=circlefilled size=10) group=x ATTRID=Test;

run;


Occasional Contributor
Posts: 15

Re: Color coding data points by third variable in sgplot?

Update:

I managed to get this to work by:

1) Removing the FILLEDOUTLINEDMARKERS option

2) Changing the attribute variable name "fillcolor" to "MarkerColor".

Is there a list of keyword for attribute map variables?

Also, what if I still wanted outlined markers?

Thanks again,

Bill

SAS Super FREQ
Posts: 8,719

Re: Color coding data points by third variable in sgplot?

Hi:

  There is a section in the ODS Graphics doc for the SG Procedures that discusses Attribute Maps and shows the list of reserved variable names:

SAS(R) 9.4 ODS Graphics: Procedures Guide, Fourth Edition (the specific link with reserved variables)

and

SAS(R) 9.4 ODS Graphics: Procedures Guide, Fourth Edition (the top link that shows all the sections)

cynthia

Contributor
Posts: 64

Re: Color coding data points by third variable in sgplot?

Hi Bill,

Do you have Mock, that will be helpful to us to give correct options can be used.

Yes if you need symbols to present, you have to use markercolor.

Thanks,'

Durga.

SAS Super FREQ
Posts: 1,080

Re: Color coding data points by third variable in sgplot?

What you need is a color response based on a variable. 

COLORRESPONSE is supported for both SCATTER and BUBBLE starting from SAS 9.4(TS1M2).  You are using TS1M1, so that option is not available.  However, these options are available in GTL in TS1M1. 

An easy way to get started would be to run the SGPLOT procedure as you have, and set TMPLOUT="FileName.sas"  SGPLOT will write the generated GTL template program to this file name.  Template name will be "SGPLOT".  Go to the ScatterPlot statement as add the option MARKERCOLORGRADIENT=variable.  You can also use the COLORMODEL option to get the colors you want, or use DISCERTEATTRMAP to get full control on colors and values.

You will need to use the following statement to run the graph from template. 

proc sgrender data=foo template=sgplot;

run;

Occasional Contributor
Posts: 15

Re: Color coding data points by third variable in sgplot?

Sanjay and others,

        Thank you very much for your help with this. Excellent answers as usual.  I will also look into upgrading to TS1M2.

Bill Price

Ask a Question
Discussion stats
  • 5 replies
  • 582 views
  • 3 likes
  • 4 in conversation