Sniffler
Sniffler

Reputation: 3

Select argument doesn't work on cca objects

I created an object of class cca in vegan and now I am trying to tidy up the triplot. However, I seemingly can't use the select argument to only show specified items. My code looks like this:

data("varechem")
data("varespec")
ord <- cca(varespec ~ Al+S, varechem)
plot(ord, type = "n")
text(ord, display = "sites", select = c("18", "21"))

I want only the two specified sites (18 and 21) to appear in the plot, but when I run the code nothing happens. I do not even get an error meassage.

I'm really stuck, but I am fairly certain that this bit of code is correct. Can someone help me?

Upvotes: 0

Views: 118

Answers (2)

Gavin Simpson
Gavin Simpson

Reputation: 174938

I can't recall now, but I don't think the intention was to allow "names" to select which rows of the scores should be selected. The documentation speaks of select being a logical vector, or indices of the scores to be selected. By indices it was meant numeric indices, not rownames.

The example fails because select is also used to subset the labels character vector of values to be plotted in text(), and this labels character vector is not named. Using a character vector to subset another vector requires that the other vector be named.

Your example works if you do:

data("varechem")
data("varespec")
ord <- cca(varespec ~ Al + S, varechem)
plot(ord, type = "n")

take <- which(rownames(varechem) %in% c("18", "21"))
# or
# take <- rownames(varechem) %in% c("18", "21")
text(ord, display = "sites", select = take)

I'll have a think about whether it will be simple to support the use case of your example.

Upvotes: 1

Abdur Rohman
Abdur Rohman

Reputation: 2944

The following code probably gives the result you want to achieve:

First, create an object to store the blank CCA1-CCA2 plot

p1 = plot(ord, type = "n")

Find and then save the coordinates of the sites 18 and 21

p1$p1$sites[c("18", "21"),]
#         CCA1      CCA2
#18  0.3496725 -1.334061
#21 -0.8617759 -1.588855
site18 = p1$sites["18",]
site21 = p1$sites["21",]

Overlay the blank CCA1-CCA2 plot with the points of site 18 and 21. Setting different colors to different points might be a good idea.

points(p1$sites[c("18", "21"),], pch = 19, col = c("blue", "red"))

Showing labels might be informative.

text(x = site18[1], y = site18[2] + 0.3, labels = "site 18")
text(x = site21[1], y = site21[2] + 0.3, labels = "site 21")

Here is the resulted plot. enter image description here

Upvotes: 0

Related Questions