Title: | Make 'ggplot2' Versions of Vegan's Ordiplots |
---|---|
Description: | The 'vegan' package includes several functions for adding features to ordination plots: ordiarrows(), ordiellipse(), ordihull(), ordispider() and ordisurf(). This package adds these same features to ordination plots made with 'ggplot2'. In addition, gg_ordibubble() sizes points relative to the value of an environmental variable. |
Authors: | John Quensen [aut, cre, cph] , Gavin Simpson [aut], Jari Oksanen [aut] |
Maintainer: | John Quensen <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.4.4 |
Built: | 2024-11-22 06:08:28 UTC |
Source: | https://github.com/jfq3/ggordiplots |
Fits environmental parameters to an ordination plot of sites and plots them as arrows or centroid labels.
gg_envfit( ord, env, groups = NA, scaling = 1, choices = c(1, 2), perm = 999, alpha = 0.05, angle = 20, len = 0.5, unit = "cm", arrow.col = "red", pt.size = 3, show_factors = FALSE, plot = TRUE )
gg_envfit( ord, env, groups = NA, scaling = 1, choices = c(1, 2), perm = 999, alpha = 0.05, angle = 20, len = 0.5, unit = "cm", arrow.col = "red", pt.size = 3, show_factors = FALSE, plot = TRUE )
ord |
An ordination object. |
env |
A data frame of environmental parameters. |
groups |
A vector of groups. |
scaling |
Scaling value for plot. |
choices |
Axes to plot. |
perm |
Number of permutations. |
alpha |
Maximum alpha value to be included in plot. |
angle |
Angle of arrow tips. |
len |
Arrow tip length. |
unit |
Unit for length ("cm", "in") |
arrow.col |
Arrow color. |
pt.size |
Symbol size. |
show_factors |
A logical for including factors on the plot; defaults to FALSE. |
plot |
A logical for plotting; defaults to TRUE. |
Vegan's envfit function fits environmental variables to an ordination. If the variable is numeric, an arrow is fitted to the ordination plot indicating the direction in which that variable increases. If the variable is a factor (or character vector), vegan's envfit plotting function treats levels of the factor as groups and adds labels for the factor's level to the ordination plot at each group's centroid center. This behavior is now enabled in gg_envfit() if show_factors is set to TRUE.
Silently returns the plot and data frames used for the plotting if the fit of any variable is significant at alpha. Otherwise returns a message that no variable is significant.
In order for the arrow tips to be labeled with the names of the variables, they must be supplied as a matrix or data frame. If a single variable is supplied as a vector, the arrow tip will be labeled with "1". A way-around is to convert the vector to a data frame with the column named for the variable.
data("varespec") data("varechem") vare.dist <- vegdist(varespec) vare.mds <- monoMDS(vare.dist) gg_envfit(ord=vare.mds, env=varechem) data("dune") data("dune.env") dune.dist <- vegdist(dune) dune.mds <- monoMDS(dune.dist) # A1 supplied as a vector gg_envfit(dune.mds, env=dune.env$A1, groups=dune.env$Management) # A1 supplied as a data frame A1 <- as.data.frame(dune.env$A1) colnames(A1) <- "A1" gg_envfit(dune.mds, env=A1, groups=dune.env$Management)
data("varespec") data("varechem") vare.dist <- vegdist(varespec) vare.mds <- monoMDS(vare.dist) gg_envfit(ord=vare.mds, env=varechem) data("dune") data("dune.env") dune.dist <- vegdist(dune) dune.mds <- monoMDS(dune.dist) # A1 supplied as a vector gg_envfit(dune.mds, env=dune.env$A1, groups=dune.env$Management) # A1 supplied as a data frame A1 <- as.data.frame(dune.env$A1) colnames(A1) <- "A1" gg_envfit(dune.mds, env=A1, groups=dune.env$Management)
Makes a simple ordination plot of site with the symbol size scaled to an environmental variable. Result is similar to that of BiodiversityR's ordibubble function.
gg_ordibubble( ord, env.var, groups = NA, var.label = "Level", choices = c(1, 2), plot = TRUE )
gg_ordibubble( ord, env.var, groups = NA, var.label = "Level", choices = c(1, 2), plot = TRUE )
ord |
An ordination object |
env.var |
An environmental variable. |
groups |
A vector of groups (optional). |
var.label |
Label for the legend; default is "Level." |
choices |
Axes to be plotted. |
plot |
A logical for plotting; defaults to TRUE. |
Silently returns the plot and data frames used for the plotting.
data(dune) data(dune.env) dune.bray <- vegdist(dune, method = "bray") ord <- cmdscale(dune.bray, k=(nrow(dune)-1), eig=TRUE, add=TRUE) gg_ordibubble(ord, env.var=dune.env$A1, var.label="A1")
data(dune) data(dune.env) dune.bray <- vegdist(dune, method = "bray") ord <- cmdscale(dune.bray, k=(nrow(dune)-1), eig=TRUE, add=TRUE) gg_ordibubble(ord, env.var=dune.env$A1, var.label="A1")
Modeled after the ordicluster function in vegan, this function overlays an ordination object with a cluster dendogram. Functionality has been added to include treatment groups.
gg_ordicluster( ord, cluster, treatments = NA, choices = c(1, 2), prune = 0, col = 1, pt.size = 3, plot = TRUE )
gg_ordicluster( ord, cluster, treatments = NA, choices = c(1, 2), prune = 0, col = 1, pt.size = 3, plot = TRUE )
ord |
An ordination object. |
cluster |
A cluster object from 'hclust' based on the same distance as 'ord.' |
treatments |
A vector assigning treatments to samples. |
choices |
Ordination axes to be plotted. |
prune |
Number of upper level hierarchies removed from the dendrogram. If prune > 0, dendrogram will be disconnected. |
col |
A vector of cluster group memberships. Used to assign colors to line segments for each cluster group. |
pt.size |
Symbol size. |
plot |
A logical; defaults to TRUE. |
'treatments' should be a vector of class factor and length equal to the number of samples included in the ordination and cluster; integers are not coerced into factors.
Invisibly returns a list of the data frames used to make the plot (df_ord, df_segments) and the plot itself (plot).
Jari Oksanen, John Quensen
data(dune) data(dune.env) dune.bray <- vegdist(dune, method="bray") ord <- metaMDS(dune, k=3) cl <- hclust(dune.bray, method="complete") gg_ordicluster(ord, cluster=cl, treatments=dune.env$Management, prune=3, col=cutree(cl, 4))
data(dune) data(dune.env) dune.bray <- vegdist(dune, method="bray") ord <- metaMDS(dune, k=3) cl <- hclust(dune.bray, method="complete") gg_ordicluster(ord, cluster=cl, treatments=dune.env$Management, prune=3, col=cutree(cl, 4))
gg_ordiplot
uses ggplot2
to make an ordination plot
with group ellipses by default, and optionally hulls and/or
spiders. It is patterned after vegan
's functions ordiellipse
,
ordihull
, and ordispider
and accepts similar parameters.
gg_ordiplot( ord, groups, scaling = 1, choices = c(1, 2), kind = c("sd", "se", "ehull"), conf = NULL, show.groups = "all", ellipse = TRUE, label = FALSE, hull = FALSE, spiders = FALSE, pt.size = 3, plot = TRUE )
gg_ordiplot( ord, groups, scaling = 1, choices = c(1, 2), kind = c("sd", "se", "ehull"), conf = NULL, show.groups = "all", ellipse = TRUE, label = FALSE, hull = FALSE, spiders = FALSE, pt.size = 3, plot = TRUE )
ord |
An ordination object. |
groups |
A vector of groups. |
scaling |
Scaling for ordination plot. |
choices |
Axes to be plotted. |
kind |
Type of ellipses to show ("se", sd", "ehull"). |
conf |
Confidence value for ellipses if "se" or "sd." |
show.groups |
Subset of groups to plot. |
ellipse |
A logical for plotting ellipses; defaults to TRUE. |
label |
A logical for labeling group centroids. |
hull |
A logical for plotting group hulls. |
spiders |
A logical for plotting group spiders. |
pt.size |
Symbol size. |
plot |
A logical for plotting; defaults to TRUE. |
Silently returns the plot and data frames used for the plotting.
data("dune") data("dune.env") dune.hel <- decostand(dune, method = "hellinger") ord <- rda(dune.hel) gg_ordiplot(ord, groups = dune.env$Management)
data("dune") data("dune.env") dune.hel <- decostand(dune, method = "hellinger") ord <- rda(dune.hel) gg_ordiplot(ord, groups = dune.env$Management)
Fits a surface (contour) plot of an environmental variable to an ordination plot.
gg_ordisurf( ord, env.var, groups = NA, choices = c(1, 2), var.label = "Level", binwidth, pt.size = 3, family = "gaussian", plot = TRUE )
gg_ordisurf( ord, env.var, groups = NA, choices = c(1, 2), var.label = "Level", binwidth, pt.size = 3, family = "gaussian", plot = TRUE )
ord |
An ordination object. |
env.var |
Environmental variable to fit to plot. |
groups |
A vector of groups (optional). |
choices |
Axes to plot. |
var.label |
Label for the contour legend; default is "Level." |
binwidth |
Controls the number of contours in the plot. |
pt.size |
Symbol size. |
family |
Error distribution and link function used by the gam function to fit the contours. |
plot |
A logical for plotting; defaults to TRUE. |
By default, 'binwidth' is calculated as the difference between minimum and maximum values of the variable divided by 15.
The colors for the points are mapped to fill; if you want to rename the legend for the groups (points), use labs(fill="New name").
See the help for stats::family for possible values for family.
Silently returns the plot and data frames used for the plotting.
Code for extracting plot data from the ordisurf result was taken from a blog by Oliviea Rata Burge.
Olivia Rata Burge, John Quensen
https://oliviarata.wordpress.com/2014/07/17/ordinations-in-ggplot2-v2-ordisurf/'
data(varespec) data(varechem) vare.dist <- vegdist(varespec) vare.mds <- monoMDS(vare.dist) gg_ordisurf(vare.mds, env.var = varechem$Baresoil, var.label="Bare Soil")
data(varespec) data(varechem) vare.dist <- vegdist(varespec) vare.mds <- monoMDS(vare.dist) gg_ordisurf(vare.mds, env.var = varechem$Baresoil, var.label="Bare Soil")
The vegan and BiodiversityR packages include several functions for adding features to ordination plots: ordiarrows, ordibubbles, ordiellipse, ordihull, ordispider, ordisurf, ordicluster. This package adds these same features to ordination plots made with ggplot2.
gg_enfit Fits vectors representing environmental variables to ordination plots.
gg_ordibubble Sizes site symbols in proportion to environmental variable.
gg_ordicluster Overlays cluster diagram on ordination plot.
gg_ordiplot Ordination plot with options for ellipses, hulls, and spiders distinguishing treatment groups.
gg_ordisurf Ordination plot with surface contours for environmental variable.
Makes ordination axis labels that include, if appropriate, the % total variance explained by each axis.
ord_labels(ord)
ord_labels(ord)
ord |
A vegan ordination object. |
If there are no eigenvalues in ord, or if any eigenvalues are less than 0, each element of the vector returned has the form "DIMn" where n is the axis number. Otherwise, each element of the vector returned has the form "AxisN xx.x%" where "Axis" is taken from the vector of eigenvalues in ord if they are named or simply "DIM" if they are not, N is the number of the axis, and xx.x is the % of total variance explained by the axis.
A character vector, each element of which can be used to label the corresponding axis of an ordination plot.
data("dune") data("dune.env") dune_hel <- decostand(dune, method = "hellinger") ord <- rda(dune_hel) axis_labels <- ord_labels(ord) axis_labels[c(1,2)]
data("dune") data("dune.env") dune_hel <- decostand(dune, method = "hellinger") ord <- rda(dune_hel) axis_labels <- ord_labels(ord) axis_labels[c(1,2)]
Scales envfit arrows to fit within 75
scale_arrow(arrows, data, at = c(0, 0), fill = 0.75)
scale_arrow(arrows, data, at = c(0, 0), fill = 0.75)
arrows |
A two column data frame of coordinates from envfit result. |
data |
A two column data frame of coordinates for ordination plot. |
at |
coordinates of origin (0, 0) |
fill |
proportion of plot area to fill with maximum arrow length |
Silently returns a data frame of scaled coordinates for adding arrows to ordination plot.
Jari Oksanen with modifications by Gavin Simpson and John Quensen
data("varespec") data("varechem") vare_dist <- vegdist(varespec) vare_mds <- monoMDS(vare_dist) plt1 <- gg_envfit(ord=vare_mds, env=varechem, plot = FALSE) mult <- scale_arrow(plt1$df_arrows, plt1$df_ord[ , c("x", "y")]) mult
data("varespec") data("varechem") vare_dist <- vegdist(varespec) vare_mds <- monoMDS(vare_dist) plt1 <- gg_envfit(ord=vare_mds, env=varechem, plot = FALSE) mult <- scale_arrow(plt1$df_arrows, plt1$df_ord[ , c("x", "y")]) mult