- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 12-14-2009 10:04 AM
(1663 views)
How can I superimpose a scatterplot to a boxplot?
7 REPLIES 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This code works. Thanks a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Thanks!
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Thanks!
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
data _null_;
set sashelp.class;
file print ods=(template="boxscatter");
put _ods_;
run;