From e56a0edd834411593429b50df2b94ba2d22c8c42 Mon Sep 17 00:00:00 2001 From: Don van den Bergh Date: Tue, 3 Dec 2024 22:20:40 +0100 Subject: [PATCH] fix issue with stacked histogram --- R/jaspHistogram.R | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/R/jaspHistogram.R b/R/jaspHistogram.R index de54618..ef6b3df 100644 --- a/R/jaspHistogram.R +++ b/R/jaspHistogram.R @@ -111,13 +111,19 @@ jaspHistogram <- function( position = histogramPosition ) - # for each groupingvariable, bin by breaks and find the largest count - temp <- do.call(rbind, tapply(x, groupingVariable, function(subset) { - h <- graphics::hist(subset, plot = FALSE, breaks = binWidthType) - c(counts = max(h[["counts"]]), density = max(h[["density"]])) - })) - maxCounts <- max(temp[, "counts"]) - maxDensity <- max(temp[, "density"]) + if (identical(histogramPosition, "stack") || inherits(histogramPosition, "PositionStack")) { + # for a stacked figure we base maxCounts and maxDensity on the ungrouped data + maxCounts <- h[["counts"]] + maxDensity <- h[["density"]] + } else { + # for each groupingvariable, bin by breaks and find the largest count + temp <- do.call(rbind, tapply(x, groupingVariable, function(subset) { + h <- graphics::hist(subset, plot = FALSE, breaks = binWidthType) + c(counts = max(h[["counts"]]), density = max(h[["density"]])) + })) + maxCounts <- max(temp[, "counts"]) + maxDensity <- max(temp[, "density"]) + } } else { dataHistogram <- data.frame(x = x)