Published on :
Graph CREATION_INTERNE

Annotated Statistical Visualization with PROC GPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
This script first generates a test dataset containing various physical measurements classified by type (TEST). It then uses the MEANS procedure to calculate the mean, minimum, and maximum for each category. A complex DATA step constructs an annotation dataset (Annotate Facility) to manually draw horizontal error bars representing the Min-Max interval and mark the mean. Finally, the GPLOT procedure (SAS©/GRAPH) displays the result. Note: This code uses older graphical procedures (SAS©/GRAPH) instead of modern ODS Graphics procedures (SGPLOT).
Data Analysis

Type : CREATION_INTERNE


The data is defined directly in the script via the CARDS statement in the DATA step 'a'.

1 Code Block
DATA STEP Data
Explanation :
Initialization of global graphics options and creation of the source dataset 'a' containing the variables TEST (category) and BREAKS (value).
Copied!
1/* Set graphics options */
2goptions reset=all cback=white border;
3 
4/* Set system options */
5DATA a;
6INPUT TEST $ BREAKS ;
7CARDS;
8Cold 5
9Cold 12
10Cold 14
11Cold 22
12Cold 52
13Heat 20
14Heat 25
15Heat 10
16Heat 22
17Heat 47
18Gases 12
19Gases 25
20Gases 33
21Gases 48
22Gases 24
23Pressure 10
24Pressure 12
25Pressure 14
26Pressure 22
27Pressure 60
28Xrays 20
29Xrays 25
30Xrays 14
31Xrays 22
32Xrays 29
33Humidity 20
34Humidity 25
35Humidity 33
36Humidity 40
37Humidity 24
38;
39RUN;
2 Code Block
PROC SORT
Explanation :
Sorting data by the group variable 'TEST' to prepare for aggregation.
Copied!
1/* Sort
2data by variable TEST */
3PROC SORT;
4BY TEST;
5 
6RUN;
7 
3 Code Block
PROC MEANS Data
Explanation :
Calculation of descriptive statistics (mean, standard deviation, min, max) grouped by 'TEST'. Results are stored in output table 'b'.
Copied!
1/**************************************************/
2/* Create an output data set, B using PROC MEANS */
3/* that contain new variables, MEAN, STD, STDERR, */
4/* MIN, and MAX. */
5/**************************************************/
6 
7PROC MEANS mean std stderr min max DATA=a;
8BY TEST;
9OUTPUT out=b mean=mean min=min max=max;
10RUN;
4 Code Block
DATA STEP Data
Explanation :
Creation of a special 'Annotate' dataset. It defines graphical commands (move, draw) to draw blue lines from min to max, and red markers for the mean, min, and max. Uses the data coordinate system (xsys='2', ysys='2').
Copied!
1/****************************************************************/
2/* Create an annotate data set, ANNO to draw the bars at +/- 1, */
3/* 2, or 3 Standard Deviation or Standard Error of the mean. */
4/****************************************************************/
5 
6DATA anno;
7retain xsys ysys '2' when 'a';
8LENGTH color function $8 ;
9SET b;
10 
11/* Draw the horizontal line from min to max */
12function='move'; xsys='2'; ysys='2'; yc=TEST; x=min; color='blue'; OUTPUT;
13function='draw'; x=max; color='blue'; size=2; OUTPUT;
14 
15/* Draw the MEAN horizontal line making the SIZE bigger */
16function='move'; xsys='2';ysys='2';yc=TEST;x=mean; color='red'; OUTPUT;
17function='draw'; x=mean; ysys='9'; y=+2; size=4; OUTPUT;
18function='draw'; x=mean; y=-4; size=4; OUTPUT;
19 
20/* Draw the line for the MIN value */
21function='move';xsys='2';ysys='2';yc=TEST;x=min;color='red';OUTPUT;
22function='draw';x=min;ysys='9';y=+2;size=2;OUTPUT;
23function='draw';x=min;y=-4;size=2;OUTPUT;
24 
25/* Draw the line for the MAX value */
26function='move';xsys='2';ysys='2';yc=TEST;x=max;color='red';OUTPUT;
27function='draw';x=max;ysys='9';y=+2;size=2;OUTPUT;
28function='draw';x=max;y=-4;size=2;OUTPUT;
29RUN;
5 Code Block
PROC GPLOT
Explanation :
Generation of the final graph. The GPLOT procedure uses the 'anno' dataset to superimpose custom drawings on the defined axes. The horizontal axis is set from 0 to 100.
Copied!
1axis1 order=(0 to 100 BY 10);
2symbol1 i=none v=none c=black;
3 
4PROC GPLOT DATA=b ;
5plot test*mean / anno=anno haxis=axis1 href=30 60 90; /* The HREF= option draws reference lines */
6RUN;
7QUIT;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.