general.scatter: Function factory to generate a scatter plots

Description Usage Arguments Value Author(s) Examples

Description

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.

Usage

1
2
3
general.scatter(data, by = NULL, fun = "mean", smooth.fun = "auto",
  smooth.formula = y ~ x, density = FALSE, title = NULL, xlab = NULL,
  ylab = NULL, legend.pos = "none")

Arguments

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")

Value

ggplot2 figure

Author(s)

Jens Hooge

Examples

 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)

jhooge/BioViz documentation built on May 19, 2019, 9:28 a.m.