[R] Keep value lables with data frame manipulation

Richard M. Heiberger rmh at temple.edu
Thu Jul 13 18:36:37 CEST 2006


> Further I do not see a simple method to label numerical
> variables. I often encounter discrete, but still metric data, as e.g. risk
> scores. Usually it would be nice to use them in their original coding,
> which may include zero or decimal places and to label them at the same time.

## For this specific case, I use a "position" attribute.


tmp <- data.frame(y=rnorm(30), x=factor(rep(c(0,1,2,4,8), 6)))
attr(tmp$x, "position") <- as.numeric(as.character(tmp$x))

tmp
as.numeric(tmp$x)
attr(tmp$x, "position")

bwplot(y ~ x, data=tmp)

panel.bwplot.position <- function(x, y, ..., x.at) {
         for (x.i in x.at) {
           y.i <- y[x.i==x]
           panel.bwplot(rep(x.i, length(y.i)), y.i, ...)
         }
       }

bwplot.position <- function(formula, data, ..., x.at) {
  if (missing(x.at)) {
    x.name <- dimnames(attr(terms(formula),"factors"))[[2]]
    x.at <- attr(data[[x.name]], "position")
  }
  bwplot(formula, data, ...,
         x.at=x.at,
         panel=panel.bwplot.position,
         scales=list(x=list(at=x.at, limits=x.at+c(-1,1))))
}

bwplot.position(y ~ x, data=tmp)


## The above is a simplified version of
##     panel.bwplot.intermediate.hh
## in the online files for
##                 Statistical Analysis and Data Display
##                 Richard M. Heiberger and Burt Holland
##    http://springeronline.com/0-387-40270-5
## 
## An example of a boxplot with both placement and color of the boxes
## under user control is in
## 
## http://astro.ocis.temple.edu/~rmh/HH/bwplot-color.pdf



More information about the R-help mailing list