Skip to content

Commit 20fb4dd

Browse files
committed
Also add in density plots
1 parent 893be52 commit 20fb4dd

7 files changed

+17
-11
lines changed

R/commonMachineLearningClustering.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@
476476
table$setData(clusterMeans)
477477
}
478478

479-
.mlClusteringPlotDensities <- function(dataset, options, jaspResults, ready, position) {
479+
.mlClusteringPlotDensities <- function(dataset, options, jaspResults, ready, position, type) {
480480
if (!is.null(jaspResults[["clusterDensities"]]) || !options[["clusterDensityPlot"]]) {
481481
return()
482482
}
@@ -488,9 +488,15 @@
488488
return()
489489
}
490490
clusterResult <- jaspResults[["clusterResult"]]$object
491+
predictions <- clusterResult[["pred.values"]]
492+
ncolors <- clusterResult[["clusters"]]
493+
if (type == "densitybased") {
494+
ncolors <- ncolors + 1
495+
predictions[predictions == 0] <- gettext("Noisepoint")
496+
}
497+
clusters <- as.factor(predictions)
491498
if (!options[["clusterDensityPlotSingleFigure"]]) {
492499
for (variable in unlist(options[["predictors"]])) {
493-
clusters <- as.factor(clusterResult[["pred.values"]])
494500
xBreaks <- jaspGraphs::getPrettyAxisBreaks(dataset[[variable]], min.n = 4)
495501
plotData <- data.frame(
496502
cluster = clusters,
@@ -500,7 +506,7 @@
500506
ggplot2::geom_density(mapping = ggplot2::aes(fill = cluster), color = "black", alpha = 0.6) +
501507
ggplot2::scale_x_continuous(name = variable, breaks = xBreaks, limits = range(xBreaks)) +
502508
ggplot2::scale_y_continuous(name = gettext("Density")) +
503-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(levels(clusters)))) +
509+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
504510
jaspGraphs::geom_rangeframe() +
505511
jaspGraphs::themeJaspRaw(legend.position = "right") +
506512
ggplot2::theme(axis.ticks.y = ggplot2::element_blank(), axis.text.y = ggplot2::element_blank())
@@ -509,11 +515,11 @@
509515
}
510516
} else {
511517
dataList <- c(dataset[, options[["predictors"]]])
512-
plotData <- data.frame(value = unlist(dataList), variable = rep(options[["predictors"]], lengths(dataList)), cluster = rep(clusterResult[["pred.values"]], length(options[["predictors"]])))
518+
plotData <- data.frame(value = unlist(dataList), variable = rep(options[["predictors"]], lengths(dataList)), cluster = rep(predictions, length(options[["predictors"]])))
513519
xBreaks <- jaspGraphs::getPrettyAxisBreaks(plotData[["value"]])
514520
p <- ggplot2::ggplot(data = plotData, mapping = ggplot2::aes(x = value, y = factor(variable), height = ..density.., fill = factor(cluster))) +
515521
ggridges::geom_density_ridges(stat = "density", alpha = .6) +
516-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(unique(clusterResult[["pred.values"]])))) +
522+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
517523
ggplot2::scale_x_continuous(name = gettext("Value"), breaks = xBreaks, limits = range(xBreaks)) +
518524
ggplot2::scale_y_discrete(name = gettext("Feature")) +
519525
jaspGraphs::geom_rangeframe(sides = "b") +

R/mlClusteringDensityBased.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mlClusteringDensityBased <- function(jaspResults, dataset, options, ...) {
5252
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "densitybased")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "densitybased")
5656
}
5757

5858
.densityBasedClustering <- function(dataset, options, jaspResults) {

R/mlClusteringFuzzyCMeans.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mlClusteringFuzzyCMeans <- function(jaspResults, dataset, options, ...) {
5252
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "cmeans")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "cmeans")
5656
}
5757

5858
.cMeansClustering <- function(dataset, options, jaspResults, ready) {

R/mlClusteringHierarchical.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mlClusteringHierarchical <- function(jaspResults, dataset, options, ...) {
5252
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "hierarchical")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "hierarchical")
5656

5757
# Create dendrogram
5858
.mlClusteringHierarchicalDendogram(dataset, options, jaspResults, ready, position = 10)

R/mlClusteringKMeans.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mlClusteringKMeans <- function(jaspResults, dataset, options, ...) {
5252
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "kmeans")
5353

5454
# Create the cluster densities plot
55-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9, type = "kmeans")
5656
}
5757

5858
.kMeansClustering <- function(dataset, options, jaspResults, ready) {

R/mlClusteringModelBased.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mlClusteringModelBased <- function(jaspResults, dataset, options, ...) {
5555
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9, type = "modelbased")
5656

5757
# Create the cluster densities plot
58-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10)
58+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10, type = "modelbased")
5959
}
6060

6161
emControl <- mclust::emControl

R/mlClusteringRandomForest.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mlClusteringRandomForest <- function(jaspResults, dataset, options, ...) {
5555
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9, type = "randomForest")
5656

5757
# Create the cluster densities plot
58-
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10)
58+
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10, type = "randomForest")
5959
}
6060

6161
.randomForestClustering <- function(dataset, options, jaspResults) {

0 commit comments

Comments
 (0)