Skip to content

Commit cfcf385

Browse files
committed
Same logic in cluster means plot
1 parent 35e0870 commit cfcf385

7 files changed

+16
-11
lines changed

R/commonMachineLearningClustering.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@
523523
}
524524
}
525525

526-
.mlClusteringPlotMeans <- function(dataset, options, jaspResults, ready, position) {
526+
.mlClusteringPlotMeans <- function(dataset, options, jaspResults, ready, position, type) {
527527
if (!is.null(jaspResults[["clusterMeans"]]) || !options[["clusterMeanPlot"]]) {
528528
return()
529529
}
@@ -536,8 +536,14 @@
536536
}
537537
clusterDataset <- data.frame(dataset[, options[["predictors"]], drop = FALSE])
538538
clusterResult <- jaspResults[["clusterResult"]]$object
539+
predictions <- clusterResult[["pred.values"]]
540+
ncolors <- clusterResult[["clusters"]]
541+
if (type == "densitybased") {
542+
ncolors <- ncolors + 1
543+
predictions[predictions == 0] <- gettext("Noisepoint")
544+
}
545+
clusters <- as.factor(predictions)
539546
if (options[["clusterMeanPlotSingleFigure"]]) {
540-
clusters <- as.factor(clusterResult[["pred.values"]])
541547
xBreaks <- c(1, (as.numeric(levels(clusters)) + 1) * length(options[["predictors"]]))
542548
clusterMeansData <- aggregate(clusterDataset, list(clusters), mean)
543549
clusterSdData <- aggregate(clusterDataset, list(clusters), sd)
@@ -578,14 +584,13 @@
578584
}
579585
p <- p + ggplot2::scale_x_continuous(name = NULL, breaks = xBreaks, labels = xLabels) +
580586
ggplot2::scale_y_continuous(name = gettext("Cluster Mean"), breaks = yBreaks, limits = range(yBreaks)) +
581-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(unique(clusterResult[["pred.values"]])))) +
587+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
582588
jaspGraphs::geom_rangeframe(sides = "l") +
583589
jaspGraphs::themeJaspRaw(legend.position = "right") +
584590
ggplot2::theme(axis.ticks.x = ggplot2::element_blank(), axis.text.x = ggplot2::element_text(angle = 20))
585591
plot[["oneFigure"]] <- createJaspPlot(plot = p, title = gettext("All Features"), height = 400, width = 200 * length(options[["predictors"]]))
586592
} else {
587593
for (variable in unlist(options[["predictors"]])) {
588-
clusters <- as.factor(clusterResult[["pred.values"]])
589594
xBreaks <- as.numeric(levels(clusters))
590595
clusterMeansData <- aggregate(clusterDataset[[variable]], list(clusters), mean)
591596
clusterSdData <- aggregate(clusterDataset[[variable]], list(clusters), sd)
@@ -608,7 +613,7 @@
608613
}
609614
p <- p + ggplot2::scale_x_discrete(name = gettext("Cluster"), breaks = xBreaks) +
610615
ggplot2::scale_y_continuous(name = variable, breaks = yBreaks, limits = range(yBreaks)) +
611-
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(length(unique(clusterResult[["pred.values"]])))) +
616+
ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) +
612617
jaspGraphs::geom_rangeframe(sides = "l") +
613618
jaspGraphs::themeJaspRaw() +
614619
ggplot2::theme(axis.ticks.x = ggplot2::element_blank())

R/mlClusteringDensityBased.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mlClusteringDensityBased <- function(jaspResults, dataset, options, ...) {
4949
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "densitybased")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "densitybased")
5353

5454
# Create the cluster densities plot
5555
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)

R/mlClusteringFuzzyCMeans.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mlClusteringFuzzyCMeans <- function(jaspResults, dataset, options, ...) {
4949
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "cmeans")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "cmeans")
5353

5454
# Create the cluster densities plot
5555
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)

R/mlClusteringHierarchical.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mlClusteringHierarchical <- function(jaspResults, dataset, options, ...) {
4949
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "hierarchical")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "hierarchical")
5353

5454
# Create the cluster densities plot
5555
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)

R/mlClusteringKMeans.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mlClusteringKMeans <- function(jaspResults, dataset, options, ...) {
4949
.mlClusteringMatrixPlot(dataset, options, jaspResults, ready, position = 7, type = "kmeans")
5050

5151
# Create the cluster means plot
52-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8)
52+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 8, type = "kmeans")
5353

5454
# Create the cluster densities plot
5555
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 9)

R/mlClusteringModelBased.R

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

5454
# Create the cluster means plot
55-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9, type = "modelbased")
5656

5757
# Create the cluster densities plot
5858
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10)

R/mlClusteringRandomForest.R

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

5454
# Create the cluster means plot
55-
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9)
55+
.mlClusteringPlotMeans(dataset, options, jaspResults, ready, position = 9, type = "randomForest")
5656

5757
# Create the cluster densities plot
5858
.mlClusteringPlotDensities(dataset, options, jaspResults, ready, position = 10)

0 commit comments

Comments
 (0)