Displaying Graphical Output from R

By default, graphical output from R--for instance, from the R plot function--is displayed in the IBM® SPSS® Statistics Viewer. You can display a specified R graphics file, from disk, in the IBM SPSS Statistics Viewer using the spssRGraphics.Submit function, and you can turn display of R graphics on or off using the spssRGraphics.SetOutput function. You can also set the outline title for R graphics displayed in IBM SPSS Statistics with the spssRGraphics.SetGraphicsLabel function. R graphics displayed in the IBM SPSS Statistics Viewer cannot be edited and do not use the graphics preference settings in IBM SPSS Statistics.

Example

This example makes use of the default behavior for rendering graphical output from R in the Viewer. It produces a scatterplot, along with fit lines computed from a linear model and separately from a smoothing algorithm.

COMPUTE filter_$=(nvalid(mpg, curb_wgt) = 2).
FILTER BY filter_$.
BEGIN PROGRAM R.
data <- spssdata.GetDataFromSPSS()
spssRGraphics.SetGraphicsLabel("MyGraphicsLabel")
plot(data$curb_wgt, data$mpg, col="blue", pch=19)
abline(lm(data$mpg ~ data$curb_wgt), lwd=3 )
lines(lowess(data$mpg ~ data$curb_wgt), lwd=3, lty=2)
END PROGRAM.

Result

Figure 1. R graphic displayed in the Viewer
R graphic displayed in the Viewer

Note on Generating Multiple Graphics

When invoking a graphics command that generates multiple graphics, you will need to add the parameter ask=FALSE, as in: plot(result,ask=FALSE).