File: guide-axis-stack.R

package info (click to toggle)
r-cran-ggplot2 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 9,944 kB
  • sloc: sh: 15; makefile: 5
file content (255 lines) | stat: -rw-r--r-- 7,833 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#' @include guide-axis.R
NULL

#' Stacked axis guides
#'
#' This guide can stack other position guides that represent position scales,
#' like those created with [scale_(x|y)_continuous()][scale_x_continuous()] and
#' [scale_(x|y)_discrete()][scale_x_discrete()].
#'
#' @inheritParams guide_axis
#' @param first A position guide given as one of the following:
#' * A string, for example `"axis"`.
#' * A call to a guide function, for example `guide_axis()`.
#' @param ... Additional guides to stack given in the same manner as `first`.
#' @param spacing A [unit()] objects that determines how far separate guides are
#'   spaced apart.
#'
#' @details
#' The `first` guide will be placed closest to the panel and any subsequent
#' guides provided through `...` will follow in the given order.
#'
#' @export
#'
#' @examples
#' #' # A standard plot
#' p <- ggplot(mpg, aes(displ, hwy)) +
#'   geom_point() +
#'   theme(axis.line = element_line())
#'
#' # A normal axis first, then a capped axis
#' p + guides(x = guide_axis_stack("axis", guide_axis(cap = "both")))
guide_axis_stack <- function(first = "axis", ..., title = waiver(), theme = NULL,
                             spacing = NULL, order = 0, position = waiver()) {

  check_object(spacing, is.unit, "{.cls unit}", allow_null = TRUE)

  # Validate guides
  axes <- list2(first, ...)
  axes <- lapply(axes, validate_guide)

  # Check available aesthetics
  available <- lapply(axes, `[[`, name = "available_aes")
  available <- vapply(available, function(x) all(c("x", "y") %in% x), logical(1))
  if (all(!available)) {
    cli::cli_abort(paste0(
      "{.fn guide_axis_stack} can only use guides that handle {.field x} and ",
      "{.field y} aesthetics."
    ))
  }

  # Remove guides that don't support x/y aesthetics
  if (any(!available)) {
    remove  <- which(!available)
    removed <- vapply(axes[remove], snake_class, character(1))
    axes[remove] <- NULL
    cli::cli_warn(c(paste0(
      "{.fn guide_axis_stack} cannot use the following guide{?s}: ",
      "{.and {.fn {removed}}}."
    ), i = "Guides need to handle {.field x} and {.field y} aesthetics."))
  }

  params <- lapply(axes, `[[`, name = "params")

  new_guide(
    title = title,
    theme = theme,
    guides = axes,
    guide_params = params,
    spacing = spacing,
    available_aes = c("x", "y", "theta", "r"),
    order = order,
    position = position,
    name = "stacked_axis",
    super = GuideAxisStack
  )
}

#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
GuideAxisStack <- ggproto(
  "GuideAxisStack", GuideAxis,

  params = list(
    # List of guides to track the guide objects
    guides    = list(),
    # List of parameters to each guide
    guide_params = list(),
    spacing   = NULL,
    # Standard guide stuff
    name      = "stacked_axis",
    title     = waiver(),
    theme     = NULL,
    angle     = waiver(),
    hash      = character(),
    position  = waiver(),
    direction = NULL,
    order     = 0
  ),

  available_aes = c("x", "y", "theta", "r"),

  # Doesn't depend on keys, but on member axis' class
  hashables = exprs(title, lapply(guides, snake_class), name),

  # Sets position, loops through guides to train
  train = function(self, params = self$params, scale, aesthetic = NULL, ...) {
    position <- arg_match0(
      params$position, c(.trbl, "theta", "theta.sec"),
      arg_nm = "position"
    )
    for (i in seq_along(params$guides)) {
      params$guide_params[[i]]$position <- position
      params$guide_params[[i]]$angle <- params$guide_params[[i]]$angle %|W|% params$angle
      params$guide_params[[i]] <- params$guides[[i]]$train(
        params = params$guide_params[[i]],
        scale = scale, aesthetic = aesthetic,
        ...
      )
    }
    params
  },

  # Just loops through guides
  transform = function(self, params, coord, panel_params) {
    for (i in seq_along(params$guides)) {
      params$guide_params[[i]] <- params$guides[[i]]$transform(
        params = params$guide_params[[i]],
        coord = coord, panel_params = panel_params
      )
    }
    params
  },

  # Just loops through guides
  get_layer_key = function(params, layers) {
    for (i in seq_along(params$guides)) {
      params$guide_params[[i]] <- params$guides[[i]]$get_layer_key(
        params = params$guide_params[[i]],
        layers = layers
      )
    }
    params
  },

  draw = function(self, theme, position = NULL, direction = NULL,
                  params = self$params) {
    theme <- add_theme(theme, params$theme)

    position  <- params$position  %||% position
    direction <- params$direction %||% direction

    # If we are instructed to not draw labels at interior panels, just render
    # the first axis
    draw_label  <- params$draw_label %||% TRUE
    guide_index <- if (draw_label) seq_along(params$guides) else 1L

    if (position %in% c("theta", "theta.sec")) {
      # If we are a theta guide, we need to keep track how much space in the
      # radial direction a guide occupies, and add that as an offset to the
      # next guide.
      offset  <- unit(0, "cm")
      spacing <- params$spacing %||% unit(2.25, "pt")
      grobs   <- list()

      for (i in guide_index) {
        # Add offset to params
        pars <- params$guide_params[[i]]
        pars$stack_offset <- offset
        # Draw guide
        grobs[[i]] <- params$guides[[i]]$draw(
          theme, position = position, direction = direction,
          params = pars
        )
        # Increment offset
        if (!is.null(grobs[[i]]$offset)) {
          offset <- offset + spacing + grobs[[i]]$offset
          offset <- convertUnit(offset, "cm")
        }
      }
      grob <- inject(grobTree(!!!grobs))
      return(grob)
    }

    # Loop through every guide's draw method
    grobs <- list()
    for (i in guide_index) {
      pars <- params$guide_params[[i]]
      pars$draw_label <- draw_label
      grobs[[i]] <- params$guides[[i]]$draw(
        theme, position = position, direction = direction,
        params = pars
      )
    }

    # Remove empty grobs
    grobs <- grobs[!vapply(grobs, is.zero, logical(1))]
    if (length(grobs) == 0) {
      return(zeroGrob())
    }
    along <- seq_along(grobs)

    # Get sizes
    widths  <- inject(unit.c(!!!lapply(grobs, grobWidth)))
    heights <- inject(unit.c(!!!lapply(grobs, grobHeight)))

    # Set spacing
    if (is.null(params$spacing)) {
      aes <- if (position %in% c("top", "bottom")) "x" else "y"
      spacing <- paste("axis.ticks.length", aes, position, sep = ".")
      spacing <- calc_element(spacing, theme)
    } else {
      spacing <- params$spacing
    }

    # Reorder grobs/sizes if necessary
    if (position %in% c("top", "left")) {
      along   <- rev(along)
      widths  <- rev(widths)
      heights <- rev(heights)
    }

    # Place guides in a gtable, apply spacing
    if (position %in% c("bottom", "top")) {
      gt <- gtable(widths = unit(1, "npc"), heights = heights)
      gt <- gtable_add_grob(gt, grobs, t = along, l = 1, name = "axis", clip = "off")
      gt <- gtable_add_row_space(gt, height = spacing)
      vp <- exec(
        viewport,
        y    = unit(as.numeric(position == "bottom"), "npc"),
        height = grobHeight(gt),
        just = opposite_position(position)
      )
    } else {
      gt <- gtable(widths = widths, heights = unit(1, "npc"))
      gt <- gtable_add_grob(gt, grobs, t = 1, l = along, name = "axis", clip = "off")
      gt <- gtable_add_col_space(gt, width = spacing)
      vp <- exec(
        viewport,
        x     = unit(as.numeric(position == "left"), "npc"),
        width = grobWidth(gt),
        just  = opposite_position(position)
      )
    }

    absoluteGrob(
      grob   = gList(gt),
      width  = gtable_width(gt),
      height = gtable_height(gt),
      vp = vp
    )
  }
)