compareCats: Compare Data by Category

View source: R/compareCats.R

compareCatsR Documentation

Compare Data by Category

Description

This function allows one to generate a nice plot comparing a response from samples falling into one or two categories with corresponding levels. lattice is used to create plots, with options to include the original data and to use various measures of central tendency and spread. A count of the number of samples in each level is included. Gives a visual impression of the data to go along with hypothesis tests.

Usage

compareCats(
  formula = NULL,
  data = NULL,
  cols = NULL,
  freckles = FALSE,
  method = c("sem", "sem95", "iqr", "mad", "box", "points"),
  theme = screenTheme(),
  ...
)

Arguments

formula

A formula with both LHS and RHS. The formula should comply with the usual lattice conventions, for instance y ~ factor1 | factor2.

data

A data frame containing the response and categorical/factor data. A maximum two categories with any number of levels can be plotted at one time.

cols

A vector of colors to be used for factor 1; must be as along as the levels in factor 1.

freckles

Logical; if TRUE, the actual data points are plotted as small points, hence the name, jittered slightly in the horizontal direction only.

method

One of c("sem", "sem95", "iqr", "mad", "box", "points"). Various methods for computing measures of spread and central tendency. See the documentation for ChemoSpec::.seXy.

theme

Character; A suitable lattice theme. There are two built-in themes which you can use "as is" or modify to your heart's content. If none is given, screenTheme will be used. The other option provided is posterTheme.

...

Other parameters to be passed downstream.

Details

The supplied data frame is stripped down to just the response and factor columns, then NAs are removed; the counts reflect this.

Value

A lattice object. These can be modified by the usual methods associated with these packages.

Author(s)

Bryan A. Hanson, DePauw University. hanson@depauw.edu

References

https://github.com/bryanhanson/HandyStuff

Examples

#
### Set up test data
#
require("ChemoSpec")
require("lattice")
require("latticeExtra")
require("plyr")

mydf <- data.frame(
	resp = rnorm(40),
	cat1 = sample(LETTERS[1:3], 40, replace = TRUE),
	cat2 = sample(letters[1:2], 40, replace = TRUE),
 stringsAsFactors = TRUE)
#
### One factor:
#
p <- compareCats(formula = resp~cat1, data = mydf,
	method = "sem", freckles = TRUE, poster = FALSE, 
	cols = c("red", "orange", "blue"))
print(p)
#
### Two factors:
#	
p <- compareCats(formula = resp~cat1 | cat2, data = mydf,
	method = "sem", freckles = TRUE,
	cols = c("red", "orange", "blue"))
print(p)
#
### Interchange the roles of the factors
#
p <- compareCats(formula = resp~cat2 | cat1, data = mydf,
	method = "sem", freckles = TRUE,
	cols = c("red", "blue"))
print(p)



bryanhanson/HandyStuff documentation built on July 22, 2022, 6:18 a.m.