Data visualization with SAS programming

Boxplot (multiple variable) connecting stats using Annotate

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Boxplot (multiple variable) connecting stats using Annotate

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;


Accepted Solutions
Solution
‎01-19-2016 08:11 PM
SAS Super FREQ
Posts: 1,145

Re: Boxplot (multiple variable) connecting stats using Annotate

[ Edited ]

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


All Replies
Super User
Super User
Posts: 7,720

Re: Boxplot (multiple variable) connecting stats using Annotate

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.

SAS Super FREQ
Posts: 1,145

Re: Boxplot (multiple variable) connecting stats using Annotate

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?

Solution
‎01-19-2016 08:11 PM
SAS Super FREQ
Posts: 1,145

Re: Boxplot (multiple variable) connecting stats using Annotate

[ Edited ]

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

Occasional Contributor
Posts: 13

Re: Boxplot (multiple variable) connecting stats using Annotate

Great! Thanks for the solution!

SAS Super FREQ
Posts: 1,145

Re: Boxplot (multiple variable) connecting stats using Annotate

[ Edited ]
Occasional Contributor
Posts: 13

Re: Boxplot (multiple variable) connecting stats using Annotate

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
Occasional Contributor
Posts: 13

Re: Boxplot (multiple variable) connecting stats using Annotate

By the way, the graph was done on R.
SAS Super FREQ
Posts: 1,145

Re: Boxplot (multiple variable) connecting stats using Annotate

[ Edited ]

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. 

Occasional Contributor
Posts: 13

Re: Boxplot (multiple variable) connecting stats using Annotate

I'm using SAS 9.3 :-(
SAS Super FREQ
Posts: 1,145

Re: Boxplot (multiple variable) connecting stats using Annotate

[ Edited ]

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
SAS Super FREQ
Posts: 3,630

Re: Boxplot (multiple variable) connecting stats using Annotate

@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.

Occasional Contributor
Posts: 13

Re: Boxplot (multiple variable) connecting stats using Annotate

[ Edited ]

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 870 views
  • 5 likes
  • 4 in conversation