Comments (2)
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)
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
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.
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)
- autoplot.prc should display species labels on the right of the plot
- Add `autolayer()` methods to facilitate customised plotting
- options to improve CCA autoplot in ggvegan HOT 1
- Lollipop chart to illustrate the species scores to an axis
- Labeling Ordination Plots HOT 3
- autoplot of rda showing only sites error HOT 1
- Issue downloading ggvegan in R (Version 1.1.463) HOT 11
- CCA plot issues HOT 3
- Adding biplot onto plot of fortify.rda()
- Only function avaliable is valid_layers() HOT 1
- autoplot.rda argument const is ignored HOT 4
- autoplot.rda issues HOT 1
- autoplot.rda creating an extra legend for site scores HOT 1
- Use of display= vs layers= in autoplot and scores
- no functions are exportable from package HOT 2
- Use ggrepel for the autoplot methods of ordination objects HOT 1
- `fortify()` should return a tibble
- Functions not loading HOT 1
- List of outstanding plot methods in vegan that need adding to ggvegan
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ggvegan.