Search code examples
rvegan

Change colour of the web in vegan::ordispider


I am trying to plot the results of PCoA analysis. For now I am trying with R basic plots, I will maybe then move to ggplot2.

My problem is that I don't seem able to change the colour of the spider in ordispider. I can change the colour of the points, but I would like the spider to match these for increased understandability of the plot.

Here is what I got:

library(vegan)
opar <- par(no.readonly = TRUE)

# Import data
alevines <- read.csv("Alevins_2020_ANALYSISFINAL.csv")

# Data transformation
dens <- subset(alevines, select = Dsar:Sym)
dens_rt4 <- dens^(1/4)

# Bray-Curtis distance matrix
bray_rt4 <- vegdist(dens_rt4, method = "bray")

# Tranformation of Bray-Curtis matrix to solve negative eigenvalues
sqrt <- sqrt(bray_rt4)
pcoa_sqrt <- cmdscale(sqrt, eig = TRUE)

# Extraction of points' coordinates to simplify
pcoa_final <- pcoa_sqrt$points

# Species information
spp_pcoa <- envfit(pcoa_final, dens)

# Plot
par(pty = "s")
plot(pcoa_final, asp = 1, type = "n",
     xlab = "PCoA 1", ylab = "PCoA 2",
     main = "PCoA Analysis \n Effect of the geographical zone on juvenile assemblages")
ordispider(pcoa_final, group = alevines$Zone, col = "grey", label = TRUE)
points(pcoa_final, pch = 21, bg = factor(alevines$Zone))
plot(spp_pcoa, col = "blue")

par(opar)

# Data that I used:
df <- cbind(alevines[, 3], as.data.frame(pcoa_final))

df #  where "alevines[, 3]"] is the variable Zone that is relevant for the plot (I get error if I try to change the name)

   alevines[, 3]          V1          V2
1          N_FRA -0.31953966 -0.13211577
2          N_FRA -0.34125036 -0.14192262
3          N_FRA -0.10213578 -0.32434163
4          N_FRA -0.06270165 -0.32624418
5          N_FRA  0.16412846 -0.10339652
6          S_FRA  0.10157215 -0.07840044
7          S_FRA  0.01062563 -0.06712275
8          S_FRA -0.09468745  0.05534594
9          S_FRA  0.21076827 -0.06278015
10         S_FRA  0.04307528 -0.13179782
11         S_FRA -0.07427778  0.17044107
12         S_FRA -0.05814288  0.15888828
13         S_FRA -0.23892210 -0.15302405
14         S_FRA  0.02524980 -0.36828943
15         S_FRA -0.11896887  0.18927065
16         S_FRA -0.22328449  0.12771682
17         S_FRA -0.09757700  0.08558142
18         S_FRA -0.04772112  0.18979261
19         S_FRA  0.08473274 -0.19854426
20         S_FRA -0.11273182  0.02583739
21         S_FRA -0.10407023  0.18530682
22         S_FRA  0.03202366  0.05072311
23         N_ESP -0.01593828  0.19298540
24         N_ESP -0.16013361  0.06503471
25         N_ESP  0.08506459 -0.10828077
26         N_ESP  0.02226936  0.02115659
27         N_ESP  0.26005384  0.08631482
28         N_ESP  0.10542508 -0.08439371
29         N_ESP  0.26768177  0.07939082
30         N_ESP  0.26699288  0.01749142
31         N_ESP  0.18508141 -0.20541575
32         N_ESP  0.15115365  0.02252165
33         N_ESP -0.20182258  0.12868039
34         N_ESP  0.03325551 -0.11145528
35         N_ESP -0.13561777  0.19630368
36         N_ESP -0.19252249  0.13937554
37         N_ESP -0.20645713  0.01362222
38         N_ESP  0.02491676 -0.15700943
39         S_ESP  0.25340805  0.14671086
40         S_ESP  0.13124834  0.16250687
41         S_ESP  0.21613607  0.12191268
42         S_ESP  0.23363973  0.12162282

I tried to substitute col in ordispider like this:

ordispider(pcoa_final, group = alevines$Zone, factor(alevines$Zone), label = TRUE)

But I get all spiders green, which is not what I want :( How do I change the spiders' colour from grey to the same colour that are assigned to the points? I hope I gave enough information :)


Solution

  • Please note that you got to name of your arguments if they are not in the correct position. The third argument of ordispider is display and that really does not understand what to do with factor(alevines$Zone). The argument for colour is called col. It should match the groups – not the points. So it should be a vector of length of groups. For three groups, argument col = 1:3 should set the colours for three groups from the standard palette.