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.
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;
What version of SAS do you have?
Hi Dan, 9.3
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;
Thank you so much Dan. I made a few changes and this works perfectly for what I was looking for. I really appreciate it.
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.