BookmarkSubscribeRSS Feed
deleted_user
Not applicable
How can I superimpose a scatterplot to a boxplot?
7 REPLIES 7
DanH_sas
SAS Super FREQ
Assuming you have SAS 9.2, you can do this easily using the Graph Template Language. Here is a simple example:

proc template;
define statgraph boxscatter;
begingraph;
layout overlay;
boxplot x=age y=height;
scatterplot x=age y=weight / yaxis=y2;
endlayout;
endgraph;
end;
run;

proc sgrender data=sashelp.class template=boxscatter; run;
deleted_user
Not applicable
Thanks, for the code. Do I have problem with this code if the data sources for the plots are not same. I also need to include a by variable in the boxplot. In addition, I got the following message in the log:

1143 proc template;
1144 define statgraph boxscatter;
1145 begingraph;
----------
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
1146 layout overlay;
1147 boxplot x=age y=height;
1148 scatterplot x=age y=weight / yaxis=y2;
1149 endlayout;
1150 endgraph;
--------
1
WARNING 1-322: Assuming the symbol END was misspelled as endgraph.
1151 end;
WARNING: Object will not be saved.
1152 run;
NOTE: PROCEDURE TEMPLATE used (Total process time):
real time 0.21 seconds
cpu time 0.00 seconds

WARNING: Errors were produced.
NOTE: The SAS System stopped processing this step because of errors.
1153


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

ERROR: Procedure SGRENDER not found.

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SGRENDER used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
GraphGuy
Meteorite | Level 14
Here's how with good-old traditional gplot (the example generates some fake/random data, and also plots it)...

Assuming you're using the same data for the boxplot and the scatter plot (ie, you just want to also show a scatter plot of the points that the boxplot is based on), this can be very easily done by putting the y*x on the plot statement twice, and using a different plot interpolation for each:

data foo;
do x=0 to 10 by 1;
do i=1 to 20;
y=rannor(23749);
output;
end;
end;
run;

symbol1 v=none i=box color=blue;
symbol2 v=dot height=.01 i=none color=red;
proc gplot data=foo;
plot y*x=1 y*x=2 / overlay;
run;



Or, you can plot totally separate data (that isn't necessarily confined to having the exact same x-values as the boxplot), using a technique such as this...

data foo;
do x=0 to 10 by .1;
do i=1 to 20;
y=rannor(23749);
output;
end;
end;
run;

data foo; set foo;
x_rounded=round(x);
run;

symbol1 v=none i=box color=blue;
symbol2 v=dot height=.01 i=none color=red;
proc gplot data=foo;
plot y*x_rounded=1 y*x=2 / overlay;
run;
deleted_user
Not applicable
This code works. Thanks a lot.
DanH_sas
SAS Super FREQ
Based on your log output, you must not have SAS 9.2. Robert's gplot approach should work fine for you. As a side note, the SGRENDER procedure does support BY-groups. The BY-group variable(s) is not specified in the template.

Thanks!
Dan
DanH_sas
SAS Super FREQ
Correction on my last post, you probably have 9.2 phase 1. Does the code run for you if you remove the BEGINGRAPH and ENDGRAPH?

Thanks!
Dan
DanH_sas
SAS Super FREQ
In addition, you may need to use this bit of code instead of SGRENDER:

data _null_;
set sashelp.class;
file print ods=(template="boxscatter");
put _ods_;
run;

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
  • 7 replies
  • 991 views
  • 0 likes
  • 3 in conversation