TeachingDemos (version 2.12)

TkSpline: Plot a set of data in a Tk window and interactively move a line to see predicted y-values from a spline fit corresponding to selected x-values.

Description

This function plots a dataset in a Tk window then draws the spline fit through the points. It places a line to show the predicted y from the given x value. The line can be clicked on and dragged to new x-values with the predicted y-values automatically updating. A table at the bottem of the graph shows the values and the 3 derivatives.

Usage

TkSpline(x, y, method='natural', snap.to.x=FALSE, digits=4,
                     col=c('blue','#009900','red','black'),
                     xlab=deparse(substitute(x)), ylab=deparse(substitute(y)),
                     hscale=1.5, vscale=1.5, wait=TRUE,
                     ...)

Value

If wait is FALSE then an invisible NULL is returned, if

wait is TRUE then an invisible list with the x and y values and derivatives is returned.

Arguments

x

The x-values of the data, should be sorted

y

The corresponding y-values of the data

method

Spline Method, passed to splinefun

snap.to.x

Logical, if TRUE then the line will only take on the values of x

digits

Number of digits to print, passed to format

col

Colors of the prediction and other lines

xlab

Label for the x-axis, passed to plot

ylab

Label for the y-axis, passed to plot

hscale

Horizontal scaling, passed to tkrplot

vscale

Vertical scaling, passed to tkrplot

wait

Should R wait for the window to close

...

Additional parameters passed to plot

Author

Greg Snow 538280@gmail.com

Details

This provides an interactive way to explore predictions from a set of x and y values. Internally the function splinefun is used to make the predictions.

The x-value of the reference line can be changed by clicking and dragging the line to a new position. The x and y values are shown in the margins of the graph. Below the graph is a table with the y-value and derivatives.

See Also

splinefun, TkApprox

Examples

Run this code
if(interactive()) {
  x <- 1:10
  y <- sin(x)
  TkSpline(x,y, xlim=c(0,11))
}

Run the code above in your browser using DataCamp Workspace