With the added information (package) I was able to go and dig a bit deeper.
The problem is (in short) that autoplot.decorana
adds the data to the specific layer (either geom_point
or geom_text
). This is not inherited to other layers, so adding additional layers results in blank pages.
Basically notice that one of the 2 code strings below results in an error, and note the position of the data
argument:
# Error:
ggplot() +
geom_point(data = mtcars, mapping = aes_string(x = 'hp', y = 'mpg')) +
geom_label(aes(x = hp, y = mpg, label = cyl))
# Work:
ggplot(data = mtcars) +
geom_point(mapping = aes_string(x = 'hp', y = 'mpg')) +
geom_label(aes(x = hp, y = mpg, label = cyl))
ggvegan:::autoplot.decorana
places data as in the example the returns an error.
I see 2 ways to get around this problem:
- Extract the layers data using
ggplot_build
or layer_data
and create an overall or single layer mapping.
- Extract the code for generating the data, and create our plot manually (not using
autoplot
).
I honestly think the second is simpler, as we might have to extract more information to make our data sensible. By looking at the source code of ggvegan:::autoplot.decorana
(simply printing it to console by leaving out brackets) we can extract the below code which generates the same data as used in the plot
ggvegan_data <- function(object, axes = c(1, 2), layers = c("species", "sites"), ...){
obj <- fortify(object, axes = axes, ...)
obj <- obj[obj$Score %in% layers, , drop = FALSE]
want <- obj$Score %in% c("species", "sites")
obj[want, , drop = FALSE]
}
With this we can then generate any plot that we desire, with appropriate mappings rather than layer-individual mappings
dune.plot.data <- ggvegan_data(dune.dca)
p <- ggplot(data = dune.dca, aes(x = DCA1, DCA2, colour = Score)) +
geom_point() +
geom_text(aes(label = Label), nudge_y = 0.3)
p
Which gives us what I hope is your desired output
