R: Find or assign the implied position for graphing the levels...
position
R Documentation
Find or assign the implied position for graphing the levels of a
factor.
A new class "positioned", which inherits from "ordered" and "factor",
is defined.
Description
The default values for plotting a factor x are the integers
1:length(levels(x)). These functions provide a way of
specifying alternate plotting locations for the levels.
Usage
position(x)
position(x) <- value
## S3 method for class 'positioned'
is.numeric(x, ...)
## S3 method for class 'positioned'
as.numeric(x, ...)
## S3 method for class 'positioned'
x[..., drop=FALSE]
## S3 method for class 'positioned'
is.na(x)
as.positioned(x)
as.position(x)
is.positioned(x)
positioned(x, ..., value)
## S3 method for class 'positioned'
print(x, ...)
## S3 method for class 'positioned'
unique(x, incomparables = FALSE, ...)
unpositioned(x, ...)
Arguments
x
numeric vector or factor
value
numerical values to be
associated with levels(x).
The length(value) must equal length(levels(as.factor(x))).
...
other arguments.
drop
See
Extract.
incomparables
See
unique.
Value
position(x) <- value first forces its argument to be an ordered
factor and then assigns the value to the "position"
attribute of the ordered factor.
The result is assigned class "positioned" and returned.
position(x) returns the position values associated with
levels(x). If x is a positioned factor, then
the "position" attribute is returned.
If x is a factor, then the integers
1:length(levels(x)) are returned. For anything else,
as.numeric(x) is returned.
as.position(x) returns a numeric vector the length of the
original vector. If x inherits from "factor",
then the values in the vector are the values in
position(x) subscripted by the levels of the factor.
If x is numeric, then x itself is returned.
unpositioned(x) removes the "position" attribute and
removes the "positioned" value from the the oldClass of
the object.
Author(s)
Richard M. Heiberger <rmh@temple.edu>
See Also
panel.interaction2wt,
factor.
Examples
## ordered with character levels defaults to
## integer position of specified levels
tmp <- ordered(c("mm","cm","m","m","mm","cm"),
levels=c("mm","cm","m")) ## size order
tmp
as.numeric(tmp)
levels(tmp)
position(tmp)
as.position(tmp)
as.positioned(tmp)
positioned(tmp)
unpositioned(tmp)
unique(tmp)
## position is assigned to ordered in specified order
tmp <- ordered(c("cm","mm","m","m","mm","cm"),
levels=c("mm","cm","m")) ## size order
levels(tmp)
position(tmp) <- c(-3, -2, 0) ## log10 assigned in size order
tmp
as.numeric(tmp)
levels(tmp)
position(tmp)
as.position(tmp)
as.positioned(tmp)
positioned(tmp)
unpositioned(tmp)
unique(tmp)
## numeric stays numeric
tmp <- c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010)
tmp
as.numeric(tmp)
levels(tmp)
position(tmp)
as.position(tmp)
as.positioned(tmp)
positioned(tmp)
unpositioned(tmp)
unique(tmp)
## factor with numeric levels, position is integer position in size order
tmp <- factor(c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010))
tmp
as.numeric(tmp)
levels(tmp)
position(tmp)
as.position(tmp)
as.positioned(tmp)
positioned(tmp)
unpositioned(tmp)
unique(tmp)
## ordered with numeric levels, position is numeric value in size order
tmp <- ordered(c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010))
tmp
as.numeric(tmp)
levels(tmp)
position(tmp)
as.position(tmp)
as.positioned(tmp)
positioned(tmp)
unpositioned(tmp)
unique(tmp)
## factor with numeric levels
## position is assigned in size order
tmp <- factor(c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010))
levels(tmp)
position(tmp) <- c(-3, -2, 0) ## log10 assigned in size order
tmp
as.numeric(tmp)
levels(tmp)
position(tmp)
as.position(tmp)
as.positioned(tmp)
positioned(tmp)
unpositioned(tmp)
unique(tmp)
## boxplots coded by week
tmp <- data.frame(Y=rnorm(40, rep(c(20,25,15,22), 10), 5),
week=ordered(rep(1:4, 10)))
position(tmp$week) <- c(1, 2, 4, 8)
if.R(r=
bwplot(Y ~ week, horizontal=FALSE,
scales=list(x=list(limits=c(0,9),
at=position(tmp$week),
labels=position(tmp$week))),
data=tmp, panel=panel.bwplot.intermediate.hh)
,s=
t(bwplot(week ~ Y, at=position(tmp$week),
scales=list(y=list(limits=c(0,9),
at=position(tmp$week), labels=position(tmp$week))),
data=tmp, panel=panel.bwplot.intermediate.hh))
)
#### You probably don't want to use the next two examples.
#### You need to be aware of their behavior.
##
## factor with character levels defaults to
## integer position of sorted levels.
## you probably DON'T want to do this!
tmp <- factor(c("cm","mm","m","m","mm","cm")) ## default alphabetic order
tmp
as.numeric(tmp)
levels(tmp) ## you probably DON'T want to do this!
position(tmp) ## you probably DON'T want to do this!
as.numeric(tmp)
##
## position is assigned to factor in default alphabetic order.
## you probably DON'T want to do this!
tmp <- factor(c("cm","mm","m","m","mm","cm"))
levels(tmp)
position(tmp) <- c(-3, -2, 0) ## assigned in default alphabetic order
tmp
as.numeric(tmp)
levels(tmp) ## you probably DON'T want to do this!
position(tmp) ## you probably DON'T want to do this!
as.numeric(tmp)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(HH)
Loading required package: lattice
Loading required package: grid
Loading required package: latticeExtra
Loading required package: RColorBrewer
Loading required package: multcomp
Loading required package: mvtnorm
Loading required package: survival
Loading required package: TH.data
Loading required package: MASS
Attaching package: 'TH.data'
The following object is masked from 'package:MASS':
geyser
Loading required package: gridExtra
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/HH/position.Rd_%03d_medium.png", width=480, height=480)
> ### Name: position
> ### Title: Find or assign the implied position for graphing the levels of a
> ### factor. A new class "positioned", which inherits from "ordered" and
> ### "factor", is defined.
> ### Aliases: position position<- is.numeric.positioned
> ### as.numeric.positioned as.position [.positioned as.positioned
> ### is.positioned is.na.positioned positioned print.positioned
> ### unique.positioned unpositioned
> ### Keywords: dplot
>
> ### ** Examples
>
> ## ordered with character levels defaults to
> ## integer position of specified levels
> tmp <- ordered(c("mm","cm","m","m","mm","cm"),
+ levels=c("mm","cm","m")) ## size order
> tmp
[1] mm cm m m mm cm
Levels: mm < cm < m
> as.numeric(tmp)
[1] 1 2 3 3 1 2
> levels(tmp)
[1] "mm" "cm" "m"
> position(tmp)
[1] 1 2 3
> as.position(tmp)
[1] 1 2 3 3 1 2
> as.positioned(tmp)
[1] mm cm m m mm cm
Levels: mm < cm < m
position: 1 < 2 < 3
> positioned(tmp)
[1] mm cm m m mm cm
Levels: mm < cm < m
position: 1 < 2 < 3
> unpositioned(tmp)
[1] mm cm m m mm cm
Levels: mm < cm < m
> unique(tmp)
[1] mm cm m
Levels: mm < cm < m
>
> ## position is assigned to ordered in specified order
> tmp <- ordered(c("cm","mm","m","m","mm","cm"),
+ levels=c("mm","cm","m")) ## size order
> levels(tmp)
[1] "mm" "cm" "m"
> position(tmp) <- c(-3, -2, 0) ## log10 assigned in size order
> tmp
[1] cm mm m m mm cm
Levels: mm < cm < m
position: -3 < -2 < 0
> as.numeric(tmp)
[1] 2 1 3 3 1 2
> levels(tmp)
[1] "mm" "cm" "m"
> position(tmp)
[1] -3 -2 0
> as.position(tmp)
[1] -2 -3 0 0 -3 -2
> as.positioned(tmp)
[1] cm mm m m mm cm
Levels: mm < cm < m
position: -3 < -2 < 0
> positioned(tmp)
[1] cm mm m m mm cm
Levels: mm < cm < m
position: 1 < 2 < 3
> unpositioned(tmp)
[1] cm mm m m mm cm
Levels: mm < cm < m
> unique(tmp)
[1] cm mm m
Levels: mm < cm < m
position: -3 < -2 < 0
>
> ## numeric stays numeric
> tmp <- c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010)
> tmp
[1] 0.010 0.001 1.000 1.000 0.001 0.010
> as.numeric(tmp)
[1] 0.010 0.001 1.000 1.000 0.001 0.010
> levels(tmp)
NULL
> position(tmp)
[1] 0.010 0.001 1.000 1.000 0.001 0.010
> as.position(tmp)
[1] 0.010 0.001 1.000 1.000 0.001 0.010
> as.positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: 1 < 2 < 3
> positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: 1 < 2 < 3
> unpositioned(tmp)
[1] 0.010 0.001 1.000 1.000 0.001 0.010
> unique(tmp)
[1] 0.010 0.001 1.000
>
> ## factor with numeric levels, position is integer position in size order
> tmp <- factor(c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010))
> tmp
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 0.01 1
> as.numeric(tmp)
[1] 2 1 3 3 1 2
> levels(tmp)
[1] "0.001" "0.01" "1"
> position(tmp)
[1] 1 2 3
> as.position(tmp)
[1] 2 1 3 3 1 2
> as.positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: 1 < 2 < 3
> positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: 1 < 2 < 3
> unpositioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 0.01 1
> unique(tmp)
[1] 0.01 0.001 1
Levels: 0.001 0.01 1
>
> ## ordered with numeric levels, position is numeric value in size order
> tmp <- ordered(c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010))
> tmp
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
> as.numeric(tmp)
[1] 2 1 3 3 1 2
> levels(tmp)
[1] "0.001" "0.01" "1"
> position(tmp)
[1] 1 2 3
> as.position(tmp)
[1] 2 1 3 3 1 2
> as.positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: 1 < 2 < 3
> positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: 1 < 2 < 3
> unpositioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
> unique(tmp)
[1] 0.01 0.001 1
Levels: 0.001 < 0.01 < 1
>
>
> ## factor with numeric levels
> ## position is assigned in size order
> tmp <- factor(c(0.010, 0.001, 1.000, 1.000, 0.001, 0.010))
> levels(tmp)
[1] "0.001" "0.01" "1"
> position(tmp) <- c(-3, -2, 0) ## log10 assigned in size order
> tmp
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: -3 < -2 < 0
> as.numeric(tmp)
[1] 2 1 3 3 1 2
> levels(tmp)
[1] "0.001" "0.01" "1"
> position(tmp)
[1] -3 -2 0
> as.position(tmp)
[1] -2 -3 0 0 -3 -2
> as.positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: -3 < -2 < 0
> positioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
position: 1 < 2 < 3
> unpositioned(tmp)
[1] 0.01 0.001 1 1 0.001 0.01
Levels: 0.001 < 0.01 < 1
> unique(tmp)
[1] 0.01 0.001 1
Levels: 0.001 < 0.01 < 1
position: -3 < -2 < 0
>
>
>
> ## boxplots coded by week
> tmp <- data.frame(Y=rnorm(40, rep(c(20,25,15,22), 10), 5),
+ week=ordered(rep(1:4, 10)))
> position(tmp$week) <- c(1, 2, 4, 8)
>
> if.R(r=
+ bwplot(Y ~ week, horizontal=FALSE,
+ scales=list(x=list(limits=c(0,9),
+ at=position(tmp$week),
+ labels=position(tmp$week))),
+ data=tmp, panel=panel.bwplot.intermediate.hh)
+ ,s=
+ t(bwplot(week ~ Y, at=position(tmp$week),
+ scales=list(y=list(limits=c(0,9),
+ at=position(tmp$week), labels=position(tmp$week))),
+ data=tmp, panel=panel.bwplot.intermediate.hh))
+ )
>
>
> #### You probably don't want to use the next two examples.
> #### You need to be aware of their behavior.
> ##
> ## factor with character levels defaults to
> ## integer position of sorted levels.
> ## you probably DON'T want to do this!
> tmp <- factor(c("cm","mm","m","m","mm","cm")) ## default alphabetic order
> tmp
[1] cm mm m m mm cm
Levels: cm m mm
> as.numeric(tmp)
[1] 1 3 2 2 3 1
> levels(tmp) ## you probably DON'T want to do this!
[1] "cm" "m" "mm"
> position(tmp) ## you probably DON'T want to do this!
[1] 1 2 3
> as.numeric(tmp)
[1] 1 3 2 2 3 1
> ##
> ## position is assigned to factor in default alphabetic order.
> ## you probably DON'T want to do this!
> tmp <- factor(c("cm","mm","m","m","mm","cm"))
> levels(tmp)
[1] "cm" "m" "mm"
> position(tmp) <- c(-3, -2, 0) ## assigned in default alphabetic order
> tmp
[1] cm mm m m mm cm
Levels: cm < m < mm
position: -3 < -2 < 0
> as.numeric(tmp)
[1] 1 3 2 2 3 1
> levels(tmp) ## you probably DON'T want to do this!
[1] "cm" "m" "mm"
> position(tmp) ## you probably DON'T want to do this!
[1] -3 -2 0
> as.numeric(tmp)
[1] 1 3 2 2 3 1
>
>
>
>
>
>
> dev.off()
null device
1
>