Description Usage Arguments Value Author(s) Examples
Depending on the grouping variable 'by', this function factory generates either an ungrouped (by=NULL) or a grouped scatterplot (by=<grouping_variable>). This function provides the option for different regression fits, based on an inbuilt smooth function or a custom model, defined by smooth.formula.
1 2 3 |
data |
A dataframe with at least three columns (x,y and a facetting variable) (Required) |
by |
Grouping variable (Required) |
fun |
Function for statistical aggregation function (Default: "mean") |
smooth.fun |
Smoothing method (function) to use, eg. "lm", "glm", "gam", "loess", "rlm". (Default: "auto") For method = "auto" the smoothing method is chosen based on the size of the largest group (across all panels). loess is used for less than 1,000 observations; otherwise gam is used with formula = y ~ s(x, bs = "cs"). Somewhat anecdotally, loess gives a better appearance, but is O(n^2) in memory, so does not work for larger datasets. |
smooth.formula |
Formula to use in smoothing function, eg. y ~ x, y ~ poly(x, 2), y ~ log(x) (Default: y ~ x) |
density |
Display density contour (Default: FALSE) |
title |
Plot Title (Default: NULL) |
xlab |
x-axis label (Default: NULL) |
ylab |
y-axis label (Default: NULL) |
legend.pos |
(Default: "none") |
ggplot2 figure
Jens Hooge
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | ## Not run:
x <- rnorm(500)
y <- rnorm(500)+x^2
df <- data.frame(x=x, y=y,
A=sample(c("a1", "a2"), 500, replace = TRUE),
B=sample(c("b1", "b2"), 500, replace = TRUE))
## A simple ungrouped scatter plot with a loess fit
general.scatter(df)
## A grouped scatter plot with a loess fit
general.scatter(df, by="A")
## An ungrouped scatter plot and a marker indicating the data variance
general.scatter(df, fun="var")
## Scatterplots with different regression fits
# Linear Fit
general.scatter(df, by="A", smooth.fun="lm")
## Robust Linear Fit
library(MASS)
general.scatter(df, by="A", smooth.fun="rlm")
## User defined polynomial fit
# Degree = 1 resulting in a linear fit
general.scatter(df, smooth.fun="glm", smooth.formula=y ~ poly(x, 1))
# Degree = 2 resulting in a quadratic fit
general.scatter(df, smooth.fun="glm", smooth.formula=y ~ poly(x, 2))
## Equivalently for grouped scatterplots
# Degree = 1 resulting in a linear fit
general.scatter(df, by="A", smooth.fun="glm", smooth.formula=y ~ poly(x, 1))
# Degree = 2 resulting in a quadratic fit
general.scatter(df, by="B", smooth.fun="glm", smooth.formula=y ~ poly(x, 2))
## Including a density contour
general.scatter(df, by="B", smooth.fun="glm", smooth.formula=y ~ poly(x, 2),
density=T, legend.pos="bottom")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.