Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################################################################################
- ### Experiment 4: Patch the bins function
- library("ggplot2")
- bins <- function (breaks, closed = c("right", "left"), fuzz = 1e-08 *
- stats::median(diff(breaks)))
- {
- stopifnot(is.numeric(breaks))
- closed <- match.arg(closed)
- breaks <- sort(breaks)
- if (closed == "right") {
- fuzzes <- c(-fuzz, rep.int(fuzz, length(breaks) - 1))
- }
- else {
- fuzzes <- c(rep.int(-fuzz, length(breaks) - 1), fuzz)
- }
- ## PATCH
- print(breaks)
- ## [1] -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65
- ## [9] -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25
- ## [17] -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15
- ## [25] 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55
- ## [33] 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95
- ## [41] 1.00
- fuzzy = breaks + fuzzes
- print(fuzzy)
- ## [1] -1.0e+00 -9.5e-01 -9.0e-01 -8.5e-01 -8.0e-01
- ## [6] -7.5e-01 -7.0e-01 -6.5e-01 -6.0e-01 -5.5e-01
- ## [11] -5.0e-01 -4.5e-01 -4.0e-01 -3.5e-01 -3.0e-01
- ## [16] -2.5e-01 -2.0e-01 -1.5e-01 -1.0e-01 -5.0e-02
- ## [21] -5.0e-10 5.0e-02 1.0e-01 1.5e-01 2.0e-01
- ## [26] 2.5e-01 3.0e-01 3.5e-01 4.0e-01 4.5e-01
- ## [31] 5.0e-01 5.5e-01 6.0e-01 6.5e-01 7.0e-01
- ## [36] 7.5e-01 8.0e-01 8.5e-01 9.0e-01 9.5e-01
- ## [41] 1.0e+00
- structure(list(breaks = breaks, fuzzy = breaks + fuzzes,
- right_closed = closed == "right"), class = "ggplot2_bins")
- ## THIS IS THE PATCHED FUNCTION
- }
- assignInNamespace("bins", bins, ns = "ggplot2")
- # ggplot2:::bins # check
- df <- data.frame(var = seq(-100,100,10)/100)
- p <- ggplot(data = df, aes(x = var)) + geom_histogram(aes(y = ..count..), binwidth = 0.05, boundary = 1, closed = "left")
- p
- ################################################################################
- ### Experiment 5: Patch the bin_vector function
- library("ggplot2")
- bin_vector <- function(x, bins, weight = NULL, pad = FALSE) {
- stopifnot(ggplot2:::is_bins(bins))
- if (all(is.na(x))) {
- return(ggplot2:::bin_out(length(x), NA, NA, xmin = NA, xmax = NA))
- }
- if (is.null(weight)) {
- weight <- rep(1, length(x))
- } else {
- weight[is.na(weight)] <- 0
- }
- ## PATCH
- print("weight")
- print(weight)
- ## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- ## PATCH
- print("str(bins)")
- print(str(bins))
- ## List of 3
- ## $ breaks : num [1:41] -1 -0.95 -0.9 -0.85 -0.8 -0.75 -0.7 -0.65 -0.6 -0.55 ...
- ## $ fuzzy : num [1:41] -1 -0.95 -0.9 -0.85 -0.8 ...
- ## $ right_closed: logi FALSE
- ## - attr(*, "class")= chr "ggplot2_bins"
- ## NULL
- ## PATCH
- print("bins$fuzzy")
- print(bins$fuzzy)
- ## [1] "bins$fuzzy"
- ## [1] -1.0e+00 -9.5e-01 -9.0e-01 -8.5e-01 -8.0e-01
- ## [6] -7.5e-01 -7.0e-01 -6.5e-01 -6.0e-01 -5.5e-01
- ## [11] -5.0e-01 -4.5e-01 -4.0e-01 -3.5e-01 -3.0e-01
- ## [16] -2.5e-01 -2.0e-01 -1.5e-01 -1.0e-01 -5.0e-02
- ## [21] -5.0e-10 5.0e-02 1.0e-01 1.5e-01 2.0e-01
- ## [26] 2.5e-01 3.0e-01 3.5e-01 4.0e-01 4.5e-01
- ## [31] 5.0e-01 5.5e-01 6.0e-01 6.5e-01 7.0e-01
- ## [36] 7.5e-01 8.0e-01 8.5e-01 9.0e-01 9.5e-01
- ## [41] 1.0e+00
- ## PATCH
- print("bins$breaks")
- print(bins$breaks)
- ## [1] -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65
- ## [9] -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25
- ## [17] -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15
- ## [25] 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55
- ## [33] 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95
- ## [41] 1.00
- ## PATCH
- print("bins$right_closed")
- print(bins$right_closed)
- ## [1] FALSE
- # bin_idx <- cut(x, bins$breaks, right = bins$right_closed,
- # include.lowest = TRUE)
- ## PATCH
- ## Replace bins$breaks with bins$fuzzy
- bin_idx <- cut(x, bins$fuzzy, right = bins$right_closed,
- include.lowest = TRUE)
- ## PATCH
- print("dput(bin_idx)")
- print(dput(bin_idx)) ## should I see fuzz here?
- ## bin_idx <- structure(c(1L, 3L, 5L, 7L, 9L, 11L, 12L, 14L, 16L, 18L, 21L,
- ## 22L, 24L, 26L, 28L, 31L, 32L, 34L, 37L, 38L, 40L), .Label = c("[-1,-0.95)",
- ## "[-0.95,-0.9)", "[-0.9,-0.85)", "[-0.85,-0.8)", "[-0.8,-0.75)",
- ## "[-0.75,-0.7)", "[-0.7,-0.65)", "[-0.65,-0.6)", "[-0.6,-0.55)",
- ## "[-0.55,-0.5)", "[-0.5,-0.45)", "[-0.45,-0.4)", "[-0.4,-0.35)",
- ## "[-0.35,-0.3)", "[-0.3,-0.25)", "[-0.25,-0.2)", "[-0.2,-0.15)",
- ## "[-0.15,-0.1)", "[-0.1,-0.05)", "[-0.05,0)", "[0,0.05)", "[0.05,0.1)",
- ## "[0.1,0.15)", "[0.15,0.2)", "[0.2,0.25)", "[0.25,0.3)", "[0.3,0.35)",
- ## "[0.35,0.4)", "[0.4,0.45)", "[0.45,0.5)", "[0.5,0.55)", "[0.55,0.6)",
- ## "[0.6,0.65)", "[0.65,0.7)", "[0.7,0.75)", "[0.75,0.8)", "[0.8,0.85)",
- ## "[0.85,0.9)", "[0.9,0.95)", "[0.95,1]"), class = "factor")
- bin_count <- as.numeric(tapply(weight, bin_idx, sum, na.rm = TRUE))
- bin_count[is.na(bin_count)] <- 0
- ## PATCH
- print("bin_count")
- print(bin_count) ## The bin_count is incorrect
- ## [1] 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 0 1
- ## [25] 0 1 0 1 0 0 1 1 0 1 0 0 1 1 0 1
- bin_x <- (bins$breaks[-length(bins$breaks)] + bins$breaks[-1]) / 2
- bin_widths <- diff(bins$breaks)
- # Pad row of 0s at start and end
- if (pad) {
- bin_count <- c(0, bin_count, 0)
- width1 <- bin_widths[1]
- widthn <- bin_widths[length(bin_widths)]
- bin_widths <- c(width1, bin_widths, widthn)
- bin_x <- c(bin_x[1] - width1, bin_x, bin_x[length(bin_x)] + widthn)
- }
- # Add row for missings
- if (any(is.na(bins))) {
- bin_count <- c(bin_count, sum(is.na(bins)))
- bin_widths <- c(bin_widths, NA)
- bin_x <- c(bin_x, NA)
- }
- ggplot2:::bin_out(bin_count, bin_x, bin_widths)
- ## THIS IS THE PATCHED FUNCTION
- }
- assignInNamespace("bin_vector", bin_vector, ns = "ggplot2")
- df <- data.frame(var = seq(-100,100,10)/100)
- p <- ggplot(data = df, aes(x = var)) + geom_histogram(aes(y = ..count..), binwidth = 0.05, boundary = 1, closed = "left")
- p
- ################################################################################
- ### Experiment 6: Patch the bin_breaks function
- library("ggplot2")
- bin_breaks <- function(breaks, closed = c("right", "left")) {
- ## PATCH
- print(breaks)
- ## [1] -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65
- ## [9] -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25
- ## [17] -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15
- ## [25] 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55
- ## [33] 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95
- ## [41] 1.00
- ggplot2:::bins(breaks, closed)
- ## THIS IS THE PATCHED FUNCTION
- }
- assignInNamespace("bin_breaks", bin_breaks, ns = "ggplot2")
- df <- data.frame(var = seq(-100,100,10)/100)
- p <- ggplot(data = df, aes(x = var)) + geom_histogram(aes(y = ..count..), binwidth = 0.05, boundary = 1, closed = "left")
- p
- ################################################################################
- ### Experiment 7: Patch the StatBin ggproto object
- library("ggplot2")
- StatBin <- ggproto("StatBin", Stat,
- setup_params = function(data, params) {
- if (!is.null(data$y) || !is.null(params$y)) {
- stop("stat_bin() must not be used with a y aesthetic.", call. = FALSE)
- }
- if (is.integer(data$x)) {
- stop('StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?',
- call. = FALSE)
- }
- if (!is.null(params$drop)) {
- warning("`drop` is deprecated. Please use `pad` instead.", call. = FALSE)
- params$drop <- NULL
- }
- if (!is.null(params$origin)) {
- warning("`origin` is deprecated. Please use `boundary` instead.", call. = FALSE)
- params$boundary <- params$origin
- params$origin <- NULL
- }
- if (!is.null(params$right)) {
- warning("`right` is deprecated. Please use `closed` instead.", call. = FALSE)
- params$closed <- if (params$right) "right" else "left"
- params$right <- NULL
- }
- if (!is.null(params$width)) {
- stop("`width` is deprecated. Do you want `geom_bar()`?", call. = FALSE)
- }
- if (!is.null(params$boundary) && !is.null(params$center)) {
- stop("Only one of `boundary` and `center` may be specified.", call. = FALSE)
- }
- if (is.null(params$breaks) && is.null(params$binwidth) && is.null(params$bins)) {
- message_wrap("`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.")
- params$bins <- 30
- }
- params
- },
- compute_group = function(data, scales, binwidth = NULL, bins = NULL,
- center = NULL, boundary = NULL,
- closed = c("right", "left"), pad = FALSE,
- # The following arguments are not used, but must
- # be listed so parameters are computed correctly
- breaks = NULL, origin = NULL, right = NULL,
- drop = NULL, width = NULL) {
- ## PATCH
- print(str(bins))
- ## NULL
- if (!is.null(breaks)) {
- bins <- ggplot2:::bin_breaks(breaks, closed)
- ## PATCH
- print("loop level 1")
- print(str(bins))
- } else if (!is.null(binwidth)) {
- bins <- ggplot2:::bin_breaks_width(scales$x$dimension(), binwidth, center = center,
- boundary = boundary, closed = closed)
- ## PATCH
- print("loop level 2")
- print(str(bins))
- ## [1] "loop level 2"
- ## List of 3
- ## $ breaks : num [1:41] -1 -0.95 -0.9 -0.85 -0.8 -0.75 -0.7 -0.65 -0.6 -0.55 ...
- ## $ fuzzy : num [1:41] -1 -0.95 -0.9 -0.85 -0.8 ...
- ## $ right_closed: logi FALSE
- ## - attr(*, "class")= chr "ggplot2_bins"
- print("bins$fuzzy")
- print(bins$fuzzy)
- ## [1] -1.0e+00 -9.5e-01 -9.0e-01 -8.5e-01 -8.0e-01
- ## [6] -7.5e-01 -7.0e-01 -6.5e-01 -6.0e-01 -5.5e-01
- ## [11] -5.0e-01 -4.5e-01 -4.0e-01 -3.5e-01 -3.0e-01
- ## [16] -2.5e-01 -2.0e-01 -1.5e-01 -1.0e-01 -5.0e-02
- ## [21] -5.0e-10 5.0e-02 1.0e-01 1.5e-01 2.0e-01
- ## [26] 2.5e-01 3.0e-01 3.5e-01 4.0e-01 4.5e-01
- ## [31] 5.0e-01 5.5e-01 6.0e-01 6.5e-01 7.0e-01
- ## [36] 7.5e-01 8.0e-01 8.5e-01 9.0e-01 9.5e-01
- ## [41] 1.0e+00
- } else {
- bins <- ggplot2:::bin_breaks_bins(scales$x$dimension(), bins, center = center,
- boundary = boundary, closed = closed)
- ## PATCH
- print("loop level 3")
- print(str(bins))
- }
- ggplot2:::bin_vector(data$x, bins, weight = data$weight, pad = pad)
- },
- default_aes = ggplot2:::aes(y = ..count..),
- required_aes = c("x")
- )
- assignInNamespace("StatBin", StatBin, ns = "ggplot2")
- df <- data.frame(var = seq(-100,100,10)/100)
- p <- ggplot(data = df, aes(x = var)) + geom_histogram(aes(y = ..count..), binwidth = 0.05, boundary = 1, closed = "left")
- p
- ################################################################################
- ### Experiment 8: Patch the bin_breaks_width function
- library("ggplot2")
- bin_breaks_width <- function(x_range, width = NULL, center = NULL,
- boundary = NULL, closed = c("right", "left")) {
- stopifnot(length(x_range) == 2)
- # if (length(x_range) == 0) {
- # return(bin_params(numeric()))
- # }
- stopifnot(is.numeric(width), length(width) == 1)
- if (width <= 0) {
- stop("`binwidth` must be positive", call. = FALSE)
- }
- if (!is.null(boundary) && !is.null(center)) {
- stop("Only one of 'boundary' and 'center' may be specified.")
- } else if (is.null(boundary)) {
- if (is.null(center)) {
- # If neither edge nor center given, compute both using tile layer's
- # algorithm. This puts min and max of data in outer half of their bins.
- boundary <- width / 2
- } else {
- # If center given but not boundary, compute boundary.
- boundary <- center - width / 2
- }
- }
- # Find the left side of left-most bin: inputs could be Dates or POSIXct, so
- # coerce to numeric first.
- x_range <- as.numeric(x_range)
- width <- as.numeric(width)
- boundary <- as.numeric(boundary)
- shift <- floor((x_range[1] - boundary) / width)
- origin <- boundary + shift * width
- # Small correction factor so that we don't get an extra bin when, for
- # example, origin = 0, max(x) = 20, width = 10.
- max_x <- x_range[2] + (1 - 1e-08) * width
- breaks <- seq(origin, max_x, width)
- ## PATCH
- print(max_x)
- ## [1] 1.05
- ## PATCH
- print(breaks)
- ## [1] -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65
- ## [9] -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25
- ## [17] -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15
- ## [25] 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55
- ## [33] 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95
- ## [41] 1.00
- ggplot2:::bin_breaks(breaks, closed = closed)
- ## THIS IS THE PATCHED FUNCTION
- }
- assignInNamespace("bin_breaks_width", bin_breaks_width, ns = "ggplot2")
- df <- data.frame(var = seq(-100,100,10)/100)
- p <- ggplot(data = df, aes(x = var)) + geom_histogram(aes(y = ..count..), binwidth = 0.05, boundary = 1, closed = "left")
- p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement