From 5cf881cd219e24cc6bc100a87a3be7021dd1c7fb Mon Sep 17 00:00:00 2001 From: Edwin Leuven Date: Thu, 23 Jan 2025 14:36:32 +0100 Subject: [PATCH 1/2] add freq option to histogram --- R/tinyplot.R | 16 ++++++++++++++-- R/type_histogram.R | 13 +++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/R/tinyplot.R b/R/tinyplot.R index ab1b4640..7863e903 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -725,8 +725,15 @@ tinyplot.default = function( y = rep(NA, length(x)) } else if (type == "density") { if (is.null(ylab)) ylab = "Density" - } else if (type %in% c("histogram", "function")) { + } else if (type == "function") { if (is.null(ylab)) ylab = "Frequency" + } else if (type == "histogram") { + if (is.null(ylab)) { + mc = calls[[idx[1]]] + if (is.character(mc$type)) freq = mc$freq else freq = mc$type$freq + freq = ifelse(is.null(freq), TRUE, eval(freq)) + ylab = ifelse(freq, "Frequency", "Density") + } } else { y = x x = seq_along(x) @@ -1359,7 +1366,12 @@ tinyplot.formula = function( if (is.null(ylab)) ylab = "Density" if (is.null(xlab)) xlab = xnam } else if (!is.null(type) && hist_type) { - if (is.null(ylab)) ylab = "Frequency" + if (is.null(ylab)) { + mc = match.call() + if (is.character(type)) freq = mc$freq else freq = mc$type$freq + freq = ifelse(is.null(freq), TRUE, eval(freq)) + ylab = ifelse(freq, "Frequency", "Density") + } if (is.null(xlab)) xlab = xnam } else if (is.null(y)) { if (is.null(ylab)) ylab = xnam diff --git a/R/type_histogram.R b/R/type_histogram.R index 24678b76..18d139d7 100644 --- a/R/type_histogram.R +++ b/R/type_histogram.R @@ -21,9 +21,9 @@ #' # Use `type_histogram()` to pass extra arguments for customization #' tinyplot(Nile, type = type_histogram(breaks = 30)) #' @export -type_histogram = function(breaks = "Sturges") { +type_histogram = function(breaks = "Sturges", freq = TRUE) { out = list( - data = data_histogram(breaks = breaks), + data = data_histogram(breaks = breaks, freq = freq), draw = draw_rect(), name = "histogram" ) @@ -36,12 +36,13 @@ type_histogram = function(breaks = "Sturges") { type_hist = type_histogram -data_histogram = function(breaks = "Sturges") { +data_histogram = function(breaks = "Sturges", freq = TRUE) { hbreaks = breaks - fun = function(by, facet, ylab, col, bg, ribbon.alpha, datapoints, .breaks = hbreaks, ...) { + hfreq = freq + fun = function(by, facet, ylab, col, bg, ribbon.alpha, datapoints, .breaks = hbreaks, .freq = hfreq,...) { hbreaks = ifelse(!is.null(.breaks), .breaks, "Sturges") - if (is.null(ylab)) ylab = "Frequency" + if (is.null(ylab)) ylab = ifelse(.freq, "Frequency", "Density") if (is.null(by) && is.null(palette)) { if (is.null(col)) col = par("fg") if (is.null(bg)) bg = "lightgray" @@ -59,7 +60,7 @@ data_histogram = function(breaks = "Sturges") { by = k$by[1], # already split facet = k$facet[1], # already split ymin = 0, - ymax = h$counts, + ymax = if (.freq) h$counts else h$density, xmin = h$breaks[-1], xmax = h$mids + (h$mids - h$breaks[-1]) ) From 327d03a8cc985756dd9e416df8cf018462024fd9 Mon Sep 17 00:00:00 2001 From: Edwin Leuven Date: Sun, 26 Jan 2025 13:45:55 +0100 Subject: [PATCH 2/2] add help description --- R/type_histogram.R | 7 ++++++- tinyplot.Rproj | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/R/type_histogram.R b/R/type_histogram.R index 18d139d7..0f81c5b6 100644 --- a/R/type_histogram.R +++ b/R/type_histogram.R @@ -14,12 +14,17 @@ #' it was larger). If breaks is a function, the x vector is supplied to it as #' the only argument (and the number of breaks is only limited by the amount of #' available memory). +#' @param freq Passed to \code{\link[graphics]{hist}}. logical; if TRUE, the +#' histogram graphic is a representation of frequencies, the counts component +#' of the result; if FALSE, probability densities, component density, are plotted +#' (so that the histogram has a total area of one). Defaults to TRUE if and only +#' if breaks are equidistant. #' @examples #' # "histogram"/"hist" type convenience string(s) #' tinyplot(Nile, type = "histogram") #' #' # Use `type_histogram()` to pass extra arguments for customization -#' tinyplot(Nile, type = type_histogram(breaks = 30)) +#' tinyplot(Nile, type = type_histogram(breaks = 30, freq = FALSE)) #' @export type_histogram = function(breaks = "Sturges", freq = TRUE) { out = list( diff --git a/tinyplot.Rproj b/tinyplot.Rproj index dead6016..1b45193b 100644 --- a/tinyplot.Rproj +++ b/tinyplot.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 8cea7a09-6164-4787-92ac-d2570520bcee RestoreWorkspace: Default SaveWorkspace: Default