plot_clusters: Plot clusters

Description Usage Arguments Value Examples

View source: R/plot-clusters.R

Description

Plot clusters

Usage

1
2
3
4
5
6
7
8
9
plot_clusters(
  model,
  data = NULL,
  colour_vector = NULL,
  text_label = NULL,
  shape_by_group = FALSE,
  colour_label = "model",
  ...
)

Arguments

model

An object of class "partition" created by the functions [cluster::pam()] or [stats::kmeans()].

data

The original data frame used in the clustering.

colour_vector

A vector of character or factors to colour the points by.

colour_label

A label for the colour legend.

...

Other arguments to pass2 [factoextra::fviz_cluster()].

Value

A ggplot2 object.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
df <- haddock_mod %>%
  dplyr::select(fo_median, ffmsy_median, bbmsy_median) %>%
  scale()

# Evaluate the number of clusters
factoextra::fviz_nbclust(df, kmeans, method = "wss")
factoextra::fviz_nbclust(df, kmeans, method = "silhouette")
factoextra::fviz_nbclust(df, kmeans, method = "gap_stat")

# Illustrate an example with 2 clusters:
m <- kmeans(df, centers = 2L)

plot_clusters(m,
  data = df, colour_vector = haddock_mod$fmodel,
  colour_label = "F model"
)

# The default is to plot the two first principal components as x and y.
# Instead we can pick specific axes to show:
plot_clusters(m,
  data = df, colour_vector = haddock_mod$fmodel,
  colour_label = "F model",
  choose.vars = c("bbmsy_median", "ffmsy_median")
)

# Or with the more robust cluster::pam() algorithm:
m <- cluster::pam(df, k = 4L)
plot_clusters(m,
  data = df, colour_vector = haddock_mod$fmodel,
  colour_label = "F model"
)

pbs-assess/gfranges documentation built on Dec. 13, 2021, 4:50 p.m.