PageRenderTime 76ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/R/geom-violin.r

http://github.com/hadley/ggplot2
R | 177 lines | 80 code | 15 blank | 82 comment | 2 complexity | cdd8eb0ae80274ce02cf7eadf82b4b41 MD5 | raw file
Possible License(s): GPL-2.0
  1. #' Violin plot.
  2. #'
  3. #' @section Aesthetics:
  4. #' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "violin")}
  5. #'
  6. #' @inheritParams layer
  7. #' @inheritParams geom_point
  8. #' @param draw_quantiles If \code{not(NULL)} (default), draw horizontal lines
  9. #' at the given quantiles of the density estimate.
  10. #' @param trim If \code{TRUE} (default), trim the tails of the violins
  11. #' to the range of the data. If \code{FALSE}, don't trim the tails.
  12. #' @param geom,stat Use to override the default connection between
  13. #' \code{geom_violin} and \code{stat_ydensity}.
  14. #' @export
  15. #' @references Hintze, J. L., Nelson, R. D. (1998) Violin Plots: A Box
  16. #' Plot-Density Trace Synergism. The American Statistician 52, 181-184.
  17. #' @examples
  18. #' p <- ggplot(mtcars, aes(factor(cyl), mpg))
  19. #' p + geom_violin()
  20. #'
  21. #' \donttest{
  22. #' p + geom_violin() + geom_jitter(height = 0)
  23. #' p + geom_violin() + coord_flip()
  24. #'
  25. #' # Scale maximum width proportional to sample size:
  26. #' p + geom_violin(scale = "count")
  27. #'
  28. #' # Scale maximum width to 1 for all violins:
  29. #' p + geom_violin(scale = "width")
  30. #'
  31. #' # Default is to trim violins to the range of the data. To disable:
  32. #' p + geom_violin(trim = FALSE)
  33. #'
  34. #' # Use a smaller bandwidth for closer density fit (default is 1).
  35. #' p + geom_violin(adjust = .5)
  36. #'
  37. #' # Add aesthetic mappings
  38. #' # Note that violins are automatically dodged when any aesthetic is
  39. #' # a factor
  40. #' p + geom_violin(aes(fill = cyl))
  41. #' p + geom_violin(aes(fill = factor(cyl)))
  42. #' p + geom_violin(aes(fill = factor(vs)))
  43. #' p + geom_violin(aes(fill = factor(am)))
  44. #'
  45. #' # Set aesthetics to fixed value
  46. #' p + geom_violin(fill = "grey80", colour = "#3366FF")
  47. #'
  48. #' # Show quartiles
  49. #' p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))
  50. #'
  51. #' # Scales vs. coordinate transforms -------
  52. #' if (require("ggplot2movies")) {
  53. #' # Scale transformations occur before the density statistics are computed.
  54. #' # Coordinate transformations occur afterwards. Observe the effect on the
  55. #' # number of outliers.
  56. #' m <- ggplot(movies, aes(y = votes, x = rating, group = cut_width(rating, 0.5)))
  57. #' m + geom_violin()
  58. #' m + geom_violin() + scale_y_log10()
  59. #' m + geom_violin() + coord_trans(y = "log10")
  60. #' m + geom_violin() + scale_y_log10() + coord_trans(y = "log10")
  61. #'
  62. #' # Violin plots with continuous x:
  63. #' # Use the group aesthetic to group observations in violins
  64. #' ggplot(movies, aes(year, budget)) + geom_violin()
  65. #' ggplot(movies, aes(year, budget)) +
  66. #' geom_violin(aes(group = cut_width(year, 10)), scale = "width")
  67. #' }
  68. #' }
  69. geom_violin <- function(mapping = NULL, data = NULL,
  70. stat = "ydensity", position = "dodge",
  71. ...,
  72. draw_quantiles = NULL,
  73. trim = TRUE,
  74. scale = "area",
  75. na.rm = FALSE,
  76. show.legend = NA,
  77. inherit.aes = TRUE) {
  78. layer(
  79. data = data,
  80. mapping = mapping,
  81. stat = stat,
  82. geom = GeomViolin,
  83. position = position,
  84. show.legend = show.legend,
  85. inherit.aes = inherit.aes,
  86. params = list(
  87. trim = trim,
  88. scale = scale,
  89. draw_quantiles = draw_quantiles,
  90. na.rm = na.rm,
  91. ...
  92. )
  93. )
  94. }
  95. #' @rdname ggplot2-ggproto
  96. #' @format NULL
  97. #' @usage NULL
  98. #' @export
  99. GeomViolin <- ggproto("GeomViolin", Geom,
  100. setup_data = function(data, params) {
  101. data$width <- data$width %||%
  102. params$width %||% (resolution(data$x, FALSE) * 0.9)
  103. # ymin, ymax, xmin, and xmax define the bounding rectangle for each group
  104. plyr::ddply(data, "group", transform,
  105. xmin = x - width / 2,
  106. xmax = x + width / 2
  107. )
  108. },
  109. draw_group = function(self, data, ..., draw_quantiles = NULL) {
  110. # Find the points for the line to go all the way around
  111. data <- transform(data,
  112. xminv = x - violinwidth * (x - xmin),
  113. xmaxv = x + violinwidth * (xmax - x)
  114. )
  115. # Make sure it's sorted properly to draw the outline
  116. newdata <- rbind(
  117. plyr::arrange(transform(data, x = xminv), y),
  118. plyr::arrange(transform(data, x = xmaxv), -y)
  119. )
  120. # Close the polygon: set first and last point the same
  121. # Needed for coord_polar and such
  122. newdata <- rbind(newdata, newdata[1,])
  123. # Draw quantiles if requested, so long as there is non-zero y range
  124. if (length(draw_quantiles) > 0 & !scales::zero_range(range(data$y))) {
  125. stopifnot(all(draw_quantiles >= 0), all(draw_quantiles <= 1))
  126. # Compute the quantile segments and combine with existing aesthetics
  127. quantiles <- create_quantile_segment_frame(data, draw_quantiles)
  128. aesthetics <- data[
  129. rep(1, nrow(quantiles)),
  130. setdiff(names(data), c("x", "y")),
  131. drop = FALSE
  132. ]
  133. both <- cbind(quantiles, aesthetics)
  134. quantile_grob <- GeomPath$draw_panel(both, ...)
  135. ggname("geom_violin", grobTree(
  136. GeomPolygon$draw_panel(newdata, ...),
  137. quantile_grob)
  138. )
  139. } else {
  140. ggname("geom_violin", GeomPolygon$draw_panel(newdata, ...))
  141. }
  142. },
  143. draw_key = draw_key_polygon,
  144. default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,
  145. alpha = NA, linetype = "solid"),
  146. required_aes = c("x", "y")
  147. )
  148. # Returns a data.frame with info needed to draw quantile segments.
  149. create_quantile_segment_frame <- function(data, draw_quantiles) {
  150. dens <- cumsum(data$density) / sum(data$density)
  151. ecdf <- stats::approxfun(dens, data$y)
  152. ys <- ecdf(draw_quantiles) # these are all the y-values for quantiles
  153. # Get the violin bounds for the requested quantiles.
  154. violin.xminvs <- (stats::approxfun(data$y, data$xminv))(ys)
  155. violin.xmaxvs <- (stats::approxfun(data$y, data$xmaxv))(ys)
  156. # We have two rows per segment drawn. Each segment gets its own group.
  157. data.frame(
  158. x = interleave(violin.xminvs, violin.xmaxvs),
  159. y = rep(ys, each = 2),
  160. group = rep(ys, each = 2)
  161. )
  162. }