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

Following the script given in http://blogs.sas.com/content/graphicallyspeaking/2015/12/06/boxplot-with-connect-using-annotate/, I managed to create a boxplot with multiple connect lines for Q1 and Q3 statistics running through 10 categories. However, I need to repeat this for several other response variables. Is there an easy way to create multiple boxplots with Annotate? My attempt in creating three boxplots with one sgplot code (using the by statement) failed miserably as only one boxplot was created but with 6 lines going through the different categories. Can anybody help? Thanks!

 

proc sgplot data=residrank sganno=sganno noautolegend;
vbox student / category=rank_linearpred;
by resp_name;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

Maybe there is a simpler solution.  No annotation is required.  Three graphs are generated by Origin. 

 

proc sort data=sashelp.cars out=cars;
  by origin;
run;


proc sgplot data=cars nocycleattrs noautolegend;
  by origin;
  vbox mpg_city / category=type connect=q1;
  vbox mpg_city / category=type nofill connect=q3;
run;

 

BoxConnect.png

View solution in original post

12 REPLIES 12
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, Sanjay is the best man to answer that one as he's the graph master.  From my side, I would split the graph up.  I would first do the box plot as you have.  Then for each other line you require, do a series plot for that data, overlaying each result:

proc sgplot data=residrank sganno=sganno noautolegend;
  vbox student / category=rank_linearpred;
  series x=... y=...;
  by resp_name;
run;

You should have the data elements in your data.

Jay54
Meteorite | Level 14

Can you share the data sets (main and anno) you have generated for this program? That will save us some time.  Also, which release of SAS are you using?

Jay54
Meteorite | Level 14

Maybe there is a simpler solution.  No annotation is required.  Three graphs are generated by Origin. 

 

proc sort data=sashelp.cars out=cars;
  by origin;
run;


proc sgplot data=cars nocycleattrs noautolegend;
  by origin;
  vbox mpg_city / category=type connect=q1;
  vbox mpg_city / category=type nofill connect=q3;
run;

 

BoxConnect.png

JulieM
Obsidian | Level 7

Great! Thanks for the solution!

JulieM
Obsidian | Level 7

Hi Sanjay,

 

I found this graph on the web and this is actually what I was aiming for (smoothed Q1 and Q3 done by lowess as connect lines). In this case, I believe there's no recourse but to use annotate, is that correct? And how to do that for multiple response variables?

 

Would truly appreciate your help on this one.

 


ResVsPred_Smooth.png
JulieM
Obsidian | Level 7
By the way, the graph was done on R.
Jay54
Meteorite | Level 14

Which release of SAS are you using?  That can make a big difference. With 9.4M3 you can use the SPLINE statement.  Else you would need to compute the spline cureve yourself using Annotate.  Can be done, but more work. 

JulieM
Obsidian | Level 7
I'm using SAS 9.3 😞
Jay54
Meteorite | Level 14

GTL SERIESPLOT supports SMOOTHCONNECT.  This is a poor cousin of SPLINE, but may work for you.  I will write up a blog post on how to do this.  My simple use case works without BY variable.  Will need work for BY variable support.  Also, SAS 9.4 output is nicer since it uses SUBPIXEL.  SAS 9.3 does not use SUBPIXEL, so the curve is not as nice.

 

I changed the data so x axis is numeric.  Connect makes more sense with numeric x axis. Note however, that the smooth connect line overshoots the value when the curvature is sharp, as seen at Week 2.

 

 


Box_Connect_Smooth_94.png
Rick_SAS
SAS Super FREQ

@JulieM Just because someone creates a graph does not mean it's a good idea. In the graph you present, it looks like the X axis is actually continuous, but was binned to create categorical varlues for the box plots.  The "connecting curves" look like an attempt to approximate quantile regression curves. You can learn more about the plot you provided and why quantile regression is a better idea in the article "Quantile regression: Better than connecting the sample quantiles of binned data."

 

The QUANTREG procedure can create this kinds of plots automatically, and the curves that it creates are better estimates for the quantities that it looks like you are interested in.

 

Instead of telling us the graph you want to make, why not describe the data and the statistical questions you are trying to visualize? If quantile regression is the answer, there are many experts in the Statistical Procedures Support Community who can advise you with syntax.  The article I mentioned previously has sample syntax.

JulieM
Obsidian | Level 7

Hi @Rick_SAS,

 

I'm actually exploring ways to visualize the presence/absence of heteroskedasticity of residuals in a linear model. It seemed a good idea to do that using boxplots of bins of equal counts across the range of the linear predictors (with smoothed connecting curves to emphasize the general overall pattern). I'll have a look at quantile regressions. 

 

Thanks for the tip!

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
  • 12 replies
  • 3295 views
  • 5 likes
  • 4 in conversation