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!
Depends on your version of SAS, not all versions support this type of overlays.
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://blogs.sas.com/content/graphicallyspeaking/2012/10/30/violin-plots/
Another possible cause is TRANSPARENCY = 1 ->
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) |
@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!
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!
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:
@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!
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?
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?
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;
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;
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;
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;
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?
@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.
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...
Presort your data in desired order?
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.