Data visualization with SAS programming

Superimpose a graph

Reply
N/A
Posts: 0

Superimpose a graph

How can I superimpose a scatterplot to a boxplot?
SAS Super FREQ
Posts: 890

Re: Superimpose a graph

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;
N/A
Posts: 0

Re: Superimpose a graph

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
SAS Employee
Posts: 967

Re: Superimpose a graph

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;
N/A
Posts: 0

Re: Superimpose a graph

This code works. Thanks a lot.
SAS Super FREQ
Posts: 890

Re: Superimpose a graph

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
SAS Super FREQ
Posts: 890

Re: Superimpose a graph

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
SAS Super FREQ
Posts: 890

Re: Superimpose a graph

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;
Ask a Question
Discussion stats
  • 7 replies
  • 184 views
  • 0 likes
  • 3 in conversation