Description Usage Arguments Value Examples
View source: R/plot-clusters.R
Plot clusters
1 2 3 4 5 6 7 8 9 |
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()]. |
A ggplot2 object.
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"
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.