embed_plotly: Embedding Plot Using the JavaScript Library Plotly

View source: R/visualization.R

embed_plotlyR Documentation

Embedding Plot Using the JavaScript Library Plotly

Description

Plots the embedded coordinates, with each point colored by a specified color, using Plotly.

Usage

embed_plotly(coords, x = NULL, colors = NULL,
  color_scheme = grDevices::rainbow, cex = 1, text = NULL,
  equal_axes = FALSE)

Arguments

coords

Matrix of embedded coordinates, with as many rows as observations, and 2 columns; or the return value of running sneer.

x

Either a data frame or a column that can be used to derive a suitable vector of colors. Ignored if colors is provided.

colors

Vector containing colors for each coordinate.

color_scheme

Either a color ramp function, or the name of a ColorBrewer scheme. See 'Details'.

cex

Size of the points. Ignored if text is provided.

text

Vector of label text to display instead of a point. If the labels are long or the data set is large, this is unlikely to be very legible, but is occasionally useful.

equal_axes

If TRUE, the X and Y axes are set to have the same extents.

Details

This will open a web browser if you are using the R CLI. In RStudio, it will put the plot in RStudio's Plots tab.

The x argument can be used to provide a suitable vector of colors from either a data frame or vector.

If a data frame is provided, then a vector of colors will be looked for. If it's present, it will be used as the colors argument directly. Otherwise, a factor column will be looked for, and each level will be mapped to a different color. Otherwise, one color will be used for each point. If more than one column of a type is found in the data frame, the last one encountered is used.

If a vector is provided, a similar procedure to the data frame is used when mapping from its content to a vector of colors. Additionally, a numeric vector can be provided, which will be linearly mapped to a color scheme.

The color_scheme parameter can be one of either a color ramp function, accepting an integer n as an argument and returning n colors, or the name of a ColorBrewer color scheme. Probably should be one of the "Qualitative" set.

For some applicable color ramp functions, see the Palettes help page in the grDevices package (e.g. by running the ?rainbow command).

Note

Use of this function requires installing and loading the plotly package, using version 4 or above.

More information on plotly is available at its website, https://plot.ly.

Examples

## Not run: 
# Embed with PCA
pca_iris <- sneer(iris, method = "pca", ret = c("dy"))

#' library("plotly")
# Visualize the resulting embedding, colored by iris species, using the
# rainbow color scheme
embed_plotly(pca_iris$coords, iris$Species, color_scheme = rainbow)

# same as above, save typing '$coords' each time
embed_plotly(pca_iris, iris$Species, color_scheme = rainbow)

# topo.colors scheme
embed_plotly(pca_iris$coords, iris$Species, color_scheme = topo.colors)

# Pass in data frame and it will use any factor column it finds
embed_plotly(pca_iris$coords, iris)

# Use the "Dark2" ColorBrewer scheme
embed_plotly(pca_iris$coords, iris, color_scheme = "Dark2")

# Can plot the category names instead of points, but looks bad if they're
# long (or the dataset is large)
embed_plotly(pca_iris$coords, iris$Species, cex = 0.5, text = iris$Species)

tsne_iris <- sneer(iris, method = "tsne", ret = c("dx", "dy", "deg"))
# how well is the 32 nearest neighborhood preserved for each point?
nbr_pres_32 <- nbr_pres(tsne_iris$dx, tsne_iris$dy, 32)
# Project preservation onto each point with a sequential color scheme
embed_plot(tsne_iris$coords, nbr_pres_32, color_scheme = "Blues")

## End(Not run)

jlmelville/sneer documentation built on Nov. 15, 2022, 8:13 a.m.