Modifying a Scatter Plot

After producing a scatterplot of a data set we might like to add a line, a regression line for example, to the plot. As an example, Devore and Peck [8, page 105, Example 2] describe a data set collected to examine the effect of bicycle lanes on drivers and bicyclists. The variables given by
(def travel-space (list 12.8 12.9 12.9 13.6 14.5 14.6 15.1 17.5 19.5 20.8))
(def separation (list 5.5 6.2 6.3 7.0 7.8 8.3 7.1 10.0 10.8 11.0))
represent the distance between the cyclist and the roadway center line and the distance between the cyclist and a passing car, respectively, recorded in ten cases. A regression line fit to these data, with "2D separation as the dependent variable, has a slope of 0.66 and an intercept of -2.18. Let's see how to add this line to a scatterplot of the data.

We can use the expression

(plot-points travel-space separation)
to produce a scatterplot of these points. To be able to add a line to the plot, however, we must be able to refer to it within XLISP-STAT. To accomplish this let's assign the result returned by the "2D plot-points function to a symbol 15:
(def myplot (plot-points travel-space separation))
The result returned by plot-points is an xlisp object . To use an object you have to send it messages. This is done using the "2D send function, as in the expression
(send object message argument1 ... )
I will use the expression
(send myplot :abline -2.18 0.66)
to tell "2D myplot to add the graph of a line a + bx, with a = - 2.18 and b = 0.66, to itself. The message selector is "2D :abline, the numbers -2.18 and 0.66 are the arguments. The message consists of the selector and the arguments. Message selectors are always Lisp keywords; that is, they are symbols that start with a colon. Before typing in this expression you might want to resize and rearrange the listener window so you can see the plot and type commands at the same time. Once you have resized the listener window you can easily switch between the small window and a full size window by clicking in the zoom box at the right corner of the window title bar. The resulting plot is shown in Figure [*]
Figure: Scatterplot of bicycle data with fitted line.
\begin{figure}\centering
\vspace{3.47in}
\parbox[t]{3.8in}{}
\end{figure}

Scatter plot objects understand a number of other messages. One other message is the "2D :help message 16:

> (send myplot :help)
> (send scatterplot-proto :help)
SCATTERPLOT-PROTO
Scatterplot.
Help is available on the following:

:ABLINE :ACTIVATE :ADD-FUNCTION :ADD-LINES :ADD-METHOD :ADD-MOUSE-MODE 
:ADD-POINTS :ADD-SLOT :ADD-STRINGS :ADJUST-HILITE-STATE
:ADJUST-POINT-SCREEN-STATES :ADJUST-POINTS-IN-RECT :ADJUST-TO-DATA
:ALL-POINTS-SHOWING-P :ALL-POINTS-UNMASKED-P :ALLOCATE
:ANY-POINTS-SELECTED-P :APPLY-TRANSFORMATION :BACK-COLOR :BRUSH
:BUFFER-TO-SCREEN :CANVAS-HEIGHT :CANVAS-WIDTH :CLEAR :CLEAR-LINES
:CLEAR-MASKS :CLEAR-POINTS :CLEAR-STRINGS ....

The list of topics will be the same for all scatter plots but will be somewhat different for rotating plots, scatterplot matrices or histograms. Instance slots are internal storage locations in the object; you can ignore them for now.

The "2D :clear message, as its name suggests, clears the plot and allows you to build up a new plot from scratch. Two other useful messages are "2D :add-points and "2D :add-lines. To find out how to use them we can use the "2D :help message with "2D :add-points or "2D :add-lines as arguments:

> (send myplot :help :add-points)
:ADD-POINTS
Method args: (points &key point-labels (draw t))
Adds points to plot. POINTS is a list of sequences, POINT-LABELS a list of
strings. If DRAW is true the new points are added to the screen.
NIL
> (send myplot :help :add-lines)
:ADD-LINES
Method args: (lines &key type (draw t))
Adds lines to plot. LINES is a list of sequences, the coordinates of the line 
starts. TYPE is normal or dashed. If DRAW is true the new lines are added to the
screen.
NIL
>

The plot produced above shows some curvature in the data. A regression of "2D separation on a linear and a quadratic term in "2D travel-space produces estimates of -16.41924 for the constant, 2.432667 as the coefficient of the linear term and -0.05339121 as the coefficient of the quadratic term. Let's use the "2D :clear, "2D :add-points and "2D :add-lines messages to change "2D myplot to show the data along with the fitted quadratic model. First we use the expressions

(def x (rseq 12 22 50))
(def y (+ -16.41924 (* 2.432667 x) (* -0.05339121 (* x x))))
to define "2D x as a grid of 50 equally spaced points between 12 and 22 and "2D y as the fitted response at these "2D x values. Then the expressions
(send myplot :clear)
(send myplot :add-points travel-space separation)
(send myplot :add-lines x y)
change myplot to look like Figure [*].
Figure: Scatterplot of bicycle data with fitted curve.
\begin{figure}\centering
\vspace{3.47in}
\parbox[t]{3.8in}{}
\end{figure}
Of course we could have used "2D plot-points to get a new plot and just modified that plot with "2D :add-lines, but the approach used here allowed us to try out all three messages.