A function to draw the principal axes of a 2D ellipse from a correlation, covariance or sums of squares and cross products matrix in an existing plot.
Arguments
- x
A square positive definite matrix at least 2x2 in size. It will be treated as the correlation or covariance of a multivariate normal distribution.
- centre, center
The center of the ellipse
- scale
If x is a correlation matrix, then the standard deviations of each parameter can be given in the scale parameter. This defaults to
c(1, 1)
, so no rescaling will be done.- which
An integer vector to select which variables from the object
x
will be plotted. The default is the first 2.- level
The coverage level of a simultaneous region of the ellipse. The default is 0.95, for a 95% region. This is used to control the size of the ellipse.
- radius
The size of the ellipsoid may also be controlled by specifying the value of a t-statistic on its boundary. This defaults to the square root of a chi-square statistic for a given
level
on 2 degrees of freedom, however in a small sample ofn
observations, a more accurate value issqrt(2 * qf(level, 2, n - 1 ))
.- labels
Either a logical value, a character string, or a character vector of length 2. If
TRUE
, the default, the axes are labeled "PC1", "PC2". If a single character string, the digits 1, and 2 are pasted on the end.- label.ends
A vector of indices in the range
1:4
indicating which ends of the axes should be labeled, corresponding to a selection of rows of the 4 x 2 matrix of axes end points. Values1:2
represent the minimum and maximum of the first dimension respectively. Values3:4
represent the minimum and maximum of the second dimension. Default:c(2, 4)
.- label.pos
Positions of text labels relative to the ends of the axes used in
text
for the four possiblelabel.ends
. 1, 2, 3, 4 represent below, to the left, above and to the right. The default,c(2, 4, 1, 3)
, positions the labels outside the axes.- ...
Value
Invisibly returns a 4 x 2 matrix containing the end points of the axes in pairs (min, max) by rows.
Examples
data(iris)
cov <- cov(iris[,1:2])
mu <- colMeans(iris[,1:2])
radius <- sqrt(qchisq(0.68, 2))
plot(iris[,1:2], asp=1)
car::ellipse(mu, cov, radius = radius)
res <- ellipse.axes(cov, center=mu, level = 0.68,
labels = TRUE)
res
#> Sepal.Length Sepal.Width
#> Xmin -1.24882344 0.1061294
#> Xmax 1.24882344 -0.1061294
#> Ymin -0.05518532 -0.6493648
#> Ymax 0.05518532 0.6493648