|
476 | 476 | table$setData(clusterMeans)
|
477 | 477 | }
|
478 | 478 |
|
479 |
| -.mlClusteringPlotDensities <- function(dataset, options, jaspResults, ready, position) { |
| 479 | +.mlClusteringPlotDensities <- function(dataset, options, jaspResults, ready, position, type) { |
480 | 480 | if (!is.null(jaspResults[["clusterDensities"]]) || !options[["clusterDensityPlot"]]) {
|
481 | 481 | return()
|
482 | 482 | }
|
|
488 | 488 | return()
|
489 | 489 | }
|
490 | 490 | 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) |
491 | 498 | if (!options[["clusterDensityPlotSingleFigure"]]) {
|
492 | 499 | for (variable in unlist(options[["predictors"]])) {
|
493 |
| - clusters <- as.factor(clusterResult[["pred.values"]]) |
494 | 500 | xBreaks <- jaspGraphs::getPrettyAxisBreaks(dataset[[variable]], min.n = 4)
|
495 | 501 | plotData <- data.frame(
|
496 | 502 | cluster = clusters,
|
|
500 | 506 | ggplot2::geom_density(mapping = ggplot2::aes(fill = cluster), color = "black", alpha = 0.6) +
|
501 | 507 | ggplot2::scale_x_continuous(name = variable, breaks = xBreaks, limits = range(xBreaks)) +
|
502 | 508 | 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)) + |
504 | 510 | jaspGraphs::geom_rangeframe() +
|
505 | 511 | jaspGraphs::themeJaspRaw(legend.position = "right") +
|
506 | 512 | ggplot2::theme(axis.ticks.y = ggplot2::element_blank(), axis.text.y = ggplot2::element_blank())
|
|
509 | 515 | }
|
510 | 516 | } else {
|
511 | 517 | 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"]]))) |
513 | 519 | xBreaks <- jaspGraphs::getPrettyAxisBreaks(plotData[["value"]])
|
514 | 520 | p <- ggplot2::ggplot(data = plotData, mapping = ggplot2::aes(x = value, y = factor(variable), height = ..density.., fill = factor(cluster))) +
|
515 | 521 | 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)) + |
517 | 523 | ggplot2::scale_x_continuous(name = gettext("Value"), breaks = xBreaks, limits = range(xBreaks)) +
|
518 | 524 | ggplot2::scale_y_discrete(name = gettext("Feature")) +
|
519 | 525 | jaspGraphs::geom_rangeframe(sides = "b") +
|
|
523 | 529 | }
|
524 | 530 | }
|
525 | 531 |
|
526 |
| -.mlClusteringPlotMeans <- function(dataset, options, jaspResults, ready, position) { |
| 532 | +.mlClusteringPlotMeans <- function(dataset, options, jaspResults, ready, position, type) { |
527 | 533 | if (!is.null(jaspResults[["clusterMeans"]]) || !options[["clusterMeanPlot"]]) {
|
528 | 534 | return()
|
529 | 535 | }
|
|
536 | 542 | }
|
537 | 543 | clusterDataset <- data.frame(dataset[, options[["predictors"]], drop = FALSE])
|
538 | 544 | clusterResult <- jaspResults[["clusterResult"]]$object
|
| 545 | + predictions <- clusterResult[["pred.values"]] |
| 546 | + ncolors <- clusterResult[["clusters"]] |
| 547 | + if (type == "densitybased") { |
| 548 | + ncolors <- ncolors + 1 |
| 549 | + predictions[predictions == 0] <- gettext("Noisepoint") |
| 550 | + } |
| 551 | + clusters <- as.factor(predictions) |
539 | 552 | if (options[["clusterMeanPlotSingleFigure"]]) {
|
540 |
| - clusters <- as.factor(clusterResult[["pred.values"]]) |
541 | 553 | xBreaks <- c(1, (as.numeric(levels(clusters)) + 1) * length(options[["predictors"]]))
|
542 | 554 | clusterMeansData <- aggregate(clusterDataset, list(clusters), mean)
|
543 | 555 | clusterSdData <- aggregate(clusterDataset, list(clusters), sd)
|
|
578 | 590 | }
|
579 | 591 | p <- p + ggplot2::scale_x_continuous(name = NULL, breaks = xBreaks, labels = xLabels) +
|
580 | 592 | 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"]])))) + |
| 593 | + ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) + |
582 | 594 | jaspGraphs::geom_rangeframe(sides = "l") +
|
583 | 595 | jaspGraphs::themeJaspRaw(legend.position = "right") +
|
584 | 596 | ggplot2::theme(axis.ticks.x = ggplot2::element_blank(), axis.text.x = ggplot2::element_text(angle = 20))
|
585 | 597 | plot[["oneFigure"]] <- createJaspPlot(plot = p, title = gettext("All Features"), height = 400, width = 200 * length(options[["predictors"]]))
|
586 | 598 | } else {
|
587 | 599 | for (variable in unlist(options[["predictors"]])) {
|
588 |
| - clusters <- as.factor(clusterResult[["pred.values"]]) |
589 | 600 | xBreaks <- as.numeric(levels(clusters))
|
590 | 601 | clusterMeansData <- aggregate(clusterDataset[[variable]], list(clusters), mean)
|
591 | 602 | clusterSdData <- aggregate(clusterDataset[[variable]], list(clusters), sd)
|
|
608 | 619 | }
|
609 | 620 | p <- p + ggplot2::scale_x_discrete(name = gettext("Cluster"), breaks = xBreaks) +
|
610 | 621 | 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"]])))) + |
| 622 | + ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) + |
612 | 623 | jaspGraphs::geom_rangeframe(sides = "l") +
|
613 | 624 | jaspGraphs::themeJaspRaw() +
|
614 | 625 | ggplot2::theme(axis.ticks.x = ggplot2::element_blank())
|
|
634 | 645 | unlist(regmatches(p[[1]], gregexpr("[[:digit:]]+\\.*[[:digit:]]*", p[[1]])))
|
635 | 646 | }
|
636 | 647 |
|
637 |
| -.mlClusteringMatrixPlot <- function(dataset, options, jaspResults, ready, position) { |
| 648 | +.mlClusteringMatrixPlot <- function(dataset, options, jaspResults, ready, position, type) { |
638 | 649 | if (!is.null(jaspResults[["matrixPlot"]]) || !options[["matrixPlot"]]) {
|
639 | 650 | return()
|
640 | 651 | }
|
|
667 | 678 | oldFontSize <- jaspGraphs::getGraphOption("fontsize")
|
668 | 679 | jaspGraphs::setGraphOption("fontsize", .85 * oldFontSize)
|
669 | 680 | startProgressbar(length(plotMat) + 1)
|
| 681 | + ncolors <- clusterResult[["clusters"]] |
| 682 | + predictions <- clusterResult[["pred.values"]] |
| 683 | + if (type == "densitybased") { |
| 684 | + ncolors <- ncolors + 1 |
| 685 | + predictions[predictions == 0] <- gettext("Noisepoint") |
| 686 | + } |
670 | 687 | for (row in 2:l) {
|
671 | 688 | for (col in 1:(l - 1)) {
|
672 | 689 | if (col < row) {
|
673 | 690 | predictors <- dataset[, variables]
|
674 | 691 | predictors <- predictors[, c(col, row)]
|
675 |
| - plotData <- data.frame(x = predictors[, 1], y = predictors[, 2], cluster = as.factor(clusterResult[["pred.values"]])) |
| 692 | + plotData <- data.frame(x = predictors[, 1], y = predictors[, 2], cluster = as.factor(predictions)) |
676 | 693 | xBreaks <- jaspGraphs::getPrettyAxisBreaks(plotData$x, min.n = 4)
|
677 | 694 | yBreaks <- jaspGraphs::getPrettyAxisBreaks(plotData$y, min.n = 4)
|
678 | 695 | p <- ggplot2::ggplot(data = plotData, mapping = ggplot2::aes(x = x, y = y, fill = cluster)) +
|
679 | 696 | jaspGraphs::geom_point() +
|
680 |
| - ggplot2::scale_fill_manual(name = NULL, values = .mlColorScheme(clusterResult[["clusters"]])) + |
| 697 | + ggplot2::scale_fill_manual(name = NULL, values = .mlColorScheme(ncolors)) + |
681 | 698 | ggplot2::scale_x_continuous(name = NULL, breaks = xBreaks, limits = range(xBreaks)) +
|
682 | 699 | ggplot2::scale_y_continuous(name = NULL, breaks = yBreaks, limits = range(yBreaks)) +
|
683 | 700 | jaspGraphs::geom_rangeframe() +
|
|
704 | 721 | y <- sqrt(lambda1) * sin(theta) * cos(t) + sqrt(lambda2) * cos(theta) * sin(t) + mu_y
|
705 | 722 | ellips <- data.frame(x = x, y = y)
|
706 | 723 | p <- p + ggplot2::geom_path(data = ellips, mapping = ggplot2::aes(x = x, y = y), color = "black", inherit.aes = FALSE, linewidth = 1.5) +
|
707 |
| - ggplot2::geom_path(data = ellips, mapping = ggplot2::aes(x = x, y = y), color = .mlColorScheme(clusterResult[["clusters"]])[i], inherit.aes = FALSE, linewidth = 0.75) |
| 724 | + ggplot2::geom_path(data = ellips, mapping = ggplot2::aes(x = x, y = y), color = .mlColorScheme(ncolors)[i], inherit.aes = FALSE, linewidth = 0.75) |
708 | 725 | }
|
709 | 726 | }
|
710 | 727 |
|
711 | 728 | plotMat[[row - 1, col]] <- p
|
712 | 729 | }
|
713 | 730 | if (l > 2) {
|
714 | 731 | predictors <- dataset[, options[["predictors"]]]
|
715 |
| - plotData <- data.frame(cluster = as.factor(clusterResult[["pred.values"]]), predictor = predictors[, 1]) |
| 732 | + plotData <- data.frame(cluster = as.factor(predictions), predictor = predictors[, 1]) |
716 | 733 | p <- ggplot2::ggplot(plotData, ggplot2::aes(y = cluster, x = cluster, show.legend = TRUE)) +
|
717 | 734 | jaspGraphs::geom_point(ggplot2::aes(fill = cluster), alpha = 0) +
|
718 | 735 | ggplot2::xlab(NULL) +
|
719 | 736 | ggplot2::ylab(NULL) +
|
720 | 737 | ggplot2::theme(legend.key = ggplot2::element_blank()) +
|
721 |
| - ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(clusterResult[["clusters"]])) + |
| 738 | + ggplot2::scale_fill_manual(name = gettext("Cluster"), values = .mlColorScheme(ncolors)) + |
722 | 739 | jaspGraphs::geom_rangeframe(sides = "") +
|
723 | 740 | jaspGraphs::themeJaspRaw(legend.position = "left") +
|
724 | 741 | ggplot2::theme(axis.ticks = ggplot2::element_blank(), axis.text.x = ggplot2::element_blank(), axis.text.y = ggplot2::element_blank()) +
|
|
0 commit comments