Browse files

Add methods for multcomp package objects.

Fixes #144
  • Loading branch information...
1 parent b9e2c4a commit 9e50eb69baa791498bf8c87914edac4317ae8787 @hadley committed Dec 19, 2011
Showing with 145 additions and 2 deletions.
  1. +3 −1 DESCRIPTION
  2. +4 −0 NAMESPACE
  3. +3 −1 NEWS
  4. +84 −0 R/fortify-multcomp.r
  5. +51 −0 man/fortify-multcomp.Rd
View
4 DESCRIPTION
@@ -30,7 +30,8 @@ Suggests:
maps,
hexbin,
gpclib,
- maptools
+ maptools,
+ multcomp
Extends:
sp
License: GPL-2
@@ -188,3 +189,4 @@ Collate:
'annotation-map.r'
'autoplot.r'
'zzz.r'
+ 'fortify-multcomp.r'
View
4 NAMESPACE
@@ -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)
@@ -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)
View
4 NEWS
@@ -1,7 +1,7 @@
ggplot2 0.9.0
----------------------------------------------------------------
-NEW GEOMS/ANNOTATIONS
+NEW FEATURES
* `geom_map`
@@ -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`
View
84 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))
+}
View
51 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.