Published on :
Reporting CREATION_INTERNE

Graphs with broken axes

This code is also available in: Deutsch Español Français
Awaiting validation
The script initializes ODS options for graphic output. It creates two internal datasets: 'TallBar' via datalines to demonstrate a bar chart with an extreme value, and 'outOfRange' via a generative DATA STEP for a scatter plot with distributed values and an outlier. Then, it uses PROC SGPLOT to create a simple bar chart, a bar chart with a 'Full' broken Y-axis, a scatter plot with a 'Bracket' broken Y-axis, and a scatter plot with a 'Spark' broken Y-axis while configuring axis extensions.
Data Analysis

Type : CREATION_INTERNE


All data used ('TallBar' and 'outOfRange') is created directly within the SAS script via DATA STEPS and datalines or random data generation, without dependency on external sources or the SASHELP library.

1 Code Block
Macro Variables/ODS
Explanation :
Defines macro variables `gpath` for the image output path and `dpi` for dots per inch resolution. Closes the default ODS HTML destination and opens the ODS LISTING destination with a 'listing' style, configuring the image output path and resolution.
Copied!
1%let gpath='.';
2%let dpi=200;
3ods html close;
4ods listing style=listing gpath=&gpath image_dpi=&dpi;
2 Code Block
DATA STEP Data
Explanation :
Creates the SAS dataset named `TallBar`. It contains two variables: `X` (character) and `Y` (numeric). The data is provided directly in the script via the `datalines` statement, including a significantly larger value (400 for 'E') which will be used to illustrate the need for a broken axis.
Copied!
1DATA TallBar;
2 INPUT X $ Y;
3 DATALINES;
4A 10
5B 15
6C 12
7D 17
8E 400;
9RUN;
3 Code Block
PROC SGPLOT
Explanation :
Generates a simple vertical bar chart. `ods graphics` resets graphics parameters, sets the width, height, and output image name. `proc sgplot` is used with the `TallBar` dataset, and the `vbar x` statement creates the bars, `response=y` specifies the response variable, `nostatlabel` suppresses statistical labels, and `fillattrs` applies fill attributes.
Copied!
1ods graphics / reset width=5in height=3in imagename='Bar';
2PROC SGPLOT DATA=tallbar;
3 vbar x / response=y nostatlabel fillattrs=graphdata1;
4 RUN;
4 Code Block
PROC SGPLOT
Explanation :
Creates a bar chart with a 'broken' Y-axis to handle the extreme value of 'E'. The `yaxis ranges` option divides the Y-axis into two segments (from `min` to 44 and from 384 to `max`), visually creating a break. `baselineattrs=(thickness=0)` makes the baseline invisible, and `values` sets the axis ticks.
Copied!
1ods graphics / reset width=5in height=3in imagename='BarBrokenAxisFull';
2PROC SGPLOT DATA=tallbar;
3 vbar x / response=y nostatlabel fillattrs=graphdata2 baselineattrs=(thickness=0);
4 yaxis ranges=(min-44 384-max) values=(0 to 400 BY 10);
5 RUN;
5 Code Block
DATA STEP Data
Explanation :
Creates the SAS dataset named `outOfRange` for a scatter plot. It generates 100 observations with random values for `x` and `y` using the `ranuni` function. An additional observation `(x=0.5, y=9.1)` is manually added to create a standout value, useful for demonstrating broken axes in scatter plots.
Copied!
1DATA outOfRange;
2 keep x y;
3 DO i=1 to 100;
4 x=ranuni(2); y=ranuni(2) + 0.3*x; OUTPUT;
5 END;
6 x=0.5; y=9.1; OUTPUT;
7RUN;
6 Code Block
PROC SGPLOT
Explanation :
Generates a scatter plot with a 'bracket' style broken Y-axis. `ods listing style=analysis` changes the output style. `styleattrs axisbreak=bracket` activates the 'bracket' style for the axis break. The `reg` statement adds a linear regression line with confidence limits (clm). `yaxis ranges` and `values` configure the Y-axis display.
Copied!
1ods listing style=analysis;
2ods graphics / reset width=5in height=3in imagename='ScatterBrokenAxisBracket';
3PROC SGPLOT DATA=outOfRange;
4 styleattrs axisbreak=bracket;
5 reg x=x y=y / clm markerattrs=(size=5);
6 yaxis ranges=(min-1.5 8.9-max) values=(0 to 10 BY 0.2) valueshint;
7 RUN;
7 Code Block
PROC SGPLOT
Explanation :
Creates a scatter plot with a 'spark' style broken Y-axis. `ods listing style=journal` changes the output style. `styleattrs axisbreak=spark axisextent=data` activates the 'spark' style for the axis break and ensures axis extensions are data-based. `nowall` and `noborder` respectively remove the background and border of the graph. The Y-axis is configured similarly to the previous graph to show the break.
Copied!
1ods listing style=journal;
2ods graphics / reset width=5in height=3in imagename='ScatterBrokenAxisSpark';
3PROC SGPLOT DATA=outOfRange nowall noborder;
4 styleattrs axisbreak=spark axisextent=DATA ;
5 reg x=x y=y / clm markerattrs=(size=5);
6 yaxis ranges=(min-1.5 8.9-max) values=(0 to 10 BY 0.2) valueshint;
7 RUN;
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.