Legend in species accumulation plots

Viewed 228

How can I add a caption to this graph? I tried the commands below but my graph disappeared

library(vegan)
data(BCI)

sp1 <- specaccum(BCI, "random")
sp2 <- specaccum(BCI, "coleman")

plot(sp1, ci.type="poly", col="blue", lwd=2,
     ci.lty=0, ci.col = rgb(.5, .5, .5, 0.5))
plot(sp2, ci.type="poly", col="yellow",lwd=2,
     ci.lty=0, ci.col = rgb(.5, .5, .5, 0.5), add=TRUE)
legend(x="top", legend=sp1)
1 Answers

The reason why your plot is disappearing is that you are trying to create a legend containing the entire contents of sp1, which is a large numerical object. Presumably that's not what you want, and you're only looking for the text "sp1" to appear in the legend. You need to specify a vector of the strings you would like added as labels in your legend, as well as a vector the colours.

Note I have also added a title to this plot using main = "sp1" in the first call to plot:

library(vegan)
data(BCI)

sp1 <- specaccum(BCI, "random")
sp2 <- specaccum(BCI, "coleman")

plot(sp1, ci.type="poly", col="blue", lwd=2,
     ci.lty=0, ci.col = rgb(.5, .5, .5, 0.5), main = "sp1")
plot(sp2, ci.type="poly", col="yellow",lwd=2,
     ci.lty=0, ci.col = rgb(.5, .5, .5, 0.5), add = TRUE)
legend(x = "bottomright", legend = c("sp1", "sp2"), fill = c("blue", "yellow"))

Created on 2020-03-13 by the reprex package (v0.3.0)

Related