loess.scatter | R Documentation |
Drawing a fit line usually requires some manual steps requiring several lines of code, such as ensuring the data is sorted by x, and for some functions doesn't contain missing values. This function takes care of these steps and automatically adds a loess fitline, or non-linear fitline. The type of scatter defaults to 'plot', but other scatter plot functions can be specified, such as graphics::smoothScatter(), for example. If 'file' is specifed, will automatically plot to a pdf of that name.
loess.scatter( x, y, file = NULL, loess = TRUE, span = 0.75, scatter = plot, ..., ylim = NULL, return.vectors = FALSE, fit.col = "red", fit.lwd = 2, fit.lty = "solid", fit.leg = TRUE, fit.r2 = TRUE, fast.loess = FALSE )
x |
data for the horizontal axis (independent variable) |
y |
data for the vertical axis (dependent variable) |
file |
file name for pdf export, leave as NULL if simply plotting to the GUI. File extension will be added automatically if missing |
loess |
logical, if TRUE, fit using loess(), else use a polynomial fit |
span |
numeric scalar, argument passed to the 'span' parameter of loess(), see ?loess for details |
scatter |
function, by default is graphics::plot(), but any scatter-plot function of the form F(x,y,...) can be used, for example graphics::smoothScatter(). |
... |
further arguments to the plot function specified by 'scatter', e.g, 'main', 'xlab', etc |
ylim |
numeric range for y axis, argument passed to plot(), see ?plot. |
return.vectors |
logical, if TRUE, do not plot anything, just return the x and y coordinates of the fit line as a list of vectors, x and y. |
fit.col |
colour of the fit line |
fit.lwd |
width of the fit line |
fit.lty |
type of the fit line |
fit.leg |
whether to include an automatic legend for the fit line (will alter the y-limits to fit) |
fit.r2 |
logical, whether to display r squared of the fit in the fit legend |
fast.loess |
logical, if TRUE will alter control parameters to make the loess calculation faster, which is useful for datasets with more than 1000 points. Also reduce the value of 'span' to increase speed. |
if file is a character argument, plots data x,y to a file, else will generate a plot to the current plotting environment/GUI. The display of the x,y points defaults to 'plot', but alternate scatter plot functions can be specified, such as graphics::smoothScatter() which used density smoothing, for example. Also, another option is to set return.vectors=TRUE, and then the coordinates of the fit line will be returned, and no plot will be produced.
library(NCmisc) require(KernSmooth) DD <- sim.cor(1000,4) # create a simulated, correlated dataset loess.scatter(DD[,3],DD[,4],loess=FALSE,bty="n",pch=".",cex=2) loess.scatter(DD[,3],DD[,4],scatter=smoothScatter) xy <- loess.scatter(DD[,3],DD[,4],return.vectors=TRUE) prv(xy) # preview the vectors produced
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.