Learn R Programming

r2d2 (version 1.0.2)

freq2d: Bivariate (Two-Dimensional) Frequency Distribution

Description

Calculate a frequency distribution for two continuous variables.

Usage

freq2d(x, ...)

# S3 method for formula freq2d(formula, data, subset, ...)

# S3 method for default freq2d(x, y, n=20, pad=0, layout=1, print=TRUE, dnn=NULL, ...)

Value

The layout argument specifies one of the following formats for the binned frequency output:

  1. table that is easy to read, aligned like a scatterplot.

  2. list with three elements (x, y, table) that can be passed to various plotting functions.

  3. data.frame with three columns (x, y, frequency) that can be analyzed further.

Arguments

x

a vector of x values, or a data frame whose first two columns contain the x and y values.

y

a vector of y values.

formula

a formula, such as y~x.

data

a data.frame, matrix, or list from which the variables in formula should be taken.

subset

an optional vector specifying a subset of observations to be used.

n

the desired number of bins for the output, a scalar or a vector of length 2.

pad

number of rows and columns to add to each margin, containing only zeros.

layout

one of three layouts for the output: 1, 2, or 3.

print

whether to display the resulting table on the screen using dots for zeros.

dnn

the names to be given to the dimensions in the result.

...

named arguments to be passed to the default method.

Author

Arni Magnusson.

Details

The exact number of bins is determined by the pretty function, based on the value of n.

Padding the margins with zeros can be helpful for subsequent analysis, such as smoothing.

The print logical flag only has an effect when layout=1.

See Also

cut, table, and print.table are the basic underlying functions.

hist2d in the gplots package is a related function with graphical capabilities.

conf2d calculates a bivariate empirical confidence region, a smooth polygon.

r2d2-package gives an overview of the package.

Examples

Run this code
freq2d(Ushape)
freq2d(quakes$long, quakes$lat, dnn="")
freq2d(lat~long, quakes, n=c(10,20), pad=1)

# Supress display
freq2d(saithe)
range(freq2d(saithe, print=FALSE))

# Layout, plot
freq2d(saithe, layout=2)
freq2d(saithe, layout=3)
contour(freq2d(saithe, layout=2))
lattice::contourplot(Freq~Bio+HR, freq2d(saithe,layout=3))

Run the code above in your browser using DataLab