Data visualization with SAS programming

overlay jitter scatter plot on boxplot in sgplot

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

overlay jitter scatter plot on boxplot in sgplot

I want to add jitter scatter plot on my boxplot.  I used sgplot to produce my boxplot.  I found this post here http://blogs.sas.com/content/graphicallyspeaking/2012/05/07/unbox-your-box-plots/

The picture is exactly what I want but I wonder if I can produce it with sgplot.

 

Below is my sgplot code

 

proc sgplot data=box;
/* title "Plot of Hemoglobin A1c - baseline";*/
YAXIS label='Diastolic blood pressure' LABELATTRS=(SIZE= 8);
XAXIS label=' ' LABELATTRS=(SIZE= 8);
vbox result / datalabel=outid;
ods output SGPlot=boxlist;
run;


Accepted Solutions
Solution
‎08-22-2017 08:11 PM
SAS Super FREQ
Posts: 975

Re: overlay jitter scatter plot on boxplot in sgplot

If you have SAS 9.4, maintenance 1 or greater, you can do it like this:

 

proc sgplot data=sashelp.cars;
vbox weight / category=type;
scatter x=type y=weight / jitter;
run;

SGPlot2.png

View solution in original post


All Replies
Super User
Posts: 12,148

Re: overlay jitter scatter plot on boxplot in sgplot

If you search your documentation for SGPLOT and SGPANEL for "Plot Type Compatibility" you will find that in those procedures the only plots that can be used together with VBOX are Density, HBOX and Histogram.

 

The nature of the plot determines which other types share the "plot space".

 

It may be possible with an annotate dataset with SGPLOT but no promises.

 

OR sumarize your data and possibly use Polygon and lineparm or such along with scatter to simulate the VBOX portion.

 

Super User
Posts: 21,530

Re: overlay jitter scatter plot on boxplot in sgplot

Pretty sure this was asked and answered recently on here. Search through the forums. You may also be interested in the default plots from PROC UNIVARIATE, they do a good job of displaying the distribution of data.
Solution
‎08-22-2017 08:11 PM
SAS Super FREQ
Posts: 975

Re: overlay jitter scatter plot on boxplot in sgplot

If you have SAS 9.4, maintenance 1 or greater, you can do it like this:

 

proc sgplot data=sashelp.cars;
vbox weight / category=type;
scatter x=type y=weight / jitter;
run;

SGPlot2.png

New Contributor
Posts: 2

Re: overlay jitter scatter plot on boxplot in sgplot

is there any way to overlay jitter scatter plot on boxplot along with ViolinPlot

SAS Super FREQ
Posts: 1,226

Re: overlay jitter scatter plot on boxplot in sgplot

Posted in reply to Nagadurga

See this article in Graphically Speaking.  It is a bit old, and there may be easier ways to do this using SAS 9.4 or later.

https://blogs.sas.com/content/graphicallyspeaking/2012/10/30/violin-plots/

 

New Contributor
Posts: 2

Re: overlay jitter scatter plot on boxplot in sgplot

Posted in reply to Sanjay_SAS

Hello Sanjay,

 

I am trying to overlay a Boxplot, Scatterplot and ViolinPlot.For this I am using PROC SGPLOT with sas 94.

 

Example code is as below to produce boxplot + scatterplot.

However when one adds the bandplot to this, then it does not work anymore.

See the warning below obtained when adding a bandplot statement.

 

proc sgplot data=ars;

vbox rcbi / category=arm2 boxwidth=0.4 nooutliers;

scatter x=arm2 y=rcbi / group=arm2  jitter transparency=0.6

         markerattrs=(/*color=red*/ symbol=CircleFilled);

run;

 

WARNING: BANDPLOT statement has a conflict with the axis type. The plot will not be drawn.

 

This is the same when using proc SGPANEL.

I can create the violin plot using band statement using code below

 

proc sgpanel data=final;

panelby arm2;

band y=rcbi2 lower=mirror upper=density;

run;

 

But it does not work anymore if I add the vbox and scatter statement on top …

Let me know if any thoughts

 

SAS Super FREQ
Posts: 1,226

Re: overlay jitter scatter plot on boxplot in sgplot

[ Edited ]
Posted in reply to Nagadurga

You can overlay the Band and the Scatter without any issues.  Adding VBox is the issue.

 

Since all data markers are already in the plot (Scatter) you only need to overplot the Q1-Q3 box, Mean, Median and Whiskers.  One way to do this would be to first run PROC MEANS to get these values in an output data set.  Then merge these with the original data, and use HighLow plot(s) overlay to draw the box details along with the Scatter and Band.  Use another scatter for the Mean marker.  I suggest dimming (transparency) the scatter markers to avoid clutter.

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 935 views
  • 5 likes
  • 6 in conversation