Learn R Programming

dbrobust (version 1.0.0)

plot_qgraph: Plot a Network Graph from a Distance Matrix

Description

This internal function visualizes a network graph representation of a distance matrix, where nodes represent observations and edges represent similarity. Groups can be specified for node coloring. A maximum number of nodes can be set to avoid overcrowding, and weak edges are thresholded.

Usage

plot_qgraph(
  dist_mat,
  group = NULL,
  max_nodes = 100,
  label_size = 2,
  edge_threshold = 0.1,
  layout = "spring",
  seed = 123,
  main_title = NULL
)

Value

Invisibly returns NULL. The plot is drawn as a side effect.

Arguments

dist_mat

A square distance matrix or a dist object. Distances are automatically normalized to [0,1] and converted to similarity via 1 - distance.

group

Optional factor or vector indicating group membership for nodes, used for coloring.

max_nodes

Integer. Maximum number of nodes to plot. If the number of observations exceeds this, stratified sampling is performed to reduce the node count.

label_size

Numeric. Size of the node labels.

edge_threshold

Numeric between 0 and 1. Edges with similarity below this threshold are removed.

layout

Character string specifying the layout algorithm for qgraph. Default is "spring".

seed

Integer. Random seed used for reproducibility during sampling and layout.

main_title

Optional character string specifying the main title of the plot.

Details

This function is internal and not intended for direct use. It is called by visualize_distances() to display network graphs of robust distances.

Features:

  • Converts dist objects to matrices automatically.

  • Downsamples nodes if the number of observations exceeds max_nodes, using stratified sampling by group.

  • Normalizes the distance matrix to [0,1] and converts it to similarity (1 - distance).

  • Removes weak edges below edge_threshold.

  • Colors nodes according to group membership.

  • Adds a main title using title() after plotting with qgraph.

Examples

Run this code
# --------------------------------------
# Network Graph Example from Robust Distances
# --------------------------------------
data("Data_HC_contamination", package = "dbrobust")
# Subset small dataset
Data_small <- Data_HC_contamination[1:20, ]

cont_vars <- c("V1", "V2", "V3", "V4")
cat_vars  <- c("V5", "V6", "V7")
bin_vars  <- c("V8", "V9")
w <- Data_small$w_loop

# Compute GGower robust distances
dist_sq_ggower <- dbrobust::robust_distances(
  data = Data_small,
  cont_vars = cont_vars,
  bin_vars  = bin_vars,
  cat_vars  = cat_vars,
  w = w,
  alpha = 0.10,
  method = "ggower"
)

# Create factor indicating Normal vs Outlier
n_obs <- nrow(dist_sq_ggower)
group_vec <- rep("Normal", n_obs)
group_vec[attr(dist_sq_ggower, "outlier_idx")] <- "Outlier"
group_factor <- factor(group_vec, levels = c("Normal", "Outlier"))

# Plot network graph (small, for CRAN)
dbrobust::plot_qgraph(
  dist_mat = sqrt(dist_sq_ggower),
  group = group_factor,
  max_nodes = 10,
  label_size = 2,
  edge_threshold = 0.1,
  layout = "spring",
  seed = 123,
  main_title = "GGower Network Graph with Outliers"
)

Run the code above in your browser using DataLab