BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NKormanik
Barite | Level 11

Time to get fancy.  Color-coded third variable would make a particular visual assessment a lot easier.  Say, ramp from Blue to Red, based on a third continuous variable.  (Way too many groups to use Groups=.  And too many different datasets with different numbers, so need as much generalizability as possible.)

 

Scouting the Internet I see various possibilities, but all seem seriously complicated.

 

Rick Wicklin's post looks promising.  But how to translate to my need, is the question.

 

http://blogs.sas.com/content/iml/2014/03/26/color-scatter-plot-markers-by-a-third-variable.html

 

Assume that I have the following, thus far:

 

proc sgplot data=Have;
format N f2.0; scatter x=x y=y / markerchar=N markercharattrs=(size=1); run;

Thanks to DanH for that.

 

With that as starting point, is there a relatively easy way to get the N variable numbers on the plot to show in color as well, ramped from Blue to Red?

 

Assume SAS 9.4.

 

Any help greatly appreciated.

 

Nicholas Kormanik

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Whenever you have a question like this, you should look at the doc. The doc for the SCATTER statement gives the answer on the third line:

COLORRESPONSE= specifies the numeric column that is used to map colors to a gradient legend

 

For example:

proc sgplot data=sashelp.class;
scatter x=height y=weight / markerchar=Name colorresponse=age;
run;

If you want to change the colors used for the color ramp, you can use the COLORMODEL= option to choose from a variety of pre-built ramps, or create your own:

proc sgplot data=sashelp.class;
scatter x=height y=weight / markerchar=Name colorresponse=age
   colormodel=(blue green orange red); /* custom 4-color ramp */
run;

The COLORRESPONSE= and COLORMODEL= options were added to the SCATTER statement in SAS 9.m2 (??).

It was added to other statements (like DOT and SERIES) in SAS 9.4m3. 

 

You can also use the TEXTPLOT statement in SAS 9.4m2. Sanjay wrote a great blog that has many examples, including chaging size and color.

 

For older versions of SAS, you can read this article about using the Graph Template Language (GTL) to color markers by a continuous variable.

View solution in original post

3 REPLIES 3
Rick_SAS
SAS Super FREQ

Whenever you have a question like this, you should look at the doc. The doc for the SCATTER statement gives the answer on the third line:

COLORRESPONSE= specifies the numeric column that is used to map colors to a gradient legend

 

For example:

proc sgplot data=sashelp.class;
scatter x=height y=weight / markerchar=Name colorresponse=age;
run;

If you want to change the colors used for the color ramp, you can use the COLORMODEL= option to choose from a variety of pre-built ramps, or create your own:

proc sgplot data=sashelp.class;
scatter x=height y=weight / markerchar=Name colorresponse=age
   colormodel=(blue green orange red); /* custom 4-color ramp */
run;

The COLORRESPONSE= and COLORMODEL= options were added to the SCATTER statement in SAS 9.m2 (??).

It was added to other statements (like DOT and SERIES) in SAS 9.4m3. 

 

You can also use the TEXTPLOT statement in SAS 9.4m2. Sanjay wrote a great blog that has many examples, including chaging size and color.

 

For older versions of SAS, you can read this article about using the Graph Template Language (GTL) to color markers by a continuous variable.

NKormanik
Barite | Level 11

Bummer!  SAS version here is 9.4 TS Level 1M1.

 

Would have been extremely useful.  Well, others reading your answer will benefit, no doubt.

 

Thank you, Rick!  Wonderful that you answer so many questions.

 

 

Jay54
Meteorite | Level 14

With SAS 9.40M1, you can still use the GTL SCATTERPLOT statement with the MARKERCOLORGRADIENT option.  This is the same code that is eventually driven by the SGPLOT.  Look up the documentation for the statement in the GTL doc.  

 

ScatterGradient.png

 

The basic GTL code will look like this:

 

proc template;
  define statgraph class;
    begingraph;
      entrytitle 'Weight by Height for all Students';
      layout overlay;
        scatterplot x=height y=weight / group=sex name='a' markercolorgradient=age
                         markerattrs=(symbol=circlefilled size=12);
        continuouslegend 'a';
      endlayout;
    endgraph;
  end;
run;

 

proc sgrender data=sashelp.class template=class;
run;

 

The COLORMODEL takes one of the predefined gradient models from the style.  To change that, you have to change one in the style.  The SAS 9.40M3 allows setting color model directly in syntax.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2220 views
  • 2 likes
  • 3 in conversation