/R/geom-quantile.r

http://github.com/hadley/ggplot2 · R · 69 lines · 32 code · 2 blank · 35 comment · 0 complexity · 256da5b4c9516af4885be6a1a2532a3b MD5 · raw file

  1. #' Add quantile lines from a quantile regression.
  2. #'
  3. #' This can be used as a continuous analogue of a geom_boxplot.
  4. #'
  5. #' @section Aesthetics:
  6. #' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "quantile")}
  7. #'
  8. #' @export
  9. #' @inheritParams layer
  10. #' @inheritParams geom_point
  11. #' @inheritParams geom_path
  12. #' @param method.args List of additional arguments passed on to the modelling
  13. #' function defined by \code{method}.
  14. #' @param geom,stat Use to override the default connection between
  15. #' \code{geom_quantile} and \code{stat_quantile}.
  16. #' @examples
  17. #' m <- ggplot(mpg, aes(displ, 1 / hwy)) + geom_point()
  18. #' m + geom_quantile()
  19. #' m + geom_quantile(quantiles = 0.5)
  20. #' q10 <- seq(0.05, 0.95, by = 0.05)
  21. #' m + geom_quantile(quantiles = q10)
  22. #'
  23. #' # You can also use rqss to fit smooth quantiles
  24. #' m + geom_quantile(method = "rqss")
  25. #' # Note that rqss doesn't pick a smoothing constant automatically, so
  26. #' # you'll need to tweak lambda yourself
  27. #' m + geom_quantile(method = "rqss", lambda = 0.1)
  28. #'
  29. #' # Set aesthetics to fixed value
  30. #' m + geom_quantile(colour = "red", size = 2, alpha = 0.5)
  31. geom_quantile <- function(mapping = NULL, data = NULL,
  32. stat = "quantile", position = "identity",
  33. ...,
  34. lineend = "butt",
  35. linejoin = "round",
  36. linemitre = 1,
  37. na.rm = FALSE,
  38. show.legend = NA,
  39. inherit.aes = TRUE) {
  40. layer(
  41. data = data,
  42. mapping = mapping,
  43. stat = stat,
  44. geom = GeomQuantile,
  45. position = position,
  46. show.legend = show.legend,
  47. inherit.aes = inherit.aes,
  48. params = list(
  49. lineend = lineend,
  50. linejoin = linejoin,
  51. linemitre = linemitre,
  52. na.rm = na.rm,
  53. ...
  54. )
  55. )
  56. }
  57. #' @rdname ggplot2-ggproto
  58. #' @format NULL
  59. #' @usage NULL
  60. #' @export
  61. #' @include geom-path.r
  62. GeomQuantile <- ggproto("GeomQuantile", GeomPath,
  63. default_aes = defaults(
  64. aes(weight = 1, colour = "#3366FF", size = 0.5),
  65. GeomPath$default_aes
  66. )
  67. )