File: scale-identity.r

package info (click to toggle)
r-cran-ggplot2 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,412 kB
  • sloc: sh: 9; makefile: 1
file content (83 lines) | stat: -rw-r--r-- 2,494 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#' Use values without scaling.
#'
#' @name scale_identity
#' @param ... Other arguments passed on to \code{\link{discrete_scale}} or
#'   \code{\link{continuous_scale}}
#' @param guide Guide to use for this scale - defaults to \code{"none"}.
#' @examples
#' colour <- c("red", "green", "blue", "yellow")
#' qplot(1:4, 1:4, fill = colour, geom = "tile")
#' qplot(1:4, 1:4, fill = colour, geom = "tile") + scale_fill_identity()
#'
#' # To get a legend guide, specify guide = "legend"
#' qplot(1:4, 1:4, fill = colour, geom = "tile") +
#'   scale_fill_identity(guide = "legend")
#' # But you'll typically also need to supply breaks and labels:
#' qplot(1:4, 1:4, fill = colour, geom = "tile") +
#'   scale_fill_identity("trt", labels = letters[1:4], breaks = colour,
#'   guide = "legend")
#'
#' # cyl scaled to appropriate size
#' qplot(mpg, wt, data = mtcars, size = cyl)
#'
#' # cyl used as point size
#' qplot(mpg, wt, data = mtcars, size = cyl) + scale_size_identity()
NULL

#' @rdname scale_identity
#' @export
scale_colour_identity <- function(..., guide = "none") {
  identity_scale(discrete_scale("colour", "identity", identity_pal(), ..., guide = guide))
}

#' @rdname scale_identity
#' @export
scale_fill_identity <- function(..., guide = "none") {
  identity_scale(discrete_scale("fill", "identity", identity_pal(), ..., guide = guide))

}

#' @rdname scale_identity
#' @export
scale_shape_identity <- function(..., guide = "none") {
  identity_scale(continuous_scale("shape", "identity", identity_pal(), ...,  guide = guide))
}

#' @rdname scale_identity
#' @export
scale_linetype_identity <- function(..., guide = "none") {
  identity_scale(discrete_scale("linetype", "identity", identity_pal(), ..., guide = guide))

}

#' @rdname scale_identity
#' @export
scale_alpha_identity <- function(..., guide = "none") {
  identity_scale(continuous_scale("alpha", "identity", identity_pal(), ..., guide = guide))

}

#' @rdname scale_identity
#' @export
scale_size_identity <- function(..., guide = "none") {
  identity_scale(continuous_scale("size", "identity", identity_pal(), ..., guide = guide))
}

identity_scale <- function(x) {
  structure(x, class = c("identity", class(x)))
}

#' @export
scale_map.identity <- function(scale, x) {
  if (is.factor(x)) {
    as.character(x)
  } else {
    x
  }
}
#' @export
scale_train.identity <- function(scale, x) {
  # do nothing if no guide, otherwise train so we know what breaks to use
  if (scale$guide == "none") return()
  NextMethod()
}