Scatter Plots

You can use the SCATTER subroutine to create a scatter plot. The subroutine requires two vector arguments: values for the X variable and values for the Y variable.

The following statements read the MPG_City and MPG_Highway variables from the Sashelp.Cars data set and create a simple scatter plot. The plot is shown in Figure 15.8.

proc iml;
use Sashelp.Cars;
read all var {MPG_City MPG_Highway Origin};
close Sashelp.Cars;

title "Scatter Plot with Default Properties";
run Scatter(MPG_City, MPG_Highway);

Figure 15.8: Scatter Plot


For a more complicated example, the following statements create a scatter plot by using the GROUP=, OTHER=, LABEL=, LINEPARM=, and YVALUES= options. The result is shown in Figure 15.9.

title "Scatter Plot with Data Labels and a Diagonal Line";
run Scatter(MPG_City, MPG_Highway)
    group=Origin                  /* assign color/marker shape */
    other="refline 25 50 /axis=y" /* add reference line */
    label={"MPG_City" "MPG_Highway"}
    lineparm={0 6.15 1.03}  /* line through (0,0) with slope 0.2 */
    yvalues=do(15,60,15);

Figure 15.9: Marker and Axis Attributes


The following list explains the options that are used to create Figure 15.9:

  • The GROUP= option specifies a vector of values that determine groups in the plot. In Figure 15.9, the marker attributes correspond to values of the Origin variable.

  • The OTHER= option specifies statements in the SGPLOT procedure. In Figure 15.9, the REFLINE statement draws two horizontal lines in the plot.

  • The LABEL= option specifies axis labels for the X or Y axis. In Figure 15.9, both axes are labeled.

  • The LINEPARM= option specifies a three-element vector whose elements specify a point and a slope for a line. In Figure 15.9, the line goes through the point $(0, 6.15)$ and has a slope of 1.03.

  • The YVALUES= option specifies a vector of values for ticks for the Y axes. In Figure 15.9, the tick marks on the vertical axis are spaced 15 units apart in the interval $[15, 60]$.