BookmarkSubscribeRSS Feed
kelloggs
Fluorite | Level 6

Hi everybody,

I am trying to create a scatterplot X~Y with two different groups, each group should have a distinct color (Group 1 = Red, Group 2 = Blue) as well as a groups-specific gradient coloring based on a third variable Z (Group 1: Light red to dark red, Group 2: Light blue to dark blue).

So far I succeeded in creating either

- a scatterplot with the groups colored in red / blue (using proc sgrender with "scatterplot / group = Z")

OR

- a scatterplot with a single gradient coloring for both groups:

proc template

/* colors for gradient coloring*/

define style styles.gradient;

parent = styles.default;

  style GraphColors /

   'gconramp3cstart'   = "light red"

   'gconramp3cneutral' = "red"

   'gconramp3cend'     = "dark red"

end

/* Scatterplot with a single gradient coloring */

define statgraph gradientscatter;

  begingraph;

   layout overlay;        

   scatterplot x = X1 y = Y1 / markercolorgradient = Z 

   name = "scattergrad";

   scatterplot x = X2 y = Y2 / markercolorgradient = Z 

   name = "scattergrad";

   endlayout;

  endgraph;

end;

run;


/* Draw Scatterplot */

ods html style = styles.gradient;

proc sgrender data = &indata. template = gradientscatter; run;

Anybody got an idea how this could be done?

Since I need to plot  some 100k observations, I believe proc sgrender is the way to go, but of course I'd happily accept any other solution using ODS graphics.

Thanks!

Regards

3 REPLIES 3
DanH_sas
SAS Super FREQ

Try something like this and see if it works for you:

proc template

/* colors for gradient coloring*/

define style styles.gradient;

parent = styles.default;

   class ThreeColorRamp /

      StartColor = "light red"

      NeutralColor = "red"

      EndColor = "dark red"

   ;

   class ThreeColorAltRamp /

      StartColor = "light blue"

      NeutralColor = "blue"

      EndColor = "dark blue"

   ;

end

/* Scatterplot with a single gradient coloring */

define statgraph gradientscatter;

  begingraph;

   layout overlay;        

   scatterplot x = X1 y = Y1 / markercolorgradient = Z 

   name = "scattergrad" colormodel=ThreeColorRamp;

   scatterplot x = X2 y = Y2 / markercolorgradient = Z 

   name = "scattergrad" colormodel=ThreeColorAltRamp;

   endlayout;

  endgraph;

end;

run;


/* Draw Scatterplot */

ods html style = styles.gradient;

proc sgrender data = &indata. template = gradientscatter; run;

kelloggs
Fluorite | Level 6

Thanks for the quick reply!

Code looks good, but unfortunately I get the following error:

error.jpg

Looks like the color arguments are not accepted, any idea what the issue could be?

I also tried the following, which does run without an error, and the plot gets generated,but without any kind of color gradient:

proc template

define style styles.gradient;

parent = styles.default;

class ThreeColorRamp /

  endcolor=CXD3D3D3

  startcolor=CXFFDAB9

  neutralcolor=CXF0E68C

;


class ThreeColorAltRamp /

  endcolor=CXD3D3D3

  startcolor=CXFFDAB9

  neutralcolor=CXF0E68C

;

class graphbackground / color=white; end

define statgraph gradientscatter;

begingraph;

entrytitle "&title.";

  layout overlay;
   scatterplot x = volat_below y = perf_below/ markercolorgradient = Z 

   name = "scattergrad"

   colormodel = ThreeColorRamp;


   scatterplot x = volat_above y = perf_above / markercolorgradient = Z 

   name = "scattergrad"

   colormodel = ThreeColorAltRamp;

  endlayout;

endgraph;

end;

run;


ods html style = styles.gradient;

proc sgrender data = &indata. template = gradientscatter;

run;

A couple of things I forgot to mention in the first post:

- I'm on SAS 9.3.

- The final plot should have 5 different groups. The sample I provided has only 2 groups for simplicity's sake.

  I'm mentioning this because I noticed that there seems to be only a 'ThreeColorRamp' and a 'ThreeColorAltRamp' , which may imply that only 2 different gradients can be used?

Regards

kelloggs
Fluorite | Level 6


I just found the solution - the key is using "rangeattrmap":

template

define style styles.gradient;

parent = styles.default;


define statgraph gradientscatter;

begingraph;

  entrytitle "&title.";


  rangeattrmap name="density_below" ;

   range min - 5      / rangealtcolor=yellow;

   range 5  < - 20  / rangealtcolor=orange;

   range 20  < - max    /  rangealtcolor=red;

  endrangeattrmap ;

  rangeattrvar attrvar=rangevar_below var=n_below attrmap="density_below" ;


  rangeattrmap name="density_above" ;

   range 0 - 2      / rangealtcolor=skyblue;

   range 2  < - 4  / rangealtcolor=royalblue;

   range 4  < - max    / rangealtcolor=black;

  endrangeattrmap ;

  rangeattrvar attrvar=rangevar_above var=n_above attrmap="density_above" ;


  layout overlay;

   scatterplot x=x_below y=y_below/ markercolorgradient=rangevar_below;

   scatterplot x=x_above y=y_above/ markercolorgradient=rangevar_above;

  endlayout;

endgraph;

end;

run;


ods html style = styles.gradient;

proc sgrender data = &indata. template = gradientscatter; run;

Thanks for the input.

Regards

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2082 views
  • 0 likes
  • 2 in conversation