我正在使用 vegan 包做一些分析和绘图,像这样
require(vegan)
data(varechem)
data(varespec)
rdam <- rda(varechem,scale=T)
scal=2
ef <- envfit(rdam ~ Cal.vul, data = varespec)
sf <- ordisurf(rdam ~ Cal.vul, data = varespec, plot = FALSE, scaling = scal)
plot(rdam, type="po",scaling = scal)
spe.sc <- scores(rdam, choices=1:2, display="sp",scaling = scal)
arrows(0, 0, spe.sc[, 1], spe.sc[, 2], length=0, lty=1, col="red")
ordilabel(rdam,dis="sp",cex=0.8,col="red",scaling = scal)
plot(ef)
plot(sf, col = "darkgreen", add = TRUE)
但我必须重复相同的情节,只改变变量 Cal.vul 所以我想做一个函数:
plot_spcSurface <- function(rdam,speFram, speName, scal=2 )
{
ef <- envfit(rdam ~ speName, data = speFram)
sf <- ordisurf(rdam ~ speName, data = speFram, plot = FALSE, scaling = scal)
plot(rdam, type="po",scaling = scal)
spe.sc <- scores(rdam, choices=1:2, display="sp",scaling = scal)
arrows(0, 0, spe.sc[, 1], spe.sc[, 2], length=0, lty=1, col="red")
ordilabel(rdam,dis="sp",cex=0.8,col="red",scaling = scal)
plot(ef)
plot(sf, col = "darkgreen", add = TRUE)
}
plot_spcSurface(rdam,varespec,Cal.vul)
它给
objeto 'Cal.vul' not found
如果我这样称呼它
plot_spcSurface(rdam,varespec,varespec$Cal.vul)
它有效,但是物种的名称不正确,标记为“speName”而不是“Cal.vul”,我无法从函数内部获取名称,正确的方法是什么?
谢谢