Skip to content

Commit 6c2fe76

Browse files
authored
Implement immutable scales in the panel_params for CoordCartesian (#3356, part of #3322)
* remove assumption that the panel_params contain any particular element in any coord code, update structure of panel_params in CoordCartesian to use scale syntax in preparation for updated guides * move scale_range to coord_sf(), since it is no longer used in coord_cartesian() * Update scales documentation, tidy styling of Scales methods * finish tidy styling of scale-.r * small modifications to scales such that ViewScales can be applied generically to all scales * move view scale construction outside coord-cartesian.R * move view scale to separate file * move all view scale code to scale-view.r, rename constructors to view_scale_* * Better documentation of ViewScale objects * make view_scale_secondary() return a ViewScale subclass
1 parent cb41aa0 commit 6c2fe76

25 files changed

+866
-466
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Collate:
199199
'scale-shape.r'
200200
'scale-size.r'
201201
'scale-type.R'
202+
'scale-view.r'
202203
'scale-viridis.r'
203204
'scales-.r'
204205
'stat-bin.r'

R/coord-.r

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,51 +64,27 @@ Coord <- ggproto("Coord",
6464
render_fg = function(panel_params, theme) element_render(theme, "panel.border"),
6565

6666
render_bg = function(panel_params, theme) {
67-
guide_grid(theme,
68-
panel_params$x.minor,
69-
panel_params$x.major,
70-
panel_params$y.minor,
71-
panel_params$y.major)
67+
stop("Not implemented", call. = FALSE)
7268
},
7369

7470
render_axis_h = function(panel_params, theme) {
75-
arrange <- panel_params$x.arrange %||% c("secondary", "primary")
76-
77-
list(
78-
top = render_axis(panel_params, arrange[1], "x", "top", theme),
79-
bottom = render_axis(panel_params, arrange[2], "x", "bottom", theme)
80-
)
71+
stop("Not implemented", call. = FALSE)
8172
},
8273

8374
render_axis_v = function(panel_params, theme) {
84-
arrange <- panel_params$y.arrange %||% c("primary", "secondary")
85-
86-
list(
87-
left = render_axis(panel_params, arrange[1], "y", "left", theme),
88-
right = render_axis(panel_params, arrange[2], "y", "right", theme)
89-
)
75+
stop("Not implemented", call. = FALSE)
9076
},
9177

9278
# transform range given in transformed coordinates
9379
# back into range in given in (possibly scale-transformed)
9480
# data coordinates
9581
backtransform_range = function(self, panel_params) {
96-
warning(
97-
"range backtransformation not implemented in this coord; results may be wrong.",
98-
call. = FALSE
99-
)
100-
# return result from range function for backwards compatibility
101-
# before ggplot2 3.0.1
102-
self$range(panel_params)
82+
stop("Not implemented", call. = FALSE)
10383
},
10484

10585
# return range stored in panel_params
10686
range = function(panel_params) {
107-
warning(
108-
"range calculation not implemented in this coord; results may be wrong.",
109-
call. = FALSE
110-
)
111-
list(x = panel_params$x.range, y = panel_params$y.range)
87+
stop("Not implemented", call. = FALSE)
11288
},
11389

11490
setup_panel_params = function(scale_x, scale_y, params = list()) {

R/coord-cartesian-.r

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,50 +79,91 @@ CoordCartesian <- ggproto("CoordCartesian", Coord,
7979
is_free = function() TRUE,
8080

8181
distance = function(x, y, panel_params) {
82-
max_dist <- dist_euclidean(panel_params$x.range, panel_params$y.range)
82+
max_dist <- dist_euclidean(panel_params$x$dimension(), panel_params$y$dimension())
8383
dist_euclidean(x, y) / max_dist
8484
},
8585

8686
range = function(panel_params) {
87-
list(x = panel_params$x.range, y = panel_params$y.range)
87+
list(x = panel_params$x$dimension(), y = panel_params$y$dimension())
8888
},
8989

9090
backtransform_range = function(self, panel_params) {
9191
self$range(panel_params)
9292
},
9393

9494
transform = function(data, panel_params) {
95-
rescale_x <- function(data) rescale(data, from = panel_params$x.range)
96-
rescale_y <- function(data) rescale(data, from = panel_params$y.range)
97-
98-
data <- transform_position(data, rescale_x, rescale_y)
95+
data <- transform_position(data, panel_params$x$rescale, panel_params$y$rescale)
9996
transform_position(data, squish_infinite, squish_infinite)
10097
},
10198

10299
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
103-
train_cartesian <- function(scale, limits, name) {
104-
range <- scale_range(scale, limits, self$expand)
100+
c(
101+
view_scales_from_scale(scale_x, self$limits$x, self$expand),
102+
view_scales_from_scale(scale_y, self$limits$y, self$expand)
103+
)
104+
},
105+
106+
render_bg = function(panel_params, theme) {
107+
guide_grid(
108+
theme,
109+
panel_params$x$break_positions_minor(),
110+
panel_params$x$break_positions(),
111+
panel_params$y$break_positions_minor(),
112+
panel_params$y$break_positions()
113+
)
114+
},
105115

106-
out <- scale$break_info(range)
107-
out$arrange <- scale$axis_order()
108-
names(out) <- paste(name, names(out), sep = ".")
109-
out
110-
}
116+
render_axis_h = function(panel_params, theme) {
117+
arrange <- panel_params$x.arrange %||% c("secondary", "primary")
118+
arrange_scale_keys <- c("primary" = "x", "secondary" = "x.sec")[arrange]
119+
arrange_scales <- panel_params[arrange_scale_keys]
111120

112-
c(
113-
train_cartesian(scale_x, self$limits$x, "x"),
114-
train_cartesian(scale_y, self$limits$y, "y")
121+
list(
122+
top = draw_view_scale_axis(arrange_scales[[1]], "top", theme),
123+
bottom = draw_view_scale_axis(arrange_scales[[2]], "bottom", theme)
124+
)
125+
},
126+
127+
render_axis_v = function(panel_params, theme) {
128+
arrange <- panel_params$y.arrange %||% c("primary", "secondary")
129+
arrange_scale_keys <- c("primary" = "y", "secondary" = "y.sec")[arrange]
130+
arrange_scales <- panel_params[arrange_scale_keys]
131+
132+
list(
133+
left = draw_view_scale_axis(arrange_scales[[1]], "left", theme),
134+
right = draw_view_scale_axis(arrange_scales[[2]], "right", theme)
115135
)
116136
}
117137
)
118138

119-
scale_range <- function(scale, limits = NULL, expand = TRUE) {
120-
expansion <- if (expand) expand_default(scale) else c(0, 0)
139+
view_scales_from_scale <- function(scale, coord_limits = NULL, expand = TRUE) {
140+
expansion <- if (expand) expand_default(scale) else expand_scale(0, 0)
141+
limits <- scale$get_limits()
121142

122-
if (is.null(limits)) {
123-
scale$dimension(expansion)
143+
if (is.null(coord_limits)) {
144+
continuous_range <- scale$dimension(expansion, limits)
124145
} else {
125-
range <- range(scale$transform(limits))
126-
expand_range(range, expansion[1], expansion[2])
146+
continuous_range <- range(scale$transform(coord_limits))
147+
continuous_range <- expand_range4(continuous_range, expansion)
148+
}
149+
150+
aesthetic <- scale$aesthetics[1]
151+
152+
view_scales <- list(
153+
view_scale_primary(scale, limits, continuous_range),
154+
sec = view_scale_secondary(scale, limits, continuous_range),
155+
arrange = scale$axis_order(),
156+
range = continuous_range
157+
)
158+
names(view_scales) <- c(aesthetic, paste0(aesthetic, ".", names(view_scales)[-1]))
159+
160+
view_scales
161+
}
162+
163+
draw_view_scale_axis <- function(view_scale, axis_position, theme) {
164+
if(is.null(view_scale) || view_scale$is_empty()) {
165+
return(zeroGrob())
127166
}
167+
168+
draw_axis(view_scale$break_positions(), view_scale$get_labels(), axis_position, theme)
128169
}

R/coord-flip.r

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ CoordFlip <- ggproto("CoordFlip", CoordCartesian,
4848
self$range(panel_params)
4949
},
5050

51-
range = function(panel_params) {
51+
range = function(self, panel_params) {
5252
# summarise_layout() expects the original x and y ranges here,
5353
# not the ones we would get after flipping the axes
54-
list(x = panel_params$y.range, y = panel_params$x.range)
54+
un_flipped_range <- ggproto_parent(CoordCartesian, self)$range(panel_params)
55+
list(x = un_flipped_range$y, y = un_flipped_range$x)
5556
},
5657

5758
setup_panel_params = function(self, scale_x, scale_y, params = list()) {

R/coord-sf.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,14 @@ parse_axes_labeling <- function(x) {
466466
labs = unlist(strsplit(x, ""))
467467
list(top = labs[1], right = labs[2], bottom = labs[3], left = labs[4])
468468
}
469+
470+
scale_range <- function(scale, limits = NULL, expand = TRUE) {
471+
expansion <- if (expand) expand_default(scale) else expand_scale(0, 0)
472+
473+
if (is.null(limits)) {
474+
scale$dimension(expansion)
475+
} else {
476+
continuous_range <- range(scale$transform(limits))
477+
expand_range4(continuous_range, expansion)
478+
}
479+
}

R/coord-transform.r

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,34 @@ CoordTrans <- ggproto("CoordTrans", Coord,
150150
train_trans(scale_x, self$limits$x, self$trans$x, "x"),
151151
train_trans(scale_y, self$limits$y, self$trans$y, "y")
152152
)
153+
},
154+
155+
render_bg = function(panel_params, theme) {
156+
guide_grid(
157+
theme,
158+
panel_params$x.minor,
159+
panel_params$x.major,
160+
panel_params$y.minor,
161+
panel_params$y.major
162+
)
163+
},
164+
165+
render_axis_h = function(panel_params, theme) {
166+
arrange <- panel_params$x.arrange %||% c("secondary", "primary")
167+
168+
list(
169+
top = render_axis(panel_params, arrange[1], "x", "top", theme),
170+
bottom = render_axis(panel_params, arrange[2], "x", "bottom", theme)
171+
)
172+
},
173+
174+
render_axis_v = function(panel_params, theme) {
175+
arrange <- panel_params$y.arrange %||% c("primary", "secondary")
176+
177+
list(
178+
left = render_axis(panel_params, arrange[1], "y", "left", theme),
179+
right = render_axis(panel_params, arrange[2], "y", "right", theme)
180+
)
153181
}
154182
)
155183

0 commit comments

Comments
 (0)