beta_diversity_3d: Draw 3D Scatterplot of Beta Diversity

View source: R/beta_diversity_3d.R

beta_diversity_3dR Documentation

Draw 3D Scatterplot of Beta Diversity

Description

This is a wrapper function for plot_ly with the "scatterplot3d" option. It can be used on a data frame, or directly on the output of phyloseq::ordinate with the NMDS or PCoA method (or vegan::metaMDS or ape::pcoa, respectively), or on the output of limma::plotMDS. Default categorical colors are from dittoSeq.

Usage

beta_diversity_3d(x, ...)

## S3 method for class 'data.frame'
beta_diversity_3d(x, axes = colnames(x)[1:3],
                  color.column = colnames(x)[4],
                  label.column = colnames(x)[5],
                  color.key = NULL, ...)

## S3 method for class 'monoMDS'
beta_diversity_3d(x, metadata,
                  color.column,
                  label.column = NULL,
                  color.key = NULL, ...)

## S3 method for class 'pcoa'
beta_diversity_3d(x, metadata,
                  color.column,
                  label.column = NULL,
                  color.key = NULL, ...)

## S3 method for class 'MDS'
beta_diversity_3d(x, metadata,
                  color.column,
                  label.column = NULL,
                  color.key = NULL, ...)

Arguments

x

A data frame, "monoMDS", "pcoa", or "MDS" object containing ordination results.

axes

If x is a data frame, the column names for three axes to plot.

color.column

The name of a numeric or categorical column to be used for coloring points. This column should be found in x if x is a data frame, or in metadata otherwise.

label.column

The name of a character column to be used for labeling points. This column should be found in x if x is a data frame, or in metadata otherwise. It defaults to sample names if x is a "monoMDS" or "MDS".

color.key

If color.column refers to a character or factor column, a named vector of colors, with names corresponding to values in the column. The default is to use dittoSeq::dittoColors. If color.column refers to a numeric column, a long vector of colors to be used as the color scale. The default is to use viridis. Passed to the colors argument of plot_ly.

metadata

A data frame of sample metadata, in the same order as x.

...

Optional arguments passed to plot_ly.

Value

A "plotly" object.

Author(s)

Lindsay Clark

Examples

## Not run: 
# Perform NMDS with Bray distance

ord_mfiber_prop1a <- ordinate(ps_mfiber_prop, "NMDS", "bray", k = 3)

beta_diversity_3d(ord_mfiber_prop1a,
                  sample_data(ps_mfiber_prop), "TRT", "Label")

# NMDS with UniFrac distance (ordinate won't let you adjust k)

dist_mfiber_4 <- phyloseq::distance(ps_mfiber_prop, "unifrac")
ord_mfiber_prop4a <- vegan::metaMDS(dist_mfiber_4, k = 3)

beta_diversity_3d(ord_mfiber_prop4a,
                  sample_data(ps_mfiber_prop), "TRT", "Label")
                  
# MDS of gene expression with limma
mds1 <- plotMDS(logCPM.filt, top = 5000)
beta_diversity_3d(mds1, metadata = d.filt$samples,
                  color.column = "Group")

# Changing title and axis labels, manually setting colors, and saving

mfiber_colors <- c(CO = "magenta", BP = "black", MF = "turquoise",
                   FOS = "orange", RS = "skyblue", TP = "green")

p1 <- beta_diversity_3d(ord_mfiber_prop1a,
                  sample_data(ps_mfiber_prop), "TRT", "Label",
                  color.key = mfiber_colors) %>%
  plotly::layout(title = "NMDS with Bray distance",
         scene = list(xaxis = list(title = "Axis 1"),
                      yaxis = list(title = "Axis 2"),
                      zaxis = list(title = "Axis 3")))
htmlwidgets::saveWidget(partial_bundle(p1),
                        file = "results/NMDS_Bray_MFiber_plotly.html")

## End(Not run)

HPCBio/plotly_microbiome documentation built on May 9, 2022, 11:37 p.m.