Learn R Programming

specmine (version 4.0.0)

tsne_kmeans_plot2D: t-SNE 2D k-means plot

Description

Creates a two-dimensional t-SNE plot coloured by k-means clusters.

Usage

tsne_kmeans_plot2D(
  dataset,
  tsne.result,
  num.clusters = 3,
  dims = c(1, 2),
  kmeans.result = NULL,
  labels = FALSE,
  bw = FALSE,
  ellipses = FALSE,
  leg.pos = "right",
  xlim = NULL,
  ylim = NULL
)

Value

A ggplot object.

Arguments

dataset

Dataset to cluster.

tsne.result

Result returned by tsne_analysis_dataset().

num.clusters

Number of k-means clusters.

dims

Two embedding dimensions to plot.

kmeans.result

Optional result returned by clustering().

labels

Logical indicating whether sample labels should be displayed.

bw

Logical indicating whether a black-and-white plot should be used.

ellipses

Logical indicating whether group ellipses should be displayed.

leg.pos

Legend position.

xlim

Optional x-axis limits.

ylim

Optional y-axis limits.

Examples

Run this code
if (requireNamespace("Rtsne", quietly = TRUE)) {
  datamat <- matrix(
    rnorm(40),
    nrow = 5,
    dimnames = list(
      paste0("x", 1:5),
      paste0("s", 1:8)
    )
  )

  dataset <- list(
    data = datamat,
    metadata = data.frame(
      class = factor(rep(c("A", "B"), length.out = 8))
    )
  )

  tsne.result <- tsne_analysis_dataset(
    dataset,
    perplexity = 2,
    max_iter = 250,
    seed = 42
  )

  kmeans.result <- list(
    cluster = rep(1:2, each = 4)
  )

  tsne_kmeans_plot2D(
    dataset,
    tsne.result,
    num.clusters = 2,
    kmeans.result = kmeans.result
  )
}

Run the code above in your browser using DataLab