Giter Club home page Giter Club logo

Comments (2)

gavinsimpson avatar gavinsimpson commented on September 17, 2024

Ordination diagrams need to have a fixed aspect ratio because the Euclidean distance on the plot is directly (or closely, but not perfectly, depending on scalings) related to some method-specific measure of dissimilarity. On a CCA, distances on the plot are related to the chi-square distance between samples.

To set this up in ggplot we need to use coord_equal() or coord_fixed(). For example (using the newer version of ggvegan, which changes a little how the scores are returned via fortify():

library('vegan')
library('ggvegan')
data(varespec, varechem)

ord <- cca(varespec ~ Al + P*(K + Baresoil), data=varechem)

The autoplot() method produces

autoplot(ord)

cca-autoplot

To recreate this, but just showing the biplot arrows we would do the following

ford <- fortify(ord, axes = 1:2)  # fortify the ordination
take <- c('CCA1', 'CCA2')  # which columns contain the scores we want
arrows <- subset(ford, Score == 'biplot')  # take only biplot arrow scores
## multiplier for arrows to scale them to the plot range
mul <- ggvegan:::arrowMul(arrows[, take],
                          subset(ford, select = take, Score == 'sites'))
arrows[, take] <- arrows[, take] * mul  # scale biplot arrows

Now plot

ggplot() +
  geom_point(data = subset(ford, Score == 'sites'),
             mapping = aes(x = CCA1, y = CCA2)) + 
  geom_segment(data = arrows,
             mapping = aes(x = 0, y = 0, xend = CCA1, yend = CCA2),
             arrow = arrow(length = unit(0.01, "npc"))) +
  geom_text(data = arrows, # crudely push labels away arrow heads
             mapping = aes(label = Label, x = CCA1 * 1.1, y = CCA2 * 1.1)) +
  coord_fixed()

This gives

cca-manual

If you want to include more scores in the plot, then the multiplier line needs to be:

mul <- ggvegan:::arrowMul(arrows[, take],
                          subset(ford, select = take, Score %in% c('sites')))

and add extra score types to the RHS of %in%.

from ggvegan.

padpadpadpad avatar padpadpadpad commented on September 17, 2024

Thanks Gavin

This is a fantastically reproducible example given the lack of reproducibility in my question.

Cannot really add anything in here...

I have fiddled with your plot function:

ggplot() +
  geom_point(data = subset(ford, Score == 'sites'),
             mapping = aes(x = CCA1, y = CCA2)) + 
  geom_segment(data = arrows,
               mapping = aes(x = 0, y = 0, xend = CCA1, yend = CCA2),
               arrow = arrow(length = unit(0.01, "npc"))) +
  geom_text(data = arrows,
            mapping = aes(label = Label, x = CCA1, y = CCA2, hjust = 0.5*(1 - sign(CCA1)), vjust = 0.5*(1-sign(CCA2)))) +
  coord_fixed() +
  scale_x_continuous(expand = c(.1, .1)) +
  scale_y_continuous(expand = c(.1, .1))

This will now always (if my code is right) keep labels away from arrow heads and the scale_x and scale_y_continuous will increase the axis spaces if your labels are quite long and at the edge of the plot space.

Thanks again Gavin. I really appreciate your help (and everything you do for the multivariate ecological community).

Cheers
Dan

from ggvegan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.