Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(grid)
- r1 <- rectGrob(width = unit(0, "npc"), gp = gpar(col = "red", fill = "grey")) # zero-width
- r2 <- rectGrob(height = unit(0, "npc"), gp = gpar(col = "red", fill = "grey")) # zero-height
- grid.draw(r1) # depicted as a vertical line, rather than disappear completely
- grid.draw(r2) # depicted as a horizontal line, rather than disappear completely
- p <- ggplot(data, aes(x = value)) +
- geom_histogram(color = "red") +
- facet_wrap(~ key, ncol = 1)
- View(layer_data(p) %>% filter(PANEL == 2)) # look at the data associated with facet panel 2
- # modified version of StatBin2 inherits from StatBin, except for an
- # additional 2nd last line in compute_group() function
- StatBin2 <- ggproto(
- "StatBin2",
- StatBin,
- compute_group = function (data, scales, binwidth = NULL, bins = NULL,
- center = NULL, boundary = NULL,
- closed = c("right", "left"), pad = FALSE,
- breaks = NULL, origin = NULL, right = NULL,
- drop = NULL, width = NULL) {
- if (!is.null(breaks)) {
- if (!scales$x$is_discrete()) {
- breaks <- scales$x$transform(breaks)
- }
- bins <- ggplot2:::bin_breaks(breaks, closed)
- }
- else if (!is.null(binwidth)) {
- if (is.function(binwidth)) {
- binwidth <- binwidth(data$x)
- }
- bins <- ggplot2:::bin_breaks_width(scales$x$dimension(), binwidth,
- center = center, boundary = boundary,
- closed = closed)
- }
- else {
- bins <- ggplot2:::bin_breaks_bins(scales$x$dimension(), bins,
- center = center, boundary = boundary,
- closed = closed)
- }
- res <- ggplot2:::bin_vector(data$x, bins, weight = data$weight, pad = pad)
- # drop 0-count bins completely before returning the dataframe
- res <- res[res$count > 0, ]
- res
- })
- ggplot(data, aes(x = value)) +
- geom_histogram(color = "red", stat = StatBin2) + # specify stat = StatBin2
- facet_wrap(~ key, ncol = 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement