Skip to content

Commit

Permalink
Add methods for multcomp package objects.
Browse files Browse the repository at this point in the history
Fixes #144
  • Loading branch information
hadley committed Dec 20, 2011
1 parent b9e2c4a commit 9e50eb6
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 2 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Expand Up @@ -30,7 +30,8 @@ Suggests:
maps,
hexbin,
gpclib,
maptools
maptools,
multcomp
Extends:
sp
License: GPL-2
Expand Down Expand Up @@ -188,3 +189,4 @@ Collate:
'annotation-map.r'
'autoplot.r'
'zzz.r'
'fortify-multcomp.r'
4 changes: 4 additions & 0 deletions NAMESPACE
Expand Up @@ -252,8 +252,11 @@ S3method(facet_train_layout,null)
S3method(facet_train_layout,wrap)
S3method(format,facet)
S3method(fortify,"NULL")
S3method(fortify,cld)
S3method(fortify,confint.glht)
S3method(fortify,data.frame)
S3method(fortify,default)
S3method(fortify,glht)
S3method(fortify,Line)
S3method(fortify,Lines)
S3method(fortify,lm)
Expand All @@ -262,6 +265,7 @@ S3method(fortify,Polygon)
S3method(fortify,Polygons)
S3method(fortify,SpatialLinesDataFrame)
S3method(fortify,SpatialPolygons)
S3method(fortify,summary.glht)
S3method(ggplot,data.frame)
S3method(ggplot,default)
S3method(grid.draw,absoluteGrob)
Expand Down
4 changes: 3 additions & 1 deletion NEWS
@@ -1,7 +1,7 @@
ggplot2 0.9.0
----------------------------------------------------------------

NEW GEOMS/ANNOTATIONS
NEW FEATURES

* `geom_map`

Expand All @@ -11,6 +11,8 @@ NEW GEOMS/ANNOTATIONS

* `annotation_map`

* New fortify methods for objects produced by the `multcomp` package.

MINOR CHANGES

* `geom_text` now supports `fontfamily`, `fontface`, and `lineheight`
Expand Down
84 changes: 84 additions & 0 deletions R/fortify-multcomp.r
@@ -0,0 +1,84 @@
#' Fortify methods for objects produced by \pkg{multcomp}
#'
#' @param model an object of class \code{glht}, \code{confint.glht},
#' \code{summary.glht} or \code{\link{cld}}
#' @param data,... other arguments to the generic ignored in this method.
#' @name fortify-multcomp
#' @examples
#' if (require("multcomp")) {
#' amod <- aov(breaks ~ wool + tension, data = warpbreaks)
#' wht <- glht(amod, linfct = mcp(tension = "Tukey"))
#'
#' fortify(wht)
#' ggplot(wht, aes(lhs, estimate)) + geom_point()
#'
#' CI <- confint(wht)
#' fortify(CI)
#' ggplot(CI, aes(lhs, estimate, ymin = lwr, ymax = upr)) +
#' geom_pointrange()
#'
#' fortify(summary(wht))
#' ggplot(mapping = aes(lhs, estimate)) +
#' geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) +
#' geom_point(aes(size = p), data = summary(wht)) +
#' scale_size(trans = "reverse")
#'
#' cld <- cld(wht)
#' fortify(cld)
#' }
NULL

#' @method fortify glht
#' @rdname fortify-multcomp
#' @export
fortify.glht <- function(model, data, ...) {
unrowname(data.frame(
lhs = rownames(model$linfct),
rhs = model$rhs,
estimate = coef(model),
check.names = FALSE,
stringsAsFactors = FALSE))
}

#' @rdname fortify-multcomp
#' @method fortify confint.glht
#' @export
fortify.confint.glht <- function(model, data, ...) {
coef <- model$confint
colnames(coef) <- tolower(colnames(coef))

unrowname(data.frame(
lhs = rownames(coef),
rhs = model$rhs,
coef,
check.names = FALSE,
stringsAsFactors = FALSE))
}

#' @method fortify summary.glht
#' @rdname fortify-multcomp
#' @export
fortify.summary.glht <- function(model, data, ...) {
coef <- as.data.frame(
model$test[c("coefficients", "sigma", "tstat", "pvalues")])
names(coef) <- c("estimate", "se", "t", "p")

unrowname(data.frame(
lhs = rownames(coef),
rhs = model$rhs,
coef,
check.names = FALSE,
stringsAsFactors = FALSE))
}


#' @method fortify cld
#' @rdname fortify-multcomp
#' @export
fortify.cld <- function(model, data, ...) {
unrowname(data.frame(
lhs = names(model$mcletters$Letters),
letters = model$mcletters$Letters,
check.names = FALSE,
stringsAsFactors = FALSE))
}
51 changes: 51 additions & 0 deletions man/fortify-multcomp.Rd
@@ -0,0 +1,51 @@
\name{fortify-multcomp}
\alias{fortify-multcomp}
\alias{fortify.cld}
\alias{fortify.confint.glht}
\alias{fortify.glht}
\alias{fortify.summary.glht}
\title{Fortify methods for objects produced by \pkg{multcomp}}
\usage{
\method{fortify}{glht} (model, data, ...)

\method{fortify}{confint.glht} (model, data, ...)

\method{fortify}{summary.glht} (model, data, ...)

\method{fortify}{cld} (model, data, ...)
}
\arguments{
\item{model}{an object of class \code{glht},
\code{confint.glht}, \code{summary.glht} or
\code{\link{cld}}}

\item{data,...}{other arguments to the generic ignored in
this method.}
}
\description{
Fortify methods for objects produced by \pkg{multcomp}
}
\examples{
if (require("multcomp")) {
amod <- aov(breaks ~ wool + tension, data = warpbreaks)
wht <- glht(amod, linfct = mcp(tension = "Tukey"))

fortify(wht)
ggplot(wht, aes(lhs, estimate)) + geom_point()

CI <- confint(wht)
fortify(CI)
ggplot(CI, aes(lhs, estimate, ymin = lwr, ymax = upr)) +
geom_pointrange()

fortify(summary(wht))
ggplot(mapping = aes(lhs, estimate)) +
geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) +
geom_point(aes(size = p), data = summary(wht)) +
scale_size(trans = "reverse")

cld <- cld(wht)
fortify(cld)
}
}

0 comments on commit 9e50eb6

Please sign in to comment.