Data visualization with SAS programming

SAS Box plots adding an overlay

Accepted Solution Solved
Reply
Occasional Contributor mgm
Occasional Contributor
Posts: 10
Accepted Solution

SAS Box plots adding an overlay

I'm using the following code to  create box plots and I would like to overlay the boxplot output with a specific point in the distribution(e.g. most recent value ) in order to show where we stand currently versus the historical distribution.  Is there a way to easily highlight a specific value in the box plot. 

 


proc sort data = Regionbets ; by region; run;
ods path reset;
ODS path show;
ods path(prepend) work.templat(update);

proc template;
define statgraph boxplot;
begingraph;
entrytitle "Country bets ";
layout overlay;
boxplot y=actweight x=region/
datalabel=make spread=true;
endlayout;
endgraph;
end;
run;

proc sgrender data=Regionbets template=boxplot;
label Region="test regions"
label actweight="Active";
run;

 


Accepted Solutions
Solution
‎06-08-2016 10:28 AM
SAS Super FREQ
Posts: 939

Re: SAS Box plots adding an overlay

Yes, something like that. From your initial description, my expectation is that you will have one scatter point per box. Using your code:

 

proc sort data = Regionbets ; by region; run;
ods path reset;
ODS path show;
ods path(prepend) work.templat(update);
proc template;
define statgraph boxplot;
begingraph;
   entrytitle "Country bets ";
   layout overlay;
      boxplot y=actweight x=region / datalabel=make spread=true;
      scatterplot y=curpoint x=region2 / markerattrs=(symbol=starfilled);
   endlayout;
endgraph;
end;
run;
proc sgrender data=Regionbets template=boxplot;
label Region="test regions"
label actweight="Active";
run;

 

Where "curpoint" contains the current points for each box, and "region2" is the corresponding region value for the point. THese two columns should be merged with your original data into a "merged" data set using a DATA step.

 

Hope this helps!

Dan

View solution in original post


All Replies
SAS Super FREQ
Posts: 939

Re: SAS Box plots adding an overlay

Probably the most straighfroward way it to overlay a scatterplot on the boxplot and set the marker symol different from the outlier markers.

Occasional Contributor mgm
Occasional Contributor
Posts: 10

Re: SAS Box plots adding an overlay

[ Edited ]

Something along the following lines  ? http://support.sas.com/resources/papers/proceedings12/285-2012.pdf

 

proc sort data=sashelp.cars; by DRIVETRAIN; run; data cars2; set sashelp.cars; if DRIVETRAIN="All" then ALL=-1+0.1*rannor(0); if DRIVETRAIN="Front" then FRONT=1.5+0.1*rannor(0); if DRIVETRAIN="Rear" then REAR=4+0.1*rannor(0); run; proc template; define statgraph mygraphs.example5; begingraph; layout overlay / x2axisopts=(display=(line) linearopts=(viewmin=-1 viewmax=4)); boxplot x=DRIVETRAIN y=MPG_CITY / display=(caps mean median) fillattrs=(color=white); scatterplot x=ALL y=MPG_CITY / xaxis=x2 markerattrs=(color=blue); scatterplot x=FRONT y=MPG_CITY / xaxis=x2 markerattrs=(color=red); scatterplot x=REAR y=MPG_CITY / xaxis=x2 markerattrs=(color=green); endlayout; endgraph; end; run; Figure 6b
Super User
Posts: 11,343

Re: SAS Box plots adding an overlay


mgm wrote:

Something along the following lines  ? http://support.sas.com/resources/papers/proceedings12/285-2012.pdf

 


 

 

@mgm It is a good idea to post code in the Code box opened using the SAS "Run" icon to reduce poorly formatted posts.

Solution
‎06-08-2016 10:28 AM
SAS Super FREQ
Posts: 939

Re: SAS Box plots adding an overlay

Yes, something like that. From your initial description, my expectation is that you will have one scatter point per box. Using your code:

 

proc sort data = Regionbets ; by region; run;
ods path reset;
ODS path show;
ods path(prepend) work.templat(update);
proc template;
define statgraph boxplot;
begingraph;
   entrytitle "Country bets ";
   layout overlay;
      boxplot y=actweight x=region / datalabel=make spread=true;
      scatterplot y=curpoint x=region2 / markerattrs=(symbol=starfilled);
   endlayout;
endgraph;
end;
run;
proc sgrender data=Regionbets template=boxplot;
label Region="test regions"
label actweight="Active";
run;

 

Where "curpoint" contains the current points for each box, and "region2" is the corresponding region value for the point. THese two columns should be merged with your original data into a "merged" data set using a DATA step.

 

Hope this helps!

Dan

SAS Super FREQ
Posts: 3,753

Re: SAS Box plots adding an overlay

There have been several discussions recently about overlaying scatter plots or series plots on box plots.  I've summarized some of my thoughts and presented some examples on my blog:

1. Overlay plots on a box plot in SAS: Discrete X axis

2. Overlay plots on a box plot in SAS: Continuous X axis

 
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 326 views
  • 0 likes
  • 4 in conversation