SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(grid)
  2.  
  3. r1 <- rectGrob(width = unit(0, "npc"), gp = gpar(col = "red", fill = "grey")) # zero-width
  4. r2 <- rectGrob(height = unit(0, "npc"), gp = gpar(col = "red", fill = "grey")) # zero-height
  5.  
  6. grid.draw(r1) # depicted as a vertical line, rather than disappear completely
  7. grid.draw(r2) # depicted as a horizontal line, rather than disappear completely
  8.      
  9. p <- ggplot(data, aes(x = value)) +
  10.   geom_histogram(color = "red") +
  11.   facet_wrap(~ key, ncol = 1)
  12.  
  13. View(layer_data(p) %>% filter(PANEL == 2)) # look at the data associated with facet panel 2
  14.      
  15. # modified version of StatBin2 inherits from StatBin, except for an
  16. # additional 2nd last line in compute_group() function
  17. StatBin2 <- ggproto(
  18.   "StatBin2",
  19.   StatBin,
  20.   compute_group = function (data, scales, binwidth = NULL, bins = NULL,
  21.                             center = NULL, boundary = NULL,
  22.                             closed = c("right", "left"), pad = FALSE,
  23.                             breaks = NULL, origin = NULL, right = NULL,
  24.                             drop = NULL, width = NULL) {
  25.     if (!is.null(breaks)) {
  26.       if (!scales$x$is_discrete()) {
  27.         breaks <- scales$x$transform(breaks)
  28.       }
  29.       bins <- ggplot2:::bin_breaks(breaks, closed)
  30.     }
  31.     else if (!is.null(binwidth)) {
  32.       if (is.function(binwidth)) {
  33.         binwidth <- binwidth(data$x)
  34.       }
  35.       bins <- ggplot2:::bin_breaks_width(scales$x$dimension(), binwidth,
  36.                                          center = center, boundary = boundary,
  37.                                          closed = closed)
  38.     }
  39.     else {
  40.       bins <- ggplot2:::bin_breaks_bins(scales$x$dimension(), bins,
  41.                                         center = center, boundary = boundary,
  42.                                         closed = closed)
  43.     }
  44.     res <- ggplot2:::bin_vector(data$x, bins, weight = data$weight, pad = pad)
  45.  
  46.     # drop 0-count bins completely before returning the dataframe
  47.     res <- res[res$count > 0, ]
  48.  
  49.     res
  50.   })
  51.      
  52. ggplot(data, aes(x = value)) +
  53.   geom_histogram(color = "red", stat = StatBin2) + # specify stat = StatBin2
  54.   facet_wrap(~ key, ncol = 1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top