Basic Plots and Charts

About Basic Plots and Charts

You can use the SGPLOT and SGPANEL procedures to produce basic plots and charts.
The plot and chart statements include options for controlling how the output is displayed. Many of the options are unique to the particular plot or chart. However, some general options apply to most of the basic plots and charts.
For example, options enable you to do the following:
  • specify colors, line attributes, and other visual features.
  • group the data by the values of a variable. A separate plot is created for each unique value of the grouping variable. The plot elements for each group value are automatically distinguished by different visual attributes.
  • use a secondary axis (X2 or Y2). This option is available only for the SGPLOT procedure.
  • reference an ID variable in attribute map data set. You specify this option only if you are using an attribute map to control visual attributes of the graph.
The basic plots and charts are described in the following sections. If you run the examples, your output might differ somewhat depending on the size of your graphics. The examples here were specified to be a particular size using the following statement:
ods graphics on / width=4in;

About Band Plots

A band plot creates a band that highlights part of the plot and shows upper and lower limits. The input data should be sorted by the X or Y variable.
The following examples show upper and lower mean weight values for a class of students. The first two examples use the SGPLOT procedure to show the same band plotted along the X axis and the Y axis, respectively. The third example uses the SGPANEL procedure to show a matrix that is paneled by gender.
Band plotted on the Y axis
title "Weight Limits on the Y Axis";
proc sgplot data=sashelp.classfit;
  where age > 12;
  band x=name lower=lowermean upper=uppermean;
run;
title;
Band plotted on the X axis
title "Weight Limits on the X Axis";
proc sgplot data=sashelp.classfit;
  where age > 12;
  band y=name lower=lowermean upper=uppermean;
run;
title;
Band panels
title "Weight Limits Panel by Gender";
proc sgpanel data=sashelp.classfit;
  panelby sex / uniscale=row
             spacing=8;
  where age > 12;
  band x=name lower=lowermean upper=uppermean;
run;
title;
Options are available that enable you to customize the band plot and enhance its appearance. For example, you can do the following:
  • add labels to the upper and lower edges of the band, specify how the labels are positioned, and set other attributes for the labels
  • specify fill and outline attributes
  • specify legend labels and plot transparency
Note: This list does not include all available options.

See Also

BAND Statement (SGPANEL procedure)
BAND Statement (SGPLOT procedure)

About Bubble Plots

Bubble plots show the relative magnitude of the values of a variable. The values of two variables determine the position of the bubble on the plot, and the value of a third variable determines the size of the bubble.
The following examples show the height and weight values for a class. The size of each bubble is determined by the student’s age. Examples are provided for the SGPLOT and the SGPANEL procedures.
Bubble plot
proc sgplot data=sashelp.class;
  bubble x=height y=weight size=age;
run;
Bubble panel
proc sgpanel data=sashelp.class;
  panelby sex;
  bubble x=height y=weight size=age;
run;
Options are available that enable you to customize the bubble plot and enhance its appearance. For example, you can do the following:
  • control the size of the largest and the smallest bubble
  • specify fill and outline attributes, and data labels and their attributes
  • specify legend labels, plot transparency, and URLs for Web pages to be displayed when parts of the plot are selected within an HTML page
Note: This list does not include all available options.

See Also

BUBBLE Statement (SGPANEL procedure)
BUBBLE Statement (SGPLOT procedure)

About High-Low Charts

High-low charts show how several values of one variable relate to one value of another variable. Typically, each variable value on the horizontal axis has several corresponding values on the vertical axis.
The following examples show the stock trend for IBM during a particular year. The first two examples use the SGPLOT procedure to show the same plot along the X axis and the Y axis, respectively. The third example uses the SGPANEL procedure to show a paneled graph for Intel and Microsoft stock prices in the same year. Optional values have been specified for the closing stock prices, which are represented as tick marks on the high-low lines.
High-low plot, X axis
title "Stock Trend for IBM";
proc sgplot data=sashelp.stocks
  (where=(date >= "01jan2005"d and stock = "IBM"));
  highlow x=date high=high low=low
   / close=close;
run;
title;

High-low plot, Y axis
title "Stock Trend for IBM";
proc sgplot data=sashelp.stocks
  (where=(date >= "01jan2005"d and stock = "IBM"));
  highlow y=date high=high low=low
   / close=close;
run;
title;

High-low panel
title "Stock Trend for Intel and Microsoft";
proc sgpanel data=sashelp.stocks
  (where=(date >= "01jan2005"d and
  (stock = "Intel" or stock = "Microsoft")));
  panelby stock;
  highlow x=date high=high low=low
   / close=close;
run;
title;
Options are available that enable you to customize the high-low chart and enhance its appearance. For example, you can do the following:
  • use bars instead of lines to represent the data. If you use bars, then you can specify the fill and outline attributes for the bars.
  • show tick marks for the open and closing values.
  • specify labels and arrowheads for the high and low values.
  • control the display of grouped data. For example, you can specify whether the groups are overlaid or clustered, the width of each cluster, and the order of lines or bars within a group.
  • specify legend labels, plot transparency, and URLs for Web pages to be displayed when parts of the plot are selected within an HTML page.
Note: This list does not include all available options.

See Also

HIGHLOW Statement (SGPANEL procedure)
HIGHLOW Statement (SGPLOT procedure)

About Lines

About Reference Lines

You can add horizontal or vertical reference lines to your graphics. You can draw a reference line for each value of a specified variable. Or you can specify one or more explicit values for the reference lines.
The following examples show the height values for a class of students. A horizontal reference line is overlaid on a series plot to show the average height. Examples are provided for the SGPLOT and the SGPANEL procedures.
In the first example, a value of 60.8 is specified for the reference line. The second example uses the MEANS procedure to obtain the averages for males and females in the class. The SGPANEL procedure then specifies the variable that contains these averages in order to obtain the reference lines.
Refline plot
proc sgplot data=sashelp.class;
  where (sex="F");
  series x=name y=height;
  refline 60.8;
run;
Refline panel
proc means data=sashelp.class mean;
  var height;
  class sex;
  output out= classAverages
    mean=averageWeight;
run;

data classHeightAverage;
  set sashelp.class work.classAverages;
run;

proc sgpanel data=classHeightAverage;
  panelby sex / uniscale=row
    spacing=8;
  series x=name y=height;
  refline averageWeight;
run;
Options are available that enable you to customize the reference line and enhance its appearance. For example, you can do the following:
  • specify a horizontal or vertical line. In the SGPLOT procedure, you can associate the line with a secondary axis.
  • specify line attributes, labels, and label attributes.
  • specify legend labels and line transparency.
  • specify an amount to offset all lines from discrete axis values.
  • extend the plot axes to contain the reference lines.
Note: This list does not include all available options.

About Parameterized Lines

Parameterized lines are straight lines specified by a point and a slope. The statement must be used with another plot statement that is derived from data values that provide boundaries for the axis area. For example, the LINEPARM statement can be used with a scatter plot or a histogram.
The following example shows weight with respect to height for a class of students. A single line is generated by specifying values for the point and for the slope. The line in the example approximates a line of best fit.
Lineparm plot
proc sgplot data=sashelp.class
    noautolegend;
  scatter x=height y=weight;
  lineparm x=50 y=50 slope=3.89;
run;
You can generate multiple lines by specifying a numeric variable for any or all required arguments. Examples are provided for the SGPLOT and the SGPANEL procedures. The following two examples create lines of best fit for male and female participants in a heart disease study. The lines show weight with respect to height.
The examples first sort the data set by male and female participants. The sorted data is output to a data set named HEART.
proc sort data=sashelp.heart(keep=height weight sex)
     out=heart;
  by sex;
run;
The examples then use the REG procedure and output the regression statistics to a data set named STATS. The STATS data set includes the slope and the Y-intercept for the regression.
proc reg data=heart
     outest=stats(rename=(height=slope));
  by sex;
  model weight=height;
run;
Finally, the examples merge the HEART and the STATS data sets.
data heartStats;
  merge heart stats(keep=intercept slope sex);
run;
The first example uses the SGPLOT procedure to show lines of best fit for females and males in the study. The regression lines are labeled and have their own legend.
Lineparm regression plot
proc sgplot data=heartStats;
  scatter x=height  y=weight;
  lineparm x=0 y=intercept slope=slope /
     name="Line" group=sex
     curvelabel;
  keylegend "Line";
run;
The following example uses the SGPANEL procedure to create the same information, which is paneled by gender.
Lineparm regression panel
proc sgpanel data=heartStats
   noautolegend;
  panelby sex;
  scatter x=height  y=weight;
  lineparm x=0 y=intercept slope=slope;
run;
Options are available that enable you to customize the line and enhance its appearance. For example, you can do the following:
  • specify line attributes, labels, and label attributes
  • specify legend labels and line transparency
  • prevent the line from being extended beyond the axis offset
Note: This list does not include all available options.

See Also

REFLINE Statement (SGPANEL procedure)
REFLINE Statement (SGPLOT procedure)
LINEPARM Statement (SGPANEL procedure)
LINEPARM Statement (SGPLOT procedure)

About Needle Plots

Needle plots use vertical line segments, or needles, to connect each data point to a baseline.
The following examples show the stock trend during a particular year. Examples are provided for the SGPLOT and the SGPANEL procedures. Each example specifies an optional baseline value on the Y axis.
Needle plot
title "Stock Trend for IBM";
proc sgplot data=sashelp.stocks
  (where=(date >= "01jan2005"d and stock = "IBM"));
  needle x=date y=close / baseline=80;
run;
title;
Needle plot
title "Stock Trend for Intel and Microsoft";
proc sgpanel data=sashelp.stocks
  (where=(date >= "01jan2005"d and
  (stock = "Intel" or stock = "Microsoft")));
  panelby stock;
  needle x=date y=close / baseline=24;
run;
title;
Options are available that enable you to customize the needle plot and enhance its appearance. For example, you can do the following:
  • specify a baseline value, as shown in the example.
  • add markers to the tips of the needles and specify marker attributes.
  • add data labels and specify label attributes.
  • control the display of grouped data. For example, you can specify whether the groups are overlaid or clustered, the width of each cluster, and the order of needles within a group.
  • specify an amount to offset all needle lines from discrete X values.
  • specify legend labels, plot transparency, and URLs for Web pages to be displayed when parts of the plot are selected within an HTML page.
Note: This list does not include all available options.

See Also

NEEDLE Statement (SGPANEL procedure)
NEEDLE Statement (SGPLOT procedure)

About Scatter Plots

Scatter plots show the relationship of one variable to another, often revealing concentrations or trends in the data. Typically, each variable value on the horizontal axis can have any number of corresponding values on the vertical axis.
The following examples show the relationship of height to weight for a class of students. Examples are provided for the SGPLOT and the SGPANEL procedures. The third example includes error bars.
Scatter plot
title "Height and Weight";
proc sgplot data=sashelp.class;
  scatter x=height y=weight;
run;
title;
Scatter plot panels
title "Height and Weight Panels by Gender";
proc sgpanel data=sashelp.class;
  panelby sex;
  scatter x=height y=weight;
run;
title;
Scatter plot with error bars
proc sgplot data=sashelp.classfit;
scatter x=height y=weight /
  yerrorlower=lower yerrorupper=upper;
run;
Options are available that enable you to customize the scatter plot and enhance its appearance. For example, you can do the following:
  • add and customize error bars. The previous example shows error bars.
  • specify how many times observations are repeated for computational purposes.
  • specify marker attributes, data labels, and label attributes.
  • specify an amount to offset all markers from discrete axis values.
  • control the display of grouped data. For example, you can specify whether the groups are overlaid or clustered, the width of each cluster, and the order of markers within a group
  • specify legend labels, plot transparency, and URLs for Web pages to be displayed when parts of the plot are selected within an HTML page.
Note: This list does not include all available options.

See Also

SCATTER Statement (SGPANEL procedure)
SCATTER Statement (SGPLOT procedure)

About Series Plots

Series plots display a series of line segments that connect observations of input data.
The following examples show series plots of stock trends. Examples are provided for the SGPLOT and the SGPANEL procedures.
Series plot
title "Stock Trend for IBM";
proc sgplot data=sashelp.stocks
  (where=(date >= "01jan2002"d and stock = "IBM"));
  series x=date y=close;
run;
title;
Series panel
title "Stock Trend for IBM and Microsoft";
proc sgpanel data=sashelp.stocks
  (where=(date >= "01jan2002"d and 
  (stock = "IBM" or stock = "Microsoft")));
  panelby stock;
  series x=date y=close;
run;
title;
Options are available that enable you to customize the series plot and enhance its appearance. For example, you can do the following:
  • specify line and marker attributes, data labels, curve labels, and label attributes.
  • specify an amount to offset all markers from discrete axis values.
  • control the display of grouped data. For example, you can specify whether the groups are overlaid or clustered, the width of each cluster, and the order of lines within a group
  • specify legend labels, plot transparency, and URLs for Web pages to be displayed when parts of the plot are selected within an HTML page.
Note: This list does not include all available options.

See Also

SERIES Statement (SGPANEL procedure)
SERIES Statement (SGPLOT procedure)

About Step Plots

Step plots display a series of horizontal and vertical line segments that connect observations of input data. The plots use a step function to connect the data points. The vertical line can change at each step.
The following examples show step plots of stock trends. Examples are provided for the SGPLOT and the SGPANEL procedures.
Step plot
title "Stock Trend for IBM";
proc sgplot data=sashelp.stocks
  (where=(date >= "01jan2004"d and stock = "IBM"));
  step x=date y=close;
run;
Step panel
title "Stock Trend for Intel and Microsoft";
proc sgpanel data=sashelp.stocks
  (where=(date >= "01jan2004"d and 
  (stock = "Intel" or stock = "Microsoft")));
  panelby stock;
  step x=date y=close;
run;
title;
Options are available that enable you to customize the step plot and enhance its appearance. For example, you can do the following:
  • add and customize markers and error bars.
  • specify line attributes, data labels, curve labels, and label attributes.
  • specify an amount to offset all step lines from discrete axis values.
  • control the display of grouped data. For example, you can specify whether the groups are overlaid or clustered, the width of each cluster, and the order of lines within a group
  • specify legend labels, plot transparency, and URLs for Web pages to be displayed when parts of the plot are selected within an HTML page.
Note: This list does not include all available options.

See Also

STEP Statement (SGPANEL procedure)
STEP Statement (SGPLOT procedure)

About Text Insets

A text inset provides an easy way to add text to a graphic. You can insert a text string as well as a series of label-value pairs.
The following example shows a linear regression curve with a text inset in the upper left corner. Text insets are available only for the SGPLOT procedure. The SGPANEL procedure does not support text insets.
Text inset with a plot
proc sgplot data=sashelp.class;
  reg x=height y=weight / CLM;
  inset ("Confidence level (*ESC*)
    {unicode alpha} =" ="0.05");
run;
Options are available that enable you to customize the text inset and enhance its appearance. For example, you can do the following:
  • show or hide a border
  • position the text box within the plot
  • specify text attributes, add a title, and specify title attributes
  • align the labels and values when you specify label-value pairs
Note: This list does not include all available options.

See Also

INSET Statement (SGPLOT procedure)

About Vector Plots

Vectors are directed line segments. A vector plot is a two-dimensional graphic that uses vectors to represent both direction and magnitude at each point.
The following examples show the relationship of height to weight for a class of students. Examples are provided for the SGPLOT and the SGPANEL procedures. Both examples specify optional X and Y origins and data labels.
Vector plot
title "Height and Weight - Females";
proc sgplot data=sashelp.class;
  where(sex="F");
  vector x=height y=weight
   / yorigin=75 xorigin=60
     datalabel;
run;
title;
Vector panel
title "Height and Weight - All";
proc sgpanel data=sashelp.class;
  panelby sex;
  vector x=height y=weight
   / yorigin=75 xorigin=60
     datalabel;
run;
title;
Options are available that enable you to customize the vector plot and enhance its appearance. For example, you can do the following:
  • specify the origin, as shown in the examples.
  • specify line attributes, data labels, and data label attributes. You can also show or hide the arrows, change the arrowhead shape, and the change the arrow direction.
  • specify legend labels and plot transparency.
Note: This list does not include all available options.

See Also

VECTOR Statement (SGPANEL procedure)
VECTOR Statement (SGPLOT procedure)