Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update type_histogram.R #288

Merged
merged 6 commits into from
Feb 2, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions R/type_histogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type_hist = type_histogram
data_histogram = function(breaks = "Sturges") {
hbreaks = breaks
fun = function(by, facet, ylab, col, bg, ribbon.alpha, datapoints, .breaks = hbreaks, ...) {
hbreaks = ifelse(!is.null(.breaks), .breaks, "Sturges")
hbreaks = ifelse(!mapply(is.null, .breaks), .breaks, "Sturges")
grantmcdermott marked this conversation as resolved.
Show resolved Hide resolved

if (is.null(ylab)) ylab = "Frequency"
if (is.null(by) && is.null(palette)) {
Expand All @@ -49,12 +49,11 @@ data_histogram = function(breaks = "Sturges") {
if (is.null(bg)) bg = ribbon.alpha
}

datapoints_breaks = hist(datapoints$x, breaks = hbreaks, plot = FALSE)
datapoints = split(datapoints, list(datapoints$by, datapoints$facet))
datapoints = Filter(function(k) nrow(k) > 0, datapoints)

datapoints = lapply(datapoints, function(k) {
h = hist(k$x, breaks = datapoints_breaks$breaks, plot = FALSE)
h = hist(k$x, breaks = hbreaks, plot = FALSE)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can delete the datapoints_breaks object, since we we should still use it by default in the case of grouped histograms. The thing we want to avoid is different binning widths for each group (unless the user explicitly requests it) which is why we calculate it on the full dataset first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, but atm there is no possibility of different binning for each group/facet since datapoint_breaks is always passed to breaks, and there is no possibility of passing a vector of breaks because of ifelse(). it would be nice if breaks accepted the same possibilities as hist (function, vector, number).

i think that different binning by group/facet often makes sense when freq=FALSE (missing atm).

thanks for looking into this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps introducing an option to allow for free breaks (as in this commit) is an idea...

out = data.frame(
by = k$by[1], # already split
facet = k$facet[1], # already split
Expand Down
Loading