Learn R Programming

bertopicr (version 0.3.6)

visualize_documents_2d: Visualize Documents in 2D Space using BERTopic

Description

This function generates a 3D visualization of documents using a pre-trained BERTopic model and UMAP dimensionality reduction. It uses Plotly for interactive visualizations and saves the output as an HTML file.

Usage

visualize_documents_2d(
  model,
  texts,
  reduced_embeddings,
  custom_labels = FALSE,
  hide_annotation = TRUE,
  tooltips = c("Topic", "Name", "Probability", "Text"),
  filename = "visualize_documents_2d",
  auto_open = FALSE
)

Value

The function does not return a value but saves an HTML file containing the visualization and displays it in the current R environment.

Arguments

model

A BERTopic model object. Default is 'topic_model'.

texts

A character vector or list of cleaned text documents to visualize.

reduced_embeddings

A matrix or data frame of reduced-dimensionality embeddings (2D). Typically generated using UMAP.

custom_labels

Logical. If TRUE, custom topic labels are used. Default is FALSE.

hide_annotation

Logical. If TRUE, hides annotations on the plot. Default is TRUE.

tooltips

A character vector of tooltips for hover information. Default is c("Topic", "Name", "Probability", "Text").

filename

A character string specifying the name of the HTML file to save the visualization. Default is "visualize_documents_2d". The .html extension is automatically added if not provided.

auto_open

Logical. If TRUE, opens the HTML file in the browser after saving. Default is FALSE.

Examples

Run this code
# \donttest{
if (exists("topic_model") && exists("texts_cleaned") && exists("embeddings")) {
  visualize_documents_2d(model = topic_model,
    texts = texts_cleaned,
    reduced_embeddings = embeddings,
    custom_labels = FALSE,
    hide_annotation = TRUE,
    filename = "plot",
    auto_open = TRUE)
} else {
  message("Missing topic_model/texts_cleaned/embeddings. Train a model first.")
}
# }

Run the code above in your browser using DataLab