Advertisement
Guest User

geom_flat_violin.R

a guest
Oct 3rd, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. # somewhat hackish solution to:
  2. # https://twitter.com/EamonCaddigan/status/646759751242620928
  3. # based mostly on copy/pasting from ggplot2 geom_violin source:
  4. # https://github.com/hadley/ggplot2/blob/master/R/geom-violin.r
  5.  
  6. # library(ggplot2)
  7. # library(dplyr)
  8.  
  9. "%||%" <- function(a, b) {
  10. if (!is.null(a)) a else b
  11. }
  12.  
  13. geom_flat_violin <- function(mapping = NULL, data = NULL, stat = "ydensity",
  14. position = "dodge", trim = TRUE, scale = "area",
  15. show.legend = NA, inherit.aes = TRUE, ...) {
  16. layer(
  17. data = data,
  18. mapping = mapping,
  19. stat = stat,
  20. geom = GeomFlatViolin,
  21. position = position,
  22. show.legend = show.legend,
  23. inherit.aes = inherit.aes,
  24. params = list(
  25. trim = trim,
  26. scale = scale,
  27. ...
  28. )
  29. )
  30. }
  31.  
  32. #' @rdname ggplot2-ggproto
  33. #' @format NULL
  34. #' @usage NULL
  35. #' @export
  36. GeomFlatViolin <-
  37. ggproto("GeomFlatViolin", Geom,
  38. setup_data = function(data, params) {
  39. data$width <- data$width %||%
  40. params$width %||% (resolution(data$x, FALSE) * 0.9)
  41.  
  42. # ymin, ymax, xmin, and xmax define the bounding rectangle for each group
  43. data %>%
  44. group_by(group) %>%
  45. mutate(ymin = min(y),
  46. ymax = max(y),
  47. xmin = x,
  48. xmax = x + width / 2)
  49. # )
  50. },
  51.  
  52. draw_group = function(data, panel_scales, coord) {
  53. # Find the points for the line to go all the way around
  54. data <- transform(data, xminv = x,
  55. xmaxv = x + violinwidth * (xmax - x))
  56.  
  57. # Make sure it's sorted properly to draw the outline
  58. newdata <- rbind(plyr::arrange(transform(data, x = xminv), y),
  59. plyr::arrange(transform(data, x = xmaxv), -y))
  60.  
  61. # Close the polygon: set first and last point the same
  62. # Needed for coord_polar and such
  63. newdata <- rbind(newdata, newdata[1,])
  64.  
  65. ggplot2:::ggname("geom_flat_violin", GeomPolygon$draw_panel(newdata, panel_scales, coord))
  66. },
  67.  
  68. draw_key = draw_key_polygon,
  69.  
  70. default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,
  71. alpha = NA, linetype = "solid"),
  72.  
  73. required_aes = c("x", "y")
  74. )
  75.  
  76.  
  77. # ### Example:
  78. # ggplot(diamonds, aes(cut, carat)) +
  79. # geom_flat_violin() +
  80. # coord_flip()
  81. #
  82. # # half violin plot with raw data ------------------------------------------
  83. #
  84. # ## create a violin plot of Sepal.Length per species
  85. # ## using the custom function geom_flat_violin()
  86. #
  87. # ggplot(data = iris,
  88. # mapping = aes(x = Species, y = Sepal.Length, fill = Species)) +
  89. # geom_flat_violin(scale = "count", trim = FALSE) +
  90. # stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1),
  91. # geom = "pointrange", position = position_nudge(0.05)) +
  92. # geom_dotplot(binaxis = "y", dotsize = 0.5, stackdir = "down", binwidth = 0.1,
  93. # position = position_nudge(-0.025)) +
  94. # theme(legend.position = "none") +
  95. # labs(x = "Species", y = "Sepal length (cm)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement