File: scale_defaults.R

package info (click to toggle)
r-cran-ggvis 0.4.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,716 kB
  • sloc: sh: 25; makefile: 2
file content (113 lines) | stat: -rw-r--r-- 3,070 bytes parent folder | download | duplicates (3)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#' @export
set_default_scale <- function(...) {
  stop("set_default_scale has been removed, and has been replaced by scale_numeric, ",
       "scale_nominal, and so on. See ?scales for more information")
}
#' @export
set_dscale <- set_default_scale

# Given a ggvis_scale object, apply defaults and then return the modified object.
apply_scale_defaults <- function(x) UseMethod("apply_scale_defaults")

#' @export
apply_scale_defaults.ggvis_scale <- function(x) x

#' @export
apply_scale_defaults.scale_numeric <- function(x) {
  x$zero <- x$zero %||% FALSE
  x$nice <- x$nice %||% FALSE
  x$clamp <- x$clamp %||% FALSE

  if (is.null(x$range)) {
    x$range <- switch(propname_to_scale(x$property),
      x = "width",
      y = "height",
      stroke = c("#132B43", "#56B1F7"),
      fill = c("#132B43", "#56B1F7"),
      size = c(20, 100),
      fontSize = c(10, 20),
      opacity = c(0, 1),
      angle = c(0, 2 * pi),
      radius = c(0, 50),
      stop("Don't know how to automatically set range for ", x$property, ".")
    )
  }
  if (propname_to_scale(x$property) %in% c("x", "y") && is.null(x$expand)) {
    x$expand <- 0.05
  }

  x
}

#' @export
apply_scale_defaults.scale_datetime <- function(x) {
  x$type <- x$type %||% "time"
  x$clamp <- x$clamp %||% FALSE

  if (is.null(x$range)) {
    x$range <- switch(propname_to_scale(x$property),
      x = "width",
      y = "height",
      stroke = c("#132B43", "#56B1F7"),
      fill = c("#132B43", "#56B1F7"),
      size = c(20, 100),
      fontSize = c(10, 20),
      opacity = c(0, 1),
      angle = c(0, 2 * pi),
      radius = c(0, 50),
      stop("Don't know how to automatically set range for ", x$property, ".")
    )
  }
  if (propname_to_scale(x$property) %in% c("x", "y") && is.null(x$expand)) {
    x$expand <- 0.05
  }

  x
}

#' @export
apply_scale_defaults.scale_ordinal <- function(x) {
  x$points <- x$points %||% TRUE
  x$sort <- x$sort %||% FALSE

  if (is.null(x$range)) {
    x$range <- switch(propname_to_scale(x$property),
      x = "width",
      y = "height",
      stroke = "category10",
      strokeDash = list(c(100000, 1), c(8, 6), c(2, 2), c(3, 4, 10, 4),
                        c(15, 3), c(5, 2, 10, 2)),
      fill = "category10",
      size = c(10, 100),
      stop("Don't know how to automatically set range for ", x$property, ".")
    )
  }
  if (is.null(x$padding) && x$property %in% c("x", "y")) {
    if (isTRUE(x$points)) x$padding <- 0.5
    else x$padding <- 0.1
  }
  x
}

#' @export
apply_scale_defaults.scale_nominal <- function(x) {
  x$points <- x$points %||% TRUE
  x$sort <- x$sort %||% FALSE

  if (is.null(x$range)) {
    x$range <- switch(propname_to_scale(x$property),
      x = "width",
      y = "height",
      stroke = "category10",
      strokeDash = list(c(100000, 1), c(8, 6), c(2, 2), c(3, 4, 10, 4),
                        c(15, 3), c(5, 2, 10, 2)),
      fill = "category10",
      shape = "shapes"
    )
  }
  if (is.null(x$padding) && x$property %in% c("x", "y")) {
    if (isTRUE(x$points)) x$padding <- 0.5
    else x$padding <- 0.1
  }
  x
}