Data visualization with SAS programming

Getting scatterplot to overlay box plot proc sgplot vbox

Reply
Contributor
Posts: 22

Getting scatterplot to overlay box plot proc sgplot vbox

Hi,

 

I'm trying to get a scatter plot to overlay my box plot with proc sgplot vbox.  The box plot looks great but it's not showing the individual data points.  I've exhaustively searched the forum, and this is what I could come up with:

 

title "Individual Resting State Network Connectivities";
proc sgplot data=new_t 
vbox col1 / group=_name_ ;
scatter x=_name_ y=col1 / jitter transparency=1.0
markerattrs=(color=black symbol=CircleFilled);
run;

 

What am I missing???

 

Thanks!

 

Super User
Posts: 22,820

Re: Getting scatterplot to overlay box plot proc sgplot vbox

Depends on your version of SAS, not all versions support this type of overlays. 

 

http://documentation.sas.com/?docsetId=grstatproc&docsetTarget=p0yud64khw8fuin1xgr85dgxbb7t.htm&docs...

 

However, there are workarounds and this has been asked and answered before. 

 

https://blogs.sas.com/content/graphicallyspeaking/2015/12/23/box-plot-with-stat-table-and-markers/

 

https://communities.sas.com/t5/SAS-GRAPH-and-ODS-Graphics/overlay-jitter-scatter-plot-on-boxplot-in-...

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

 

Another possible cause is TRANSPARENCY = 1 ->

 

TRANSPARENCY=value

specifies the degree of transparency for the plot. The transparency that you specify applies to all aspects of the plot statement.

Default 0.0
Range 0 (completely opaque) to 1 (completely transparent)

 

http://documentation.sas.com/?docsetId=grstatproc&docsetTarget=p1lcbd3lhs3t3bn1jk6d8sjt2yqx.htm&docs...

 


bsriv wrote:

Hi,

 

I'm trying to get a scatter plot to overlay my box plot with proc sgplot vbox.  The box plot looks great but it's not showing the individual data points.  I've exhaustively searched the forum, and this is what I could come up with:

 

title "Individual Resting State Network Connectivities";
proc sgplot data=new_t 
vbox col1 / group=_name_ ;
scatter x=_name_ y=col1 / jitter transparency=1.0
markerattrs=(color=black symbol=CircleFilled);
run;

 

What am I missing???

 

Thanks!

 


 

Contributor
Posts: 22

Re: Getting scatterplot to overlay box plot proc sgplot vbox

Thanks- I'm using SAS university but I tried a computer with SAS 9.4 and was having the same problems.   Either way, none of the links posted provide "workarounds" or am I missing something?   Thanks!

Super User
Posts: 22,820

Re: Getting scatterplot to overlay box plot proc sgplot vbox

[ Edited ]

It works fine for me out of the box, SAS University Edition, so you have some other issue, either the transparency or something else in your data. 

 

Run the solution from here, which you should be able to do on any SAS installation out of the box:

 

https://communities.sas.com/t5/SAS-GRAPH-and-ODS-Graphics/overlay-jitter-scatter-plot-on-boxplot-in-...

 


bsriv wrote:

Thanks- I'm using SAS university but I tried a computer with SAS 9.4 and was having the same problems.   Either way, none of the links posted provide "workarounds" or am I missing something?   Thanks!


 

Contributor
Posts: 22

Re: Getting scatterplot to overlay box plot proc sgplot vbox

If I change group to category it works but then the order is messed up, and the colors are gone.  Is there a way to do in group or have the categories colored?

Super User
Posts: 22,820

Re: Getting scatterplot to overlay box plot proc sgplot vbox

There probably is. 

This would be much much easier if you used the SASHELP.CARS data set and showed the code you currently have, the graph getting produced and what changes you want made. I'm guessing at this point.

 


bsriv wrote:

If I change group to category it works but then the order is messed up, and the colors are gone.  Is there a way to do in group or have the categories colored?


 

Contributor
Posts: 22

Re: Getting scatterplot to overlay box plot proc sgplot vbox

[ Edited ]

First with "category"

 

proc transpose data=sashelp.cars out=new_t;
by Make;
run;

 

title "Cars";
proc sgplot data=new_t;
vbox Col1 / category=_name_ ;
scatter x=_name_ y=col1 / jitter transparency=0.0
markerattrs=(color=black symbol=CircleFilled);
run;

 

Screen Shot 2018-02-14 at 11.13.17 AM.png

 

Then with group 

proc transpose data=sashelp.cars out=new_t;
by Make;
run;

 

title "Cars";
proc sgplot data=new_t;
vbox Col1 / group=_name_ ;
scatter x=_name_ y=col1 / jitter transparency=0.0
markerattrs=(color=black symbol=CircleFilled);
run;

 

Screen Shot 2018-02-14 at 11.15.56 AM.png

Then group and group order descend

proc transpose data=sashelp.cars out=new_t;
by Make;
run;

 

title "Cars";
proc sgplot data=new_t;
vbox Col1 / group=_name_ grouporder=descending;
scatter x=_name_ y=col1 / jitter transparency=0.0
markerattrs=(color=black symbol=CircleFilled);
run;
 

Screen Shot 2018-02-14 at 11.17.48 AM.png

 

Super User
Posts: 22,820

Re: Getting scatterplot to overlay box plot proc sgplot vbox

[ Edited ]

Isn't the first one correct? Though the scale is not the same so I'm not sure of the value of trying to show these variables in the same plot when they have varying widely scales? Perhaps looking at them one a time may work better?

 

Looking at only MPG which is on the same scale:

 

title "Cars";
proc sgplot data=new_t;
where _name_ =: 'MPG';
vbox Col1 / category=_name_ ;
scatter x=_name_ y=col1 / jitter transparency=0.0
markerattrs=(color=black symbol=CircleFilled);
run;
Contributor
Posts: 22

Re: Getting scatterplot to overlay box plot proc sgplot vbox

It is correct in the sense it shows the scatter plot superimposed on the boxplot.

 

BUT- how do I get the boxes to be different colors AND how do I get this to display in descending order?

 

 

Super User
Posts: 22,820

Re: Getting scatterplot to overlay box plot proc sgplot vbox


bsriv wrote:

It is correct in the sense it shows the scatter plot superimposed on the boxplot.

  


So your original question is answered.

 


 

BUT- how do I get the boxes to be different colors AND how do I get this to display in descending order?

 


And you have a new DIFFERENT question. GROUPORDER=DESCENDING works for me to change the order, but this depends on how you define DESCENDING. If you define it differently you can force it with a format instead.

 

And try a discrete attribute table to set the colours. 

Contributor
Posts: 22

Re: Getting scatterplot to overlay box plot proc sgplot vbox

The issue is that group order descending  works, I can't see the scatterplot with "group" only with "category", for which I can't use grouporder...

Super User
Posts: 22,820

Re: Getting scatterplot to overlay box plot proc sgplot vbox

Presort your data in desired order?

Super User
Posts: 22,820

Re: Getting scatterplot to overlay box plot proc sgplot vbox

And one other option,  take the SGPLOT code, get the GTL code using TMPLOUT and modify that instead.

 

title "Cars";
proc sgplot data=new_t tmplout='C:\_localdata\temp\myfile.sas';
where _name_ =: 'MPG';
vbox Col1 / category=_name_ ;
scatter x=_name_ y=col1 / jitter transparency=0.0 
markerattrs=(color=black symbol=CircleFilled);
run;

Generates:

 

proc template;
define statgraph sgplot;
dynamic _ticklist_;
begingraph / collation=binary;
EntryTitle "Cars" /;
layout overlay / xaxisopts=(labelFitPolicy=Split) x2axisopts=(labelFitPolicy=Split) xaxisopts=(discreteOpts=(sortOrder=ascendingFormatted)) xaxisopts=(type=Discrete discreteOpts=(tickValueList=_ticklist_ tickvaluefitpolicy=SplitRotate tickValueListPolicy=Union)) x2axisopts=(discreteOpts=(sortOrder=ascendingFormatted)) x2axisopts=(type=Discrete discreteOpts=(tickValueList=_ticklist_ tickvaluefitpolicy=SplitRotate tickValueListPolicy=Union));
   BoxPlot X=_NAME_ Y=COL1 / primary=true LegendLabel="COL1" NAME="VBOX";
   ScatterPlot X=_NAME_ Y=COL1 / subpixel=off jitter=auto Markerattrs=( Color=CX000000 Symbol=CIRCLEFILLED) DataTransparency=0 LegendLabel="COL1" NAME="SCATTER";
   DiscreteLegend "VBOX" "SCATTER";
endlayout;
endgraph;
end;
run;

GTL gives you more control over the graphics but it's definitely harder to learn and work with compared to SGPLOT.

Ask a Question
Discussion stats
  • 12 replies
  • 272 views
  • 0 likes
  • 2 in conversation