fawda123 / ggord

a take on ordination plots using ggplot2
https://fawda123.github.io/ggord/
62 stars 20 forks source link

Error when using just one explanatory variable in a RDA model #19

Closed Conny42 closed 2 years ago

Conny42 commented 2 years ago

Function ggord throws an error then plotting a RDA result. Everything works fine if I have more than one explanatory variable in my model. However, with just one explanatory variable I got the error: Error in ord_in$CCA$wa[, axes] : subscript out of bounds (?)

Any help is very much appreciated....

library(ggord)
#> Warning: Paket 'ggord' wurde unter R Version 4.1.2 erstellt
library(vegan)
#> Lade nötiges Paket: permute
#> Lade nötiges Paket: lattice
#> This is vegan 2.5-7

data(dune)
data(dune.env)

model<-rda (dune ~ Condition(Moisture) + A1, scale=F,data = dune.env)
ggord(model)
#> Error in ord_in$CCA$wa[, axes]: Indizierung außerhalb der Grenzen
ggord(model, dune.env$Moisture,ellipse = FALSE,sizepch=10,txt = 4,size=2,repel=T,alpha=0.7, addpch=3,addcol="black")+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+ggtitle("")+ theme(legend.position="none")
#> Error in ord_in$CCA$wa[, axes]: Indizierung außerhalb der Grenzen

Created on 2022-01-26 by the reprex package (v2.0.0)

fawda123 commented 2 years ago

Hi @lumberjane, it does not make sense to plot an RDA model with only one continous explanatory variable. The model has only one constrained axis (RDA1) and it is impossible to plot in two or more dimensions.

Conny42 commented 2 years ago

Thanks @fawda123 - yes, I know that the model has only one constrained axis, but why not plot the first unconstrained axis (PC1) against the RDA1 axis, as done in the plot.cca function from the vegan library?

fawda123 commented 2 years ago

@lumberjane does something like this work? I would not be able to include this in the package, but you can use this script to create some custom plots that use the default method for ggord.

library(ggord)
library(vegan)

data(dune)
data(dune.env)

model<-rda (dune ~ Condition(Moisture) + A1, scale=F,data = dune.env)

# pick your axes
axis1 <- 'RDA1'
axis2 <- 'PC1'

# data to plot, site and species data
obs1 <- data.frame(ord_in$CCA$wa[, axis1])
obs2 <-  data.frame(ord_in$CA$u[, axis2])
obs <- cbind(obs1, obs2)
names(obs) <- c(axis1, axis2)

addpts1 <- data.frame(ord_in$CCA$v[, axis1])
addpts2 <- data.frame(ord_in$CA$v[, axis2])
addpts <- cbind(addpts1, addpts2)
names(addpts) <- c(axis1, axis2)

# vectors for constraining matrix
constr <- data.frame(ord_in$CCA$biplot[, axis1, drop = F])
constr <- cbind(constr, 0)
names(constr) <- axes

ggord.default(obs, vecs = constr, axes, addpts = addpts, ptslab = T)

image