Skip to content

Commit

Permalink
Merge branch 'issue-#656'
Browse files Browse the repository at this point in the history
  • Loading branch information
jarioksa committed Jun 4, 2024
2 parents 28a0320 + 2ab9b8f commit 4a1e8ac
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
8 changes: 5 additions & 3 deletions R/ordiArrowTextXY.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
### label these heads, '...' passes arguments (such as 'cex') to
### strwidth() and strheight().
`ordiArrowTextXY` <- function (x, labels, display, choices = c(1,2),
rescale = TRUE, fill = 0.75, at = c(0,0), ...) {
rescale = TRUE, fill = 0.75, at = c(0,0),
cex = NULL, ...)
{
## handle x, which we try with scores, but also retain past usage of
## a two column matrix
X <- if (is.matrix(x)) {
Expand Down Expand Up @@ -38,8 +40,8 @@
}
}

w <- strwidth(labels, ...)
h <- strheight(labels, ...)
w <- strwidth(labels, cex = cex, ...)
h <- strheight(labels, cex = cex, ...)

## slope of arrows
b <- (X[,2] - at[2]) / (X[,1] - at[1])
Expand Down
32 changes: 17 additions & 15 deletions R/plot.cca.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
`plot.cca` <- function (x, choices = c(1, 2), display = c("sp", "wa", "cn"),
scaling = "species", type, xlim, ylim, const,
correlation = FALSE, hill = FALSE, ...) {
correlation = FALSE, hill = FALSE, cex = 0.7, ...) {
TYPES <- c("text", "points", "none")
## take care that bp arrows are also returned if only cn given
if (any(display %in% c("c","cn")))
Expand Down Expand Up @@ -77,22 +77,22 @@
if (!is.null(g$species)) {
if (type == "text")
text(g$species, rownames(g$species), col = "red",
cex = 0.7)
cex = cex)
else if (type == "points")
points(g$species, pch = "+", col = "red", cex = 0.7)
points(g$species, pch = "+", col = "red", cex = cex)
}
if (!is.null(g$sites)) {
if (type == "text")
text(g$sites, rownames(g$sites), cex = 0.7)
text(g$sites, rownames(g$sites), cex = cex)
else if (type == "points")
points(g$sites, pch = 1, cex = 0.7)
points(g$sites, pch = 1, cex = cex)
}
if (!is.null(g$constraints)) {
if (type == "text")
text(g$constraints, rownames(g$constraints), cex = 0.7,
text(g$constraints, rownames(g$constraints), cex = cex,
col = "darkgreen")
else if (type == "points")
points(g$constraints, pch = 2, cex = 0.7, col = "darkgreen")
points(g$constraints, pch = 2, cex = cex, col = "darkgreen")
}
if (!is.null(g$biplot) && nrow(g$biplot) > 0 && type != "none") {
if (length(display) > 1) {
Expand All @@ -102,8 +102,9 @@
attr(g$biplot, "arrow.mul") <- mul
arrows(0, 0, mul * g$biplot[, 1], mul * g$biplot[, 2],
length = 0.05, col = "blue")
biplabs <- ordiArrowTextXY(mul * g$biplot, rownames(g$biplot))
text(biplabs, rownames(g$biplot), col = "blue")
biplabs <- ordiArrowTextXY(mul * g$biplot, rownames(g$biplot),
cex = cex)
text(biplabs, rownames(g$biplot), col = "blue", cex = cex)
}
if (!is.null(g$regression) && nrow(g$regression > 0) && type != "none") {
rcol <- "purple4"
Expand All @@ -114,21 +115,22 @@
attr(g$regression, "arrow.mul") <- mul
arrows(0, 0, mul * g$regression[, 1], mul * g$regression[, 2],
length = 0.05, col = rcol)
biplabs <- ordiArrowTextXY(mul * g$regression, rownames(g$regression))
text(biplabs, rownames(g$regression), col = rcol)
biplabs <- ordiArrowTextXY(mul * g$regression, rownames(g$regression),
cex = cex)
text(biplabs, rownames(g$regression), col = rcol, cex = cex)
}
if (!is.null(g$centroids) && !anyNA(g$centroids) && type !=
"none") {
if (type == "text")
text(g$centroids, rownames(g$centroids), col = "blue")
text(g$centroids, rownames(g$centroids), col = "blue", cex = cex)
else if (type == "points")
points(g$centroids, pch = "x", col = "blue")
points(g$centroids, pch = "x", col = "blue", cex = cex)
}
if (!is.null(g$default) && type != "none") {
if (type == "text")
text(g$default, rownames(g$default), cex = 0.7)
text(g$default, rownames(g$default), cex = cex)
else if (type == "points")
points(g$default, pch = 1, cex = 0.7)
points(g$default, pch = 1, cex = cex)
}
class(g) <- "ordiplot"
invisible(g)
Expand Down
2 changes: 1 addition & 1 deletion R/text.ordiplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
if (arrows) {
arrows(0, 0, sco[,1], sco[,2], length = length, ...)
sco <- ordiArrowTextXY(sco, rownames(sco), rescale = FALSE)
sco <- ordiArrowTextXY(sco, rownames(sco), rescale = FALSE, ...)
}
text(sco, labels = rownames(sco), ...)
invisible(x)
Expand Down
7 changes: 4 additions & 3 deletions man/ordiArrowTextXY.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
}
\usage{
ordiArrowTextXY(x, labels, display, choices = c(1,2),
rescale = TRUE, fill = 0.75, at = c(0,0), \ldots)
ordiArrowMul(x, at = c(0,0), fill = 0.75,
display, choices = c(1,2), \ldots)
rescale = TRUE, fill = 0.75, at = c(0,0), cex = NULL, \ldots)
ordiArrowMul(x, at = c(0,0), fill = 0.75, display, choices = c(1,2), \ldots)
}

\arguments{
Expand Down Expand Up @@ -55,6 +54,8 @@ ordiArrowMul(x, at = c(0,0), fill = 0.75,
in other places than origin, you probably have to specify
\code{arrrow.mul}.}

\item{cex}{Character expansion for text.}

\item{...}{Parameters passed to \code{\link{scores}}, and
\code{\link{strwidth}} and \code{\link{strheight}}.}
}
Expand Down
36 changes: 22 additions & 14 deletions man/plot.cca.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
\usage{
\method{plot}{cca}(x, choices = c(1, 2), display = c("sp", "wa", "cn"),
scaling = "species", type, xlim, ylim, const,
correlation = FALSE, hill = FALSE, ...)
correlation = FALSE, hill = FALSE, cex = 0.7, ...)
\method{text}{cca}(x, display = "sites", labels, choices = c(1, 2),
scaling = "species", arrow.mul, head.arrow = 0.05, select, const,
axis.bp = FALSE, correlation = FALSE, hill = FALSE, ...)
Expand Down Expand Up @@ -112,6 +112,7 @@
kind of scores were requested.}
\item{axis.bp}{Draw \code{\link{axis}} for biplot arrows.}
\item{digits}{Number of digits in output.}
\item{cex}{Character expansion.}
\item{...}{Parameters passed to other functions.}
}

Expand Down Expand Up @@ -157,19 +158,21 @@
standard and more easily interpreted, and regression arrows should be
used only if you know that you need them.

If you want to have a better control of plots, it is best to
construct the plot \code{text} and \code{points} commands which
accept graphical parameters. It is important to remember to use the
same \code{scaling}, \code{correlation} and \code{hill} arguments
in all calls. The \code{plot.cca} command returns invisibly an
If you want to have a better control of plots, it is best to construct
the plot \code{text} and \code{points} commands which accept graphical
parameters. It is important to remember to use the same
\code{scaling}, \code{correlation} and \code{hill} arguments in all
calls. The \code{plot.cca} command returns invisibly an
\code{\link{ordiplot}} result object, and this will have consistent
scaling for all its elements. The easiest way for full control of
graphics is to first set up the plot frame using \code{plot} with
\code{type = "n"} and all needed scores in \code{display} and save
this result. The \code{points} and \code{text} commands for
\code{\link{ordiplot}} will allow full graphical control (see
section Examples). Utility function \code{labels} returns the default
labels in the order they are applied in \code{text}.
scaling for all its elements. It is also possible to use \R{} pipes
(\code{|>}) which maintain consistent scaling and setting. The easiest
way for full control of graphics is to first set up the plot frame
using \code{plot} with \code{type = "n"} and all needed scores in
\code{display} and save this result or use it as a first command in a
pipe. The \code{points} and \code{text} commands for
\code{\link{ordiplot}} will allow full graphical control (see section
Examples). Utility function \code{labels} returns the default labels
in the order they are applied in \code{text}.

Palmer (1993) suggested using linear constraints (\dQuote{LC scores})
in ordination diagrams, because these gave better results in
Expand Down Expand Up @@ -208,7 +211,12 @@ pca <- rda(dune)
pl <- plot(pca, type="n", scaling="sites", correlation=TRUE)
with(dune.env, points(pl, "site", pch=21, col=1, bg=Management))
text(pl, "sp", arrow=TRUE, length=0.05, col=4, cex=0.6, xpd=TRUE)
with(dune.env, legend("bottomleft", levels(Management), pch=21, pt.bg=1:4, bty="n"))
with(dune.env, legend("bottomleft", levels(Management), pch=21,
pt.bg=1:4, bty="n"))
## Configuration with pipes: make an arrow biplot
plot(pca, type="n", scaling="sites", correlation=TRUE) |>
points("sites", pch=21, col = 1, cex=1.5, bg = dune.env$Management) |>
text("species", col = "blue", arrow = TRUE, xpd = TRUE)
## Scaling can be numeric or more user-friendly names
## e.g. Hill's scaling for (C)CA
scrs <- scores(mod, scaling = "sites", hill = TRUE)
Expand Down
9 changes: 7 additions & 2 deletions tests/Examples/vegan-Ex.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -6111,7 +6111,12 @@ ungrazed-grazed -0.1219422 -0.2396552 -0.004229243 0.0429502
> pl <- plot(pca, type="n", scaling="sites", correlation=TRUE)
> with(dune.env, points(pl, "site", pch=21, col=1, bg=Management))
> text(pl, "sp", arrow=TRUE, length=0.05, col=4, cex=0.6, xpd=TRUE)
> with(dune.env, legend("bottomleft", levels(Management), pch=21, pt.bg=1:4, bty="n"))
> with(dune.env, legend("bottomleft", levels(Management), pch=21,
+ pt.bg=1:4, bty="n"))
> ## Configuration with pipes: make an arrow biplot
> plot(pca, type="n", scaling="sites", correlation=TRUE) |>
+ points("sites", pch=21, col = 1, cex=1.5, bg = dune.env$Management) |>
+ text("species", col = "blue", arrow = TRUE, xpd = TRUE)
> ## Scaling can be numeric or more user-friendly names
> ## e.g. Hill's scaling for (C)CA
> scrs <- scores(mod, scaling = "sites", hill = TRUE)
Expand Down Expand Up @@ -8948,7 +8953,7 @@ Procrustes sum of squares:
> cleanEx()
> options(digits = 7L)
> base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
Time elapsed: 8.559 0.295 8.885 0 0
Time elapsed: 8.568 0.288 8.883 0 0
> grDevices::dev.off()
null device
1
Expand Down

0 comments on commit 4a1e8ac

Please sign in to comment.