/
scale-size.r
66 lines (62 loc) · 2.06 KB
/
scale-size.r
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
#' Size scale.
#'
#' @name scale_size
#' @inheritParams scale_x_continuous
#' @param range a numeric vector of length 2 that specifies the minimum and
#' maximum size of the plotting symbol after transformation.
#' @examples
#' \donttest{
#' (p <- qplot(mpg, cyl, data=mtcars, size=cyl))
#' p + scale_size("cylinders")
#' p + scale_size("number\nof\ncylinders")
#'
#' p + scale_size(range = c(0, 10))
#' p + scale_size(range = c(1, 2))
#'
#' # Map area, instead of width/radius
#' # Perceptually, this is a little better
#' p + scale_area()
#' p + scale_area(range = c(1, 25))
#'
#' # Also works with factors, but not a terribly good
#' # idea, unless your factor is ordered, as in this example
#' qplot(mpg, cyl, data=mtcars, size=factor(cyl))
#'
#' # To control the size mapping for discrete variable, use
#' # scale_size_manual:
#' last_plot() + scale_size_manual(values=c(2,4,6))
#' }
NULL
#' @rdname scale_size
#' @export
scale_size_continuous <- function(..., range = c(1, 6)) {
continuous_scale("size", "size_c", rescale_pal(range), ...)
}
#' @rdname scale_size
#' @export
scale_size <- scale_size_continuous
#' @rdname scale_size
#' @export
scale_size_discrete <- function(..., range = c(1, 6)) {
discrete_scale("size", "size_d",
function(n) seq(range[1], range[2], length = n), ...)
}
#' Scale area instead of radius, for size.
#'
#' When \code{scale_size_area} is used, the default behavior is to scale the
#' area of points to be proportional to the value.
#'
#' Note that this controls the size scale, so it will also control
#' the thickness of lines. Line thickness will be proportional to the square
#' root of the value, which is probably undesirable in most cases.
#'
#' @param ... Other arguments passed on to \code{\link{continuous_scale}}
#' to control name, limits, breaks, labels and so forth.
#' @param range Range of output sizes. Should be greater than 0.
#' @param max_size Size of largest points.
#' @export
scale_size_area <- function(..., max_size = 6) {
continuous_scale("size", "area",
pal = abs_area(max_size),
rescaler = rescale_max, ...)
}