Data visualization with SAS programming

Customizing Scatterplots with SGPLOT

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Customizing Scatterplots with SGPLOT


Hi,

I am looking to customize a scatterplot produced with SGPLOT in the following ways:

1. Highlight specific data points

    A. Highlight one data point in one color

    B. Highlight a group of data points (not criteria A) in a second color

    C. Highlight the other points that do not meet criteria A and B in a third color

2. Superimpose two lines -

     A. One at a given point on the y axis

     B. One at a given point on the x axis.

Sample data is attached.

1A. Pick one id, for example, AB212, and fill the point one color (e.g. blue)

1B. Since AB212 is a part of group 1, fill all data points in group 1 a second color (e.g. purple)

1C. Every other data point (groups in (2, 3)) , fill a third color (e.g. green)

2A. Create a black line (thin) across the xaxis for the xline value

2B. Create a black line (thin) across the yaxis for the yline value.

Attachment

Accepted Solutions
Solution
‎01-03-2014 04:31 PM
SAS Super FREQ
Posts: 864

Re: Customizing Scatterplots with SGPLOT

Add the following code to your sample data. Simply change the SELECT_ID macro variable to the ID of interest, and the code will take care of the rest. Let me know if you have any questions about it.

Thanks!
Dan

/*-----------------------------------------------------------------------------------------------------------------*/

%let select_id=AB212;

/* Find the group of interest */

data _null_;

set test;

if (id eq &select_id) then call symput('ACTIVEGROUP', group);

run;

data plotdata;

length grpstr $ 15;

label grpstr="ID";

set test;

ref_x = .;

ref_y = .;

if (id eq "&select_id") then do;

  grpstr="&select_id";

  ref_x = x;

  ref_y = y;

end;

else if (group eq symget('ACTIVEGROUP')) then grpstr="In same group";

else grpstr="In other groups";

run;

data attrmap;

retain id "grpmap" markersymbol "circlefilled";

input value $ 1-15 markercolor $ 16-22;

cards;

In other groups green

In same group   purple

_other_         blue

;

run;

proc sgplot data=plotdata dattrmap=attrmap;

scatter x=x y=y / group=grpstr attrid=grpmap;

refline ref_x / axis=x label;

refline ref_y / label;

run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 864

Re: Customizing Scatterplots with SGPLOT

What version of SAS do you have?

Contributor
Posts: 28

Re: Customizing Scatterplots with SGPLOT

Hi Dan, 9.3

Solution
‎01-03-2014 04:31 PM
SAS Super FREQ
Posts: 864

Re: Customizing Scatterplots with SGPLOT

Add the following code to your sample data. Simply change the SELECT_ID macro variable to the ID of interest, and the code will take care of the rest. Let me know if you have any questions about it.

Thanks!
Dan

/*-----------------------------------------------------------------------------------------------------------------*/

%let select_id=AB212;

/* Find the group of interest */

data _null_;

set test;

if (id eq &select_id) then call symput('ACTIVEGROUP', group);

run;

data plotdata;

length grpstr $ 15;

label grpstr="ID";

set test;

ref_x = .;

ref_y = .;

if (id eq "&select_id") then do;

  grpstr="&select_id";

  ref_x = x;

  ref_y = y;

end;

else if (group eq symget('ACTIVEGROUP')) then grpstr="In same group";

else grpstr="In other groups";

run;

data attrmap;

retain id "grpmap" markersymbol "circlefilled";

input value $ 1-15 markercolor $ 16-22;

cards;

In other groups green

In same group   purple

_other_         blue

;

run;

proc sgplot data=plotdata dattrmap=attrmap;

scatter x=x y=y / group=grpstr attrid=grpmap;

refline ref_x / axis=x label;

refline ref_y / label;

run;

Contributor
Posts: 28

Re: Customizing Scatterplots with SGPLOT

Thank you so much Dan. I made a few changes and this works perfectly for what I was looking for. I really appreciate it.

Post a Question
Discussion Stats
  • 4 replies
  • 611 views
  • 0 likes
  • 2 in conversation