Guest User

Untitled

a guest
Jul 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. hexGrob <- function(x, y, size = rep(1, length(x)), gp = gpar()) {
  2. stopifnot(length(y) == length(x))
  3. n <- length(x)
  4. dx <- resolution(x, FALSE)
  5. dy <- resolution(y, FALSE)
  6. if(dx == 1) dx <- dy
  7. if(dy == 1) dy <- dx
  8. dratio <- dy/dx
  9. grid::polygonGrob(rep.int(cos(pi/2 + pi/3 * 0:5)*dx/sqrt(3), n) +
  10. rep(x + y/2/dratio, each = 6),
  11. rep.int(sin(pi/2 + pi/3 * 0:5)*dy/sqrt(3), n) +
  12. rep(sqrt(3)*y/2, each = 6),
  13. id.lengths = rep(6, n),
  14. gp=gp)
  15. }
  16.  
  17.  
  18. GeomSimplerHex <- ggproto("GeomSimperHex", Geom,
  19. required_aes = c("x", "y"),
  20.  
  21. default_aes = aes(
  22. colour = "black", fill = "gray", size = 0.5,
  23. linetype = 1, alpha = 1
  24. ),
  25.  
  26. draw_key = draw_key_polygon,
  27.  
  28. draw_panel = function(data, panel_params, coord) {
  29. coords <- coord$transform(data, panel_params)
  30. hexGrob(
  31. coords$x, coords$y,
  32. gp = grid::gpar(
  33. col = coords$colour,
  34. fill = alpha(coords$fill, coords$alpha),
  35. lwd = coords$size * .pt,
  36. lty = coords$linetype
  37. )
  38. )
  39. }
  40. )
  41. geom_simpler_hex <- function(mapping = NULL, data = NULL, stat = "identity",
  42. position = "identity", na.rm = FALSE, show.legend = NA,
  43. inherit.aes = TRUE, ...) {
  44. layer(
  45. geom = GeomSimplerHex, mapping = mapping, data = data, stat = stat,
  46. position = position, show.legend = show.legend, inherit.aes = inherit.aes,
  47. params = list(na.rm = na.rm, ...)
  48. )
  49. }
Add Comment
Please, Sign In to add comment