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

I want to create a scatter plot where the plot symbol values are determined according to the values of one categorical variable and the plot symbol colors are determined by another dichotomous categorical variable.  For example, using the sashelp.class data set, suppose I want to make a simple scatter plot of height*weight.  I want each age to be denoted with a different plot symbol, summarized in a legend.  For example a dot for age 11, a square for age 12, etc.  Furthermore, I want the color of the plot symbol to be blue for all males and red for all females.  Therefore, a red square would indicate a 12 year old female and a blue square would indicate a 12 year old male.  Is this possible?  Any feasible solution would need to be generalized so that it could work with BY group processing.  For example, if I had a data set with 20 classes of students with different ages and I wanted to create a separate graph for each class.

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

If you have at least SAS 9.2, maintenance 3, you can create your example in GTL using the following code:

proc template;
define statgraph plot;
begingraph;
layout overlay;
  seriesplot x=weight y=height / group=age lineattrs=(thickness=0) display=(markers)
             markercolorgroup=sex markersymbolgroup=age name="scatter";
  discretelegend "scatter" / type=markercolor location=inside
                 autoalign=(topleft topright bottomright bottomleft);
  discretelegend "scatter" / type=markersymbol;
endlayout;
endgraph;
end;
run;

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

View solution in original post

9 REPLIES 9
GraphGuy
Meteorite | Level 14

I don't think you'll be able to do this with standard Proc Gplot, using "plot y*x=z".

You can do this with annotated markers (controlling both the shape and color programmatically).

Let me know if you need to see some sample code.

polingjw
Quartz | Level 8

I'll look into your suggestion about annotated markers.  I've used the annotate facility to draw lines and insert text boxes, but not to change the symbols themselves.  Yes, if you have some sample code that is relevant to this problem, I would like to see it.  Thanks.

GraphGuy
Meteorite | Level 14

Ok - here's some code to play with...

I slightly modified my original idea.  I go ahead and use "plot y*x=z" to get the symbols & legend.

And I then annotate a simple colored 'dot/pie' on top of that symbol to get the desired color:

data anno_color; set sashelp.class;
length color $8;
xsys='2'; ysys='2'; when='a'; hsys='3';
x=weight;
y=height;
function='pie'; style='psolid'; rotate=360; size=.8;
if sex='M' then color='blue';
else if sex='F' then color='pink';
else color='black';
run;

symbol1 value=circle height=7pct color=gray33;
symbol2 value=diamond height=7pct color=gray33;
symbol3 value=plus height=7pct color=gray33;
symbol4 value=square height=7pct color=gray33;
symbol5 value=star height=7pct color=gray33;

legend1 position=(bottom center);

proc gplot data=sashelp.class anno=anno_color;
plot height*weight=age / legend=legend1;
run;

polingjw
Quartz | Level 8

That's a really creative solution.  Thanks again!

DanH_sas
SAS Super FREQ

If you have at least SAS 9.2, maintenance 3, you can create your example in GTL using the following code:

proc template;
define statgraph plot;
begingraph;
layout overlay;
  seriesplot x=weight y=height / group=age lineattrs=(thickness=0) display=(markers)
             markercolorgroup=sex markersymbolgroup=age name="scatter";
  discretelegend "scatter" / type=markercolor location=inside
                 autoalign=(topleft topright bottomright bottomleft);
  discretelegend "scatter" / type=markersymbol;
endlayout;
endgraph;
end;
run;

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

polingjw
Quartz | Level 8

Thank you.  When I run the program, every age has a plot symbol of circle, so there is no way to distinguish between the ages.  The documentation on the MARKERCOLORGROUP=  option says that the option is ignored unless MARKERSYMBOL=CHARACTER. 

DanH_sas
SAS Super FREQ

I'm sorry,I was incorrect about the required version of SAS. The code I gave you requires SAS 9.3. Sorry about the mistake.

polingjw
Quartz | Level 8

I do have SAS 9.3 and your code is working.  For some reason the plot symbols are are not displayed correctly in the HTML SAS result viewer.  When I use a different ODS destination such as PDF, it works perfectly.  Thanks!

DanH_sas
SAS Super FREQ

The reason you saw all of the same symbols in HTML is that the new default style (HTMLBLUE) uses the same symbol until it iterates through all of the colors. Try using, "ods html style=htmlbluecml", to get the same colors with unique symbols.

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
  • 9 replies
  • 7816 views
  • 3 likes
  • 3 in conversation