Created
January 22, 2020 22:50
-
-
Save johnbaums/028106a8b82dc27937ec9bb6de01da8e to your computer and use it in GitHub Desktop.
Fix axis limits for overlapping lattice histograms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://stackoverflow.com/q/59851541/489704 | |
fix_limits <- function(p) { | |
if(!is.numeric(p$panel.args.common$breaks)) | |
stop('trellis object should be constructed specifying nint') | |
b <- p$panel.args.common$breaks | |
pad <- diff(b[1:2]) | |
lims <- lapply(p$panel.args, function(x) { | |
bins <- findInterval(x$x, b) | |
ymax <- max(unlist(tapply(bins, p$panel.args.common$groups[x$subscripts], | |
function(y) table(y)/length(y)))) + 0.025 | |
ylims <- c(0, 100*ymax) | |
xlims <- b[range(bins)] + c(-3*pad, 4*pad) | |
list(xlims=xlims, ylims=ylims) | |
}) | |
p$x.limits <- lapply(lims, '[[', 'xlims') | |
p$y.limits <- lapply(lims, '[[', 'ylims') | |
p | |
} | |
But this doesn't work well when panels have wildly different ranges - the bins are calculated along the combined x-axis. Above, I would want the x-axis of each panel to be split into 100 bins.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example from https://stackoverflow.com/q/59851541/489704
fix_limits(p1)