Skip to content
Prev 1511 / 7420 Next

Making this plot in R: relative abundance vs. environmental gradient with each species ranked by niche optimum

On Tue, 2010-09-14 at 08:08 -0700, Laura S wrote:
Thanks Laura,

One way to do this would be

set.seed(123)
## generate a gradient
grad <- seq(0, 6, length = 20)
## and optima
opt <- runif(20, min = min(grad), max = max(grad))
## and some rel. abundances
abun <- rbeta(20, 1, 2)

## plot it
plot(abun ~ grad, type = "n", ylim = range(c(0, abun)))
points(abun ~ opt, type = "h")
points(abun ~ opt, type = "p", pch = 21, col = "black", bg = "white")

In the above I use pch 21 so I can fill the points with "white" to
obscure the histogram bars 'type = "h"'.

## and if we have two groups say, A and B,
## like Vallend 2008, then we'd do
## grouping variable
grp <- gl(2,10, labels = LETTERS[1:2])
plot(abun ~ grad, type = "n", ylim = range(c(0, abun)))
points(abun ~ opt, type = "h")
points(abun ~ opt, type = "p", pch = 21, col = "black",
       bg = c("white","black")[grp])
legend("topleft", legend = c("A","B"), pt.bg = c("white","black"),
       pch = 21, col = "black", bty = "n")

If you want to add a baseline, then insert a call to abline()
immediately *after* the plot call, e.g.:

plot(abun ~ grad, type = "n", ylim = range(c(0, abun)))
## add baseline
abline(h = 0, col = "grey")
points(abun ~ opt, type = "h")
points(abun ~ opt, type = "p", pch = 21, col = "black",
       bg = c("white","black")[grp])
legend("topleft", legend = c("A","B"), pt.bg = c("white","black"),
       pch = 21, col = "black", bty = "n")
## box() call required as abline will over write the initial box 
## slightly
box()

<snip />
I don't like this version - why are the points joined as if a series
along the gradient?

HTH

G