Data visualization with SAS programming

How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

Accepted Solution Solved
Reply
Super Contributor
Posts: 395
Accepted Solution

How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

Hello everyone,

 

I have two datasets and I’m trying to create Box-Whisker graph I needed to take both “Column1” and “Column2” from  "have/2" datasets. I appended the datasets by the help of "data Want” step. Then I used Proc Format and created Box-Whisker graph by using PROC SGPLOT.Now, I need to add legendlabel for two Box-Whisker graph. The default legendlabel put a line symbol for Column2 but I need to give different color for Box-Whiskers and show the symbols with square and their own color. My second question is how can I show value of Median and Mean on Box-Whisker graph and is it possible to change color of Mean and Median line and show as legendlabel by thier own color.

 

data have;
Length ID $ 20 Column1 8;
infile datalines missover dlm="|";
Input ID Column1;
datalines;
0-1|0.03
0-2|0.01
0-3|0.05
0-4|0.07
0-5|0.02
0-6|0.04
0-7|0.06
0-8|0.08
0-9|0.09
0-10|0.03
;
Run;
data have2;
Length ID $ 20 Column2 8;
infile datalines missover dlm="|";
Input ID Column2;
datalines;
0-1|0.01
0-2|0.03
0-3|0.05
0-4|0.04
0-5|0.1
0-6|0.08
0-7|0.09
0-8|0.03
0-9|0.06
0-10|0.1
;
Run;

data want;
set have(keep=Column1 rename=(Column1=ID))
    have2 (keep=Column2 rename=(Column2=ID) in=Data);
Variable = Data;
run;

proc format ;
	value Column
	0='Column1'
	1='Column2';
run;

proc datasets lib=work nodetails nolist;
modify Want;
format Variable column.;
run;
quit;
proc sgplot data=Want nocycleattrs /*noautolegend*/;
vbox ID / category=Variable Legendlabel="Column 1" connect=mean fillattrs=(color=lightsalmon) extreme;
vbox ID / category=Variable Legendlabel="Column 2" connect=median Nofill fillattrs=(color=lightcyan) extreme;
*keylegend / location=inside position=topright across=1;
yaxis label="Column 1"  /*type=linear*/;
xaxis label="Column 2";
run;

Want.png

 

Thank you.


Accepted Solutions
Solution
‎12-16-2015 04:43 PM
SAS Super FREQ
Posts: 1,134

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

[ Edited ]

All Replies
Super User
Super User
Posts: 7,682

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

I would switch to GTL (Graph Template Language), then you can use overlay's to overlay each part of the graph and assign colors patterns to each bit.  Here is an example compared with an sgplot:

http://blogs.sas.com/content/graphicallyspeaking/2012/02/16/distribution-of-maximum-lft-by-treatment...

To add to the just add another statement into the template to add a line plot in for your mean connector data and set the styles and color as you want.

Super Contributor
Posts: 395

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

Hello,

 

I tried something to create my desired graph but I couldn't succeed. I still see the Box-Whisker graphs same color also I couldn't connect  Median&Mean point and show value of Median/Mean. Also I have to say that "data have" and "data have1" can have different row number. If I merge two dataset there can be missing values. I added my updated datasets.

 

 

data have;
Length ID $ 20 Column1 8;
infile datalines missover dlm="|";
Input ID Column1;
datalines;
0-1|0.03
0-2|0.01
0-3|0.05
0-4|0.07
0-5|0.02
0-6|0.04
0-7|0.06
0-8|0.08
0-9|0.09
0-10|0.03
;
Run;
data have2;/*Added two rows*/
Length ID $ 20 Column2 8;
infile datalines missover dlm="|";
Input ID Column2;
datalines;
0-1|0.01
0-2|0.03
0-3|0.05
0-4|0.04
0-5|0.1
0-6|0.08
0-7|0.09
0-8|0.03
0-9|0.06
0-10|0.1
0-11|0.08
0-12|0.03
;
Run;

data want;
set have(keep=Column1 rename=(Column1=ID))
    have2 (keep=Column2 rename=(Column2=ID) in=Data);
Variable = Data;
run;

proc format ;
	value Column
	0='Column1'
	1='Column2';
run;

proc datasets lib=work nodetails nolist;
modify Want;
format Variable column.;
run;
proc template;
  define statgraph Test;
  	begingraph;
		entrytitle 'Test of Box-Whisker';
		layout overlay / cycleattrs=true yaxisopts=(label='THK')
                       xaxisopts=(label='Dataset');
		boxplot x=Variable y=ID /  display=(median mean outliers caps fill) 
             connect=mean name='first' legendlabel='Can';
	*	boxplot x=Variable y=ID / display=(median mean outliers caps fill) 
             connect=median ;
			 discretelegend 'first'/ location=inside halign=right valign=top across=1;
		  endlayout;
	endgraph;
  end;

proc sgrender data=want template=Test;
run;

 

Thank you.

 

SAS Super FREQ
Posts: 1,134

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

With SGPLOT you can get one connect line OR group colors.

 

proc sgplot data=Want nocycleattrs /*noautolegend*/;
  styleattrs datacolors=(lightsalmon lightcyan) datacontrastcolors=(black);
  vbox ID / category=Variable group=variable  extreme;
run;

 

BoxConnect.png

 

In the graph above, colors are created using the GROUP option.  So, the two boxes belong to different group, so they cannot be connected.  In such a case, the connect will have to be done using some other means, like Annotate.  Note group colors can be set inline in SAS 9.4 using STYLEATTRS statement.

 

Without the two colors (GROUP), you can connect by one statistic.

 

proc sgplot data=Want nocycleattrs /*noautolegend*/;
  vbox ID / category=Variable connect=mean extreme;
run;

 

BoxConnect.png

Super Contributor
Posts: 395

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

I'm happy to see your reply.

Thank you for your help.

As far as I understand it is not possible to show both connected Mean&Median and Box-Whisker graphs with different colors/legendlabel even if I use Proc Template or use other procedures.

 

I saw a link related to display Mean and Median values on Box Plot but I think it is a little bit complicated. It can exceed my current level.

 

http://support.sas.com/kb/46/719.html 

 

Thank you.

SAS Super FREQ
Posts: 1,134

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

Actually, that is not what I am suggesting.  The cases above are easy to do with the built in options.  If you want to get colored boxes, and connect by both mean and median (or some other statistics), you will have to do some extra work yourself possibly using proc MEANS.  I will write up an example for you.  What release of SAS are you using?  

Super Contributor
Posts: 395

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

Hello @Sanjay_SAS,

Thank you for your helps.

I'm using 9.4(TS1M0) Enterprise Guide 6.1 .It would be good for me to see an example. Lastly, is it possible to change color of connected Mean and Median and show as legendlabel?

 

Thank you.

 

Solution
‎12-16-2015 04:43 PM
SAS Super FREQ
Posts: 1,134

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

[ Edited ]
Super Contributor
Posts: 395

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

[ Edited ]

Hello @Sanjay_SAS,

Firstly, sorry for the late reply I was little bit busy. Secondly, I would like to thank to you. When I saw your writing on the blog I was surpised and I was happy. It helped me a lot and it was instructive. I also wonder is it possible to show Median and Mean value on Box-Plot ? When I run your code on my environment I got the error but after I comment out “” the code worked. I also got the “ERROR: Attempting to overlay incompatible plot or chart types.” error what is the reason I’m getting this error. After I tried to add different LinePattern values in LinePattern variables which are listed in the table as below they weren’t work why is the reason for ? I created my desired output as below by using your code but I have last step which is adding Median&Mean value on Boxplot. I would like to ask you what %let gpath='' %let dpi=200 refer to ? To execute my program I needed to comment out this codes.

 

Here is my code  :

 

data Want;
set Want;
NewVariable=put(left(Variable),$12.);
If NewVariable="0" THEN NewVariable="Column1";
Else NewVariable="Column2";
run;
/*****************************************
*			TEST						*
*										*
*****************************************/
proc means data=Want noprint;
  class NewVariable;
  var id;
  output out=Want2 (where=(_type_ > 0) keep=NewVariable mean median  _type_)
    mean = mean
    median = median;
  run;
data Want3;
  length Group $6;
  keep NewVariable Group Value;
  set Want2;
  Group='Mean'; Value=Mean; output;
  Group='Median'; Value=Median; output;
run;
Proc sort data=Want3;
By Group;
Run;

data sganno;
Length Label $6 DrawSpace $12;
Drop NewVariable Group Value;
set Want3 end=last;
By group;
DrawSpace='DataValue';
  LineThickness=1;
  if first.group then Function='PolyLine';
  else Function='PolyCont';
  if group='Mean' then LinePattern='Solid';
  else LinePattern='Dot';
  XC1=NewVariable; Y1=Value; output;

  if last then do;
  DrawSpace='WallPercent'; Width=50;
  LinePattern='Solid'; 
  Function='Line'; x1=01; y1=12; x2=05; y2=12; output;
  Function='Text'; x1=05; y1=12; Label='Mean'; anchor='left'; output;

  LinePattern='Dot'; 
  Function='Line'; x1=01; y1=16; x2=05; y2=16; output;
  Function='Text'; x1=05; y1=16; Label='Median'; anchor='left'; output;
  end; 
run; 

proc sgplot data=Want SGANNO=sganno nocycleattrs ;
vbox ID / category=NewVariable group=NewVariable  extreme;
 
keylegend / location=inside position=bottomleft across=1;
yaxis label=" ";
xaxis label=" ";
run;

Here is the LinePattern Values Table :

 

LİnePatternValue.png

 

Thanks a lot.

SAS Super FREQ
Posts: 1,134

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

If you are getting error messages about overlaying incompatible plot types for VBOX and SERIES, it means you have a SAS release that is prior to 9.40M1.  That is why I included the second example using SGAnnotate that will work at SAS 9.3.  I added the connect lines, but you can also add TEXT functions to display the mean and median values themselves either at the bottom/top of graph area or at the mean/median value themselves.

 

You can ignore GPATH and DPI.  I use those to control where the graph is written and its resolution.

Super Contributor
Posts: 395

Re: How To - Show Variables Legendlabel and Median&Mean Value on Box-Whisker Graph

Hello @Sanjay_SAS,

Thank you for your help. I understood better now but I couldn't show the value of Mean/Median on Box-Plot. I tried to use macro variable but couldn't succeed. Your answer has already sufficient for me.

Thanks a lot.

🔒 This topic is solved and locked.

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

Discussion stats
  • 10 replies
  • 845 views
  • 0 likes
  • 3 in conversation