You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The function fviz_mclust_bic displays the components in the wrong order when the number of clusters for the mclust models is between single digit and lower double digit (e.g. 2-17). It shows the values for the components in the order 1, 10, 11, 12,...., 2,3,4,...) M1<- Mclust(iris[,-5], G=seq(2,13), verbose = T) fviz_mclust_bic(M1)
I've made a small modification (below in bold) on the function and it seems to work now. Please check
fviz_mclust_bic_01<- function (object, model.names = NULL, shape = 19, color = "model", palette = NULL, legend = NULL, main = "Model selection", xlab = "Number of components", ylab = "BIC", ...) { if (!inherits(object, "Mclust")) stop("An object of class Mclust is required.") best_model <- object$modelName number_of_cluster <- object$G x <- object$BIC n <- ncol(x) dnx <- dimnames(x) x <- matrix(as.vector(x), ncol = n) dimnames(x) <- dnx x <- as.data.frame(x) if (is.null(model.names)) model.names <- dimnames(x)[[2]] x <- x[, model.names, drop = FALSE] x <- cbind.data.frame(cluster = rownames(x), x) x <- tidyr::gather_(x, key_col = "model", value_col = "BIC", gather_cols = colnames(x)[-1]) x <- x[!is.na(x$BIC), , drop = FALSE] x$cluster<- factor(x$cluster, levels = dnx[[1]]) x$model <- factor(x$model, levels = dnx[[2]]) if (ggpubr:::.is_col_palette(palette)) palette <- ggpubr:::.get_pal(palette, k = length(model.names)) ggline.opts <- list(data = x, x = "cluster", y = "BIC", group = "model", color = color, shape = shape, palette = palette, main = main, xlab = xlab, ylab = ylab, ...) p <- do.call(ggpubr::ggline, ggline.opts) + labs(subtitle = paste0("Best model: ", best_model, " | Optimal clusters: n = ", number_of_cluster)) + geom_vline(xintercept = number_of_cluster, linetype = 2, color = "red") + theme(legend.title = element_blank()) if (missing(legend)) p + theme(legend.position = c(0.7, 0.2), legend.direction = "horizontal", legend.key.height = unit(0.5, "line")) + guides(color = guide_legend(nrow = 5, byrow = TRUE)) else p + theme(legend.position = legend) }
With this modification, it looks like the plot is now displayed in the right order.
fviz_mclust_bic_01(M1)
The text was updated successfully, but these errors were encountered:
The function fviz_mclust_bic displays the components in the wrong order when the number of clusters for the mclust models is between single digit and lower double digit (e.g. 2-17). It shows the values for the components in the order 1, 10, 11, 12,...., 2,3,4,...)
M1<- Mclust(iris[,-5], G=seq(2,13), verbose = T)
fviz_mclust_bic(M1)
I've made a small modification (below in bold) on the function and it seems to work now. Please check
fviz_mclust_bic_01<- function (object, model.names = NULL, shape = 19, color = "model", palette = NULL, legend = NULL, main = "Model selection", xlab = "Number of components", ylab = "BIC", ...)
{ if (!inherits(object, "Mclust")) stop("An object of class Mclust is required.")
best_model <- object$modelName
number_of_cluster <- object$G
x <- object$BIC
n <- ncol(x)
dnx <- dimnames(x)
x <- matrix(as.vector(x), ncol = n)
dimnames(x) <- dnx
x <- as.data.frame(x)
if (is.null(model.names)) model.names <- dimnames(x)[[2]]
x <- x[, model.names, drop = FALSE]
x <- cbind.data.frame(cluster = rownames(x), x)
x <- tidyr::gather_(x, key_col = "model", value_col = "BIC", gather_cols = colnames(x)[-1])
x <- x[!is.na(x$BIC), , drop = FALSE]
x$cluster<- factor(x$cluster, levels = dnx[[1]])
x$model <- factor(x$model, levels = dnx[[2]])
if (ggpubr:::.is_col_palette(palette)) palette <- ggpubr:::.get_pal(palette, k = length(model.names))
ggline.opts <- list(data = x, x = "cluster", y = "BIC", group = "model", color = color, shape = shape, palette = palette, main = main, xlab = xlab, ylab = ylab, ...)
p <- do.call(ggpubr::ggline, ggline.opts) + labs(subtitle = paste0("Best model: ", best_model, " | Optimal clusters: n = ", number_of_cluster)) + geom_vline(xintercept = number_of_cluster, linetype = 2, color = "red") + theme(legend.title = element_blank())
if (missing(legend)) p + theme(legend.position = c(0.7, 0.2), legend.direction = "horizontal", legend.key.height = unit(0.5, "line")) + guides(color = guide_legend(nrow = 5, byrow = TRUE)) else p + theme(legend.position = legend)
}
With this modification, it looks like the plot is now displayed in the right order.
fviz_mclust_bic_01(M1)
The text was updated successfully, but these errors were encountered: