These functions define basic transformations of points in Euclidean space. This includes principal components (PCs) as a special case. Transformations include rotations, reflections, scaling and translation. For plotting PCs, these functions will typically be used to apply transformations along two dimensions (i.e., the Cartesian plane), but more general transformations on n-dimensional space are also possible. In all cases, points are stored as row vectors in a matrix, or in a data frame within a pcaviz object.

pcaviz_rotate(x, angle, dims, units = c("degrees","radians"))

pcaviz_reflect(x, dims)

pcaviz_scale(x, scale, dims)

pcaviz_translate(x, a, dims)

pcaviz_transform2d(x, dims, angle = 0,reflect.x = FALSE,
                   reflect.y = FALSE,scale = c(1,1),a = c(0,0),
                   units = c("degrees","radians"))

matrix_rotate(X, angle, dims = 1:2, units = c("degrees","radians"))

matrix_reflect(X, dims)

matrix_scale(X, scale, dims)

matrix_translate(X, a, dims)

matrix_transform2d(X, dims = 1:2, angle = 0,reflect.x = FALSE,
                   reflect.y = FALSE,scale = c(1,1),a = c(0,0),
                   units = c("degrees","radians"))

Arguments

x

An object of class "pcaviz".

X

A matrix in which the points are stored as rows.

dims

The transformation is applied to these dimensions (i.e., column indices of X.) The dimensions must be specified as character for the pcaviz_ functions, and numeric or character for the matrix_ functions. For pcaviz_rotate, pcaviz_transform2d, matrix_rotate and matrix_transform2d, this must be a vector of length 2. By default, pcaviz_rotate and pcaviz_transform2d apply the transformation to the first two PCs. See Extract for more details on selecting matrix columns with numeric and character indices.

angle

A scalar specifying the angle of rotation. Positive angles specify counter-clockwise rotations.

units

The angle of rotation is specified in these units.

scale

A numeric vector specifying the scaling factor for each selected dimension; must be a vector of the same length as dims.

a

A numeric vector specifying the translation of the selected dimensions; must be a vector of the same length as dims.

reflect.x

If TRUE, reflect first dimension, dims[1].

reflect.y

If TRUE, reflect second dimension, dims[2].

Value

A pcaviz object with the transformed PCs, or a matrix with the same dimensions as X giving the transformed points.

Details

matrix_rotate applies a counter-clockwise plane rotation to row vectors in X when angle is positive. pcaviz_rotate applies a counter-clockwise plane rotation to selected PCs in x.

matrix_reflect reflects points about the origin, individually along each selected dimension. pcaviz_reflect reflects selected PCs.

matrix_scale scales the selected dimensions by scale, and pcaviz_scale scales the selected PCs by scale. Note that it is also possible to use a negative scaling factor, which is equivalent to a positive scaling combined with a reflection about the origin.

matrix_translate translates row vectors in X by a within the selected dimensions. pcaviz_translate translates selected PCs by a.

matrix_transform2d and pcaviz_transform2d are convenience functions that that apply a sequence of 2-d transformations in the following order: (1) counter-clockwise rotation, (2) reflection, (3) scaling and (4) translation. Note that exactly two dimensions (PCs) must be selected.

Examples

# A matrix storing 4 points in 2-d Euclidean space (i.e., the # Cartesian plane). X <- rbind(c(0,0), c(1,0), c(0,1), c(1,1)) # Rotate the points 45 degrees counter-clockwise. matrix_rotate(X,45)
#> [,1] [,2] #> [1,] 0.000000e+00 0.0000000 #> [2,] 7.071068e-01 0.7071068 #> [3,] -7.071068e-01 0.7071068 #> [4,] 1.110223e-16 1.4142136
# Reflect the points along the first dimension; i.e., X axis # in the Cartesian plane about the X=0 line. matrix_reflect(X,1)
#> [,1] [,2] #> [1,] 0 0 #> [2,] -1 0 #> [3,] 0 1 #> [4,] -1 1
# Translate all points by (-1,2). matrix_translate(X,a = c(-1,2),1:2)
#> [,1] [,2] #> [1,] -1 2 #> [2,] 0 2 #> [3,] -1 3 #> [4,] 0 3
# Apply multiple transformations to the points. matrix_transform2d(X,angle = -90,reflect.x = TRUE,reflect.y = TRUE, scale = c(2,5),a = c(-1,2))
#> [,1] [,2] #> [1,] -1 2 #> [2,] -1 7 #> [3,] -3 2 #> [4,] -3 7
# Create pcaviz object from prcomp output. data(iris) a <- pcaviz(prcomp(iris[1:4]),dat = iris) # Rescale the first two PCs. a <- pcaviz_scale(a,scale = c(0.5,1.25),dims = c("PC1","PC2")) # Rotate the first two PCs 15 degrees counter-clockwise. a <- pcaviz_rotate(a,angle = 15,dims = c("PC1","PC2"))